Full system backup with rsync

From ArchWiki
Revision as of 11:02, 22 July 2012 by DSpider (Talk | contribs) (Using a script: highlighted "/mnt or /media")

Jump to: navigation, search

This rsync script allows creating a full backup copy across filesystems. It is setup so that the copy includes intact booting capabilities, optionally excluding selected files.

The approach has benefits over omitting system files by just copying personal data; if the system becomes corrupted in the main partition, overcoming the problem means booting into the backup as opposed to identifying and reinstalling affected programs.

Instructions were converted from this forum post.

With a single command

This operation can be done while the system is running. Since it's going to take a while, you may freely browse the web during this time. Worst case scenario you won't get the same opened tabs when you restore the backup (or boot from it) because they weren't saved. Not a big deal.

As root, run:

Note: 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.
# rsync -av /* /path/to/backup/folder --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs}

For more information on why these folders were excluded, read the next section.

Using a script

The script is very simple. Just like the above method, it rsyncs 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 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, you should read the man page for rsync and date. Hint: "$1" will act as a substitute later for whatever backup destination you want.

Save it as backup.sh and make it executable:

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.
$ nano ~/backup.sh
#!/bin/sh

START=$(date +%s)
rsync -av /* $1 --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs}
FINISH=$(date +%s)
echo "total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds"

touch $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, and /home/*/.gvfs should be added too, so that it won't complain at the end that "some files/attrs were not transferred". 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. 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 a terminal and run (as root):

# ~/backup.sh /path/to/backup/folder
Tip: You can also replace the "$1" instances from the script with the actual path. Then you could just run ~/backup.sh (as root).

Boot requirements

If you transferred the system to a different partition or drive and you want to boot it, the process is as simple as updating the backup's /etc/fstab and your bootloader's configuration file (e.g. /boot/grub/menu.lst). But if you plan on replacing the current install (or replacing the drive) and you know that you will no longer have a bootloader, you will also have to (re)install one.

Here's how you can find out the device name or UUID of a certain partition:

# fdisk -l
# blkid

Update the fstab

While the system is running, edit the backup's fstab to reflect the changes:

# nano /path/to/backup/etc/fstab
tmpfs        /tmp          tmpfs     nodev,nosuid,noexec      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

Because rsync has performed a recursive copy of the entire root filesystem, all of the sda mountpoints are problematic and booting the backup will fail. In this example, all of the offending entries are replaced with a single one:

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

/dev/sdb1    /             ext4      defaults                 0   1

Remember to use the proper device name and filesystem type.

Update the bootloader's configuration file

This section assumes that you backed up the system to another drive or partition, that your current bootloader is working fine, and that you want to boot from the backup as well.

Tip: Instead of updating the configuration file, you can also temporarily edit the menu during boot. When the menu shows up, press e (for GRUB) or the Tab key (Syslinux) and change the relevant entries. To boot, press b (GRUB) or Enter (Syslinux).
Note: You should take this opportunity to consult their wiki pages to learn which ones start counting drives/partitions from zero and which ones do not. For example, GRUB Legacy sees (hd0,0) as the first partition of the first drive. Syslinux will see it as h0 1.

All you need to do is duplicate the current entry, except pointing to a different drive or partition.

For GRUB Legacy:

# nano /boot/grub/menu.lst

For Syslinux:

# nano /boot/syslinux/syslinux.cfg

For GRUB2, it's recommended that you automatically re-generate a .cfg:

# grub-mkconfig -o /boot/grub/grub.cfg

(Re)install the bootloader

This section assumes that the system was backed up on a machine that no longer has a bootloader installed, or a machine that only has Windows on it (e.g. Windows has taken over the MBR after reinstalling it).

Warning: Don't forget to set the boot flag on /dev/sda1 (or whatever partition the boot files are located) using either gparted or cfdisk, depending on whichever the LiveCD comes with. Else you may get a "Missing operating system. FATAL: INT18: BOOT FAILURE" message.

So, using a "live" Linux distribution and from a chroot environment (this is important), run:

Note: Use /dev/sdx, not /dev/sdx1; you want to install to the MBR.

GRUB Legacy:

# grub-install /dev/sda

GRUB2:

# grub-install /dev/sda
# grub-mkconfig -o /boot/grub/grub.cfg

Syslinux:

# extlinux --install /boot/syslinux
# dd bs=440 conv=notrunc count=1 if=/usr/lib/syslinux/mbr.bin of=/dev/sda