Difference between revisions of "Full system backup with rsync (正體中文)"

From ArchWiki
Jump to: navigation, search
m (Fengchao moved page 使用 rsync 進行系統完整備份 (正體中文) to Full System Backup with rsync (正體中文) without leaving a redirect: Create lokalized name. See Help:i18n.)
(只要執行一行指令)
Line 14: Line 14:
 
如果你想要知道為什麼這些目錄被排除在外,你可以閱讀下一段的資訊。
 
如果你想要知道為什麼這些目錄被排除在外,你可以閱讀下一段的資訊。
  
{{Note|如果你是重度使用 '''hardlinks''' 的使用者,you might consider using additionally '''{{ic|-H}}''' {{ic|rsync}}'s option, which by default is turned off as memory expensive during rsync run, but nowadays it should be no problem on most of modern machines. There are a lot of hard links below the /usr folder which save disk space.}}
+
{{Note|如果你是重度使用 '''hardlinks''' 的使用者,你也許會想要使用 {{ic|rsync}} '''{{ic|-H}}''' 這個參數,在 /usr 的目錄中有許多的 hard links 當你使用這個參數後,將可以節省較多的備份空間。}}
  
{{Note|If you plan on backing up your system somewhere other than {{ic|/mnt}} or {{ic|/media}}, don't forget to add it to the list, to avoid an infinite loop.  Also, if there are any bind mounts in the system they should be excluded as well, as not to copy the bind mounted contents twice.  The example below is a good place to start and excludes all the necessary directories that are typically common to all users of Arch Linux.  Your system may have additional areas which you may also want to exclude.  Use the {{ic|mount}} command to list system mounts for additional insight on what to exclude.}}
+
{{Note|如果你計劃將系統備份到 {{ic|/mnt}} {{ic|/media}} 以外的地方,別忘了把他加入 --exclude 的清單中,以免造成無窮迴圈。}}
  
{{Note|You may want to add rsync's '''{{ic|--delete}}''' option if you are running this multiple times to the same backup folder}}
+
{{Note|你也許會想要使用 rsync '''{{ic|--delete}}''' 的參數,如果你需要經常的備份在同一個目錄中的話,這個參數可以幫助你刪除多餘的項目。}}
  
 
== 使用 script ==
 
== 使用 script ==

Revision as of 15:00, 4 February 2014

這篇文章是教你使用 rsync 來備份你的 "/" , 並且排除部份目錄。 這個方法比使用 dd 備份磁碟 (disk cloning) 更好。我們可以使用這個方法備份不同的磁區大小以及不同的檔案系統,而且這個方法也比使用 cp -a 來得更好,因為有更好的的檔案權限控管,以及保留檔案屬性還有 Access Control Lists (ACLs). [1]

只要執行一行指令

使用 root 權限, 執行:

# rsync -aAXv /* /path/to/backup/folder --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}

如果你想要知道為什麼這些目錄被排除在外,你可以閱讀下一段的資訊。

Note: 如果你是重度使用 hardlinks 的使用者,你也許會想要使用 rsync -H 這個參數,在 /usr 的目錄中有許多的 hard links 當你使用這個參數後,將可以節省較多的備份空間。
Note: 如果你計劃將系統備份到 /mnt/media 以外的地方,別忘了把他加入 --exclude 的清單中,以免造成無窮迴圈。
Note: 你也許會想要使用 rsync --delete 的參數,如果你需要經常的備份在同一個目錄中的話,這個參數可以幫助你刪除多餘的項目。

使用 script

Same as in the above method, the system files are transferred in archive mode, ensuring that symbolic links, devices, permissions and ownerships, among other file attributes are preserved, while excluding files that match the patterns from the --exclude string. On top of that, it shows at the end how much time it took, and it also writes a blank file stating when the backup was created. To learn more about what this script does, read man rsync and man date.

Note: Again, if you plan on backing up your system somewhere other than /mnt or /media, don't forget to add it to the list, to avoid an infinite loop.
Note: You may want to add rsync's --delete option if you are running this multiple times to the same backup folder
$ cd ~/Scripts
$ nano backup.sh
#!/bin/sh

if [ $# -lt 1 ]; then 
    echo "No destination defined. Usage: $0 destination" >&2
    exit 1
elif [ $# -gt 1 ]; then
    echo "Too many arguments. Usage: $0 destination" >&2
    exit 1
elif [ ! -d "$1" ]; then
   echo "Invalid path: $1" >&2
   exit 1
elif [ ! -w "$1" ]; then
   echo "Directory not writable: $1" >&2
   exit 1
fi

case "$1" in
  "/mnt") ;;
  "/mnt/"*) ;;
  "/media") ;;
  "/media/"*) ;;
  *) echo "Destination not allowed." >&2 
     exit 1 
     ;;
esac

START=$(date +%s)
rsync -aAXv /* $1 --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/var/lib/pacman/sync/*}
FINISH=$(date +%s)
echo "total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds" | tee $1/"Backup from $(date '+%A, %d %B %Y, %T')"
$ chmod +x backup.sh
Note: The contents of /dev, /proc, /sys, /tmp, /run were excluded because they are populated at boot (while the folders themselves are not created), /lost+found is filesystem-specific. For Arch Linux, /var/lib/pacman/sync/* can also be excluded. This can save a lot of time on every backup since the directory contains many small files that tend to change quite often. These are description files for every package from the repositories and can be re-generated with pacman -Syu.Also /var/log/journal/* may be skipped as it contains large number of systemd logs. Additionally, you may also want to skip /home/*/.thumbnails/*, /home/*/.mozilla/firefox/*.default/Cache/* and /home/*/.cache/chromium/*.

Backing up is easy.

While the system is running, open up a terminal and run (as root):

# /home/user/Scripts/backup.sh /some/destination

(replace user with username since you created the directory as user in the user's home directory)

You can also replace both $1 instances from the script with the actual destination path, move it to one of the folders from echo $PATH, and then simply run (as root):

# backup.sh

修改開機需要的相關檔案

在備份有開機磁區的的檔案系統時,常常會因為設定錯誤而導致系統無法正常開機,如果你因為要將系統備份到另外一個磁區或者磁碟,而且你也需要讓它能夠開機,你可以透過修改 /etc/fstab 以及更新你的 bootloader 的設定檔,來解決這個問題。

更新 fstab

在重開機之前你必須先修改備份後的系統的 fstab 來讓它能夠讀取到變更後的磁區:

# nano /path/to/backup/etc/fstab
tmpfs        /tmp          tmpfs     nodev,nosuid             0   0

/dev/sda1    /boot         ext2      defaults                 0   2
/dev/sda5    none          swap      defaults                 0   0
/dev/sda6    /             ext4      defaults                 0   1
/dev/sda7    /home         ext4      defaults                 0   2

因為 rsync 將整個磁碟都備份過來,所以所有原本的 sda 掛載點,在重開機後會因為找不到開機檔而出錯,所以我們必須將掛載點更改成新的裝置,像是把 /boot 掛載點的 sda 更改成 sdb:

# nano /path/to/backup/etc/fstab
tmpfs        /tmp          tmpfs     nodev,nosuid             0   0

/dev/sdb1    /             ext4      defaults                 0   1

注意裝置名稱與檔案系統的類別,不要設錯了。

更新 bootloader 的設定檔

這一段主要是告訴你如何從你備份的磁區、磁碟開機。

如果你使用的是 Syslinux,你只需要將原本的開機磁區指定到新的磁區上即可:

Tip: 在修改 syslinux.cfg 前,你也可以暫時性的修改開機選單,只要你在開機選單出現時按下 Tab 鍵,你就可以暫時的修改裏面的資訊,以便測試備份的磁區是否真的可用。
# nano /boot/syslinux/syslinux.cfg

如果你連開機磁區都想換到新的磁區上,你可以下這個指令:

# syslinux-install_update -i -a -m -c /mnt/backup
Note:
  • -i (安裝檔案)
  • -a (將磁區標記為開機磁區)
  • -m (安裝 MBR boot code)
  • -c (Chroot install (ex: -c /mnt))

如果你使用的是 GRUB,建議你使用指令自動產生 grub.cfg 設定檔:

# pacman -S os-prober
# grub-mkconfig -o /boot/grub/grub.cfg

當然你也可以檢查設定檔是否正確,檔案在 /boot/grub/grub.cfg。確認 UUID 是否是新的磁區,不然他仍舊會使用舊的磁區來開機。

第一次開機

重開你的電腦,並且在 bootloader 選單中選擇正確的項目,於是將會第一次載入你的系統。系統將會重新檢查你的你的 / 並且產出其他相對應的檔案。

現在你可以重新編輯 /etc/fstab 來去增加之前被你移除掉的磁區和掛載點。

如果你將資料從 HDD 轉換到 SSD (固態硬碟),別忘了啟動 TRIM。也別忘了使用 HDD 和 tmpfs 掛載點來降低 SSD 損害。- 可參考 Relocate files to tmpfsTips for Minimizing SSD Read & Writes.

Note: 你可能需要再次重開機來讓服務可以被正常的運作。 如果你的 pulseaudio沒有辦法被正常的載入。 可以嘗試 restart dbus.service 來讓它正常。

延伸閱讀

  1. Howto – local and remote snapshot backup using rsync with hard links Includes file deduplication with hard-links, MD5 integrity signature, 'chattr' protection, filter rules, disk quota, retention policy with exponential distribution (backups rotation while saving more recent backups than older)