Hippies

標題: Linux 系統備份與還原 [打印本頁]

作者: kevinhu    時間: 2015-6-30 11:51
標題: Linux 系統備份與還原
本帖最後由 kevinhu 於 2015-6-30 12:01 編輯

搞Linux系統有一段時間了,一直都沒好好的思考系統的備份與還原問題,前一陣子處理過把系統整搬移到別顆硬碟的處理,但畢竟不是屬於正常的備份還原的處理方式,因此,想想還是要有個常態的作業規則才行,所以就在網上搜了一下,看看有沒有些文章可以來參考的,簡單的查了下,有鳥哥的教學這算是最完整的學習了,不過我想要的是能夠速成點的,又找了下,找到了另一個保哥的網站,把他在Linux上的備份還原用Linux的基本指令來完成,這正是我想要的方式,與是就照做了一下,基本上還算順利完成,當然還是要把一些路徑調整成符合自己的Server的環境情況才行。
為了方便日後備忘,除了把鏈接留下外,還抄了部分重點的文件,供日後備忘也供有興趣的人學習參考。
保哥的 Linux 備份/還原筆記(支援甲機備份,乙機還原)
原文節錄入下

Backup shell

#!/bin/bash
rnd=$RANDOM
date=`date +%y%m%d`
backup_to="/mnt/backup"
echo -n "Today is $date. Writing /tmp/$rnd-exclude-file-list ... "
echo "lost+found"                    > /tmp/$rnd-exclude-file-list
echo "/mnt/*"                       >> /tmp/$rnd-exclude-file-list
echo "/media/*"                     >> /tmp/$rnd-exclude-file-list
echo "/proc/*"                      >> /tmp/$rnd-exclude-file-list
echo "/dev/*"                       >> /tmp/$rnd-exclude-file-list
echo "/sys/*"                       >> /tmp/$rnd-exclude-file-list
echo "/tmp/*"                       >> /tmp/$rnd-exclude-file-list
echo "/var/spool/squid/*"           >> /tmp/$rnd-exclude-file-list
echo "done."
echo ""

#----------------------------------------------------
datetime=`date "+%Y-%m-%d %H:%M:%S"`
echo "Backup job started at $datetime"

#####################################################
tar -cf $backup_to/$date-full-backup.tar / --totals --absolute-names \
    --ignore-failed-read --exclude-from=/tmp/$rnd-exclude-file-list
if [ "${?}" != 0 ] ; then    echo "Backup failed."
fi
rm -f /tmp/$rnd-exclude-file-list

#####################################################
datetime=`date "+%Y-%m-%d %H:%M:%S"`
echo "Backup job ended at $datetime"
echo ""
#----------------------------------------------------
datetime=`date "+%Y-%m-%d %H:%M:%S"`
echo "Compressing job started at $datetime"
#####################################################
gzip --best --rsyncable $backup_to/$date-full-backup.tar
if [ "${?}" != 0 ] ; then
    echo "Gzip failed."
fi
gzip --test $backup_to/$date-full-backup.tar.gz
if [ "${?}" != 0 ] ; then
    echo "Check the compressed file integrity failed."
fi
#####################################################
datetime=`date "+%Y-%m-%d %H:%M:%S"`
echo "Compressing job ended at $datetime"


Restore 程序


請尊重原作者,節錄部分為依原作者的文章部分複製,如有需要完整原文章請至原作者網站向作者洽詢。




作者: kevinhu    時間: 2016-4-2 21:35
如果要進行整個硬碟系統的備份的話,底下這篇是很適合參考的文章

How To Do A Full Backup
Using The tar Command



It's a good idea to do a full backup of the hard-drive after the initial installation as well as when you finally get your server set up the way you want. Having a snapshot of your system right after the initial installation gives you something to revert back to should you want to reconfigure your server without starting from scratch. Linux has many backup utilities but the old standard is still the favorite of admins because of the flexibility offered by its myriad of options.

tar commands can become quite complex. It's easier to enter the command in a text file and make it a shell script. We also need to create a directory to hold the backup file. We'll use a separate directory so we can exclude that directory from the backup (we don't want tar trying to backup a file it's in the process of creating). Enter the following commands:


cd /
mkdir backups
cd backups

Then use the nano editor to create our shell script file with the command:

nano fullserver.sh

and enter the following command into it (and don't miss that period at the end of the command):

tar -cvpf /backups/fullbackup.tar --directory=/ --exclude=proc
--exclude=sys --exclude=dev/pts --exclude=backups .

The above command is on multiple lines for readability. Enter everything on the same line when entering the command in the editor. Once entered, exit the editor (Ctrl-X) saving the file.

The c option creates the backup file.

The v option gives a more verbose output while the command is running. This option can also be safely eliminated.

The p option preserves the file and directory permissions.

The f option needs to go last because it allows you to specify the name and location of the backup file which follows next in the command (in our case this is the /backups/fullbackup.tar file).

The --directory option tells tar to switch to the root of the file system before starting the backup.

We --exclude certain directories from the backup because the contents of the directories are dynamically created by the OS. We also want to exclude the directory that contains are backup file.

Many tar references on the Web will give an exclude example as:

--exclude=/proc

However, this will often result in excluded directories still being backed up because there should not be the leading / (slash) character in front of the directory names. If you were to use the above example, tar would back up the proc directory which would result in errors saying that files had changed since they were read.

In addition to holding your backup file, the /backups directory now holds your script file also. You have to make your script executable before you can run it. To do that and run it enter the following two commands:


chmod 750 /backups/fullserver.sh
./backups/fullserver.sh

To restore your "tar ball" you need to copy it to the top-most point in the file tree that it backed up. In our case the --directory option told tar to start backing up from the root of the file system so that's where we would want to copy the tar ball to. Then simply replace the c option with the x parameter line so:

tar -xvpf /fullbackup.tar

Having a backup file is nice but not of much use if the hard-drive itself fails. If your server is running the wu-ftpd FTP server daemon, you should FTP the tar ball off of your server so that it is stored on a different system.

Note that with the use of the --exclude statements when doing the backup, the tar backup file (tar ball) isn't something you could use to do a "bare metal" restore (because the excluded directories would be missing). However, it does work very well if you do an initial vanilla install of the OS and then un-tar your tar ball on top of that.

Saving Space With Compression

You can compress your backup files to save storage space. Because a lot of Linux files are text files the space savings can be significant. Enough, perhaps, to be able to fit your archive file on a CD or DVD.

To enable the compression you have to add the z switch to both the tar-ing and untar-ing commands. You also have to change the file name to indicate the compression. Our tar-ing command becomes:

tar -zcvpf /backups/fullbackup.tar.gz --directory=/ --exclude=proc
--exclude=sys --exclude=dev/pts --exclude=backups .

Likewise, our untar-ing command becomes:

tar -zxvpf /fullbackup.tar.gz

原文出處 How To Do A Full Backup Using The tar Command
作者: kevinhu    時間: 2016-4-2 21:39
還有這篇也很好,告訴要如何對Linux全系統的備份方法

Full System Backup with tar




歡迎光臨 Hippies (http://bbs.hippies.tw/) Powered by Discuz! X3.2