System backup

From ArchWiki
Jump to navigation Jump to 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.


Two files are needed: the backup script and a file stating which files to include/exclude from the backup source.

Backup script

The script is very simple; 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 include/exclude list.

Save it as Template:Filename and make it executable: Template:File

Backup source; Template:Codeline
In this case it's performing a backup on the whole root.
Backup destination; Template:Codeline1
Passed as an argument to the script; e.g. Template:Filename
Include/exclude list; Template:Codeline
This example uses Template:Filename.

Include/exclude list

As deciding which files should populate this list can be difficult, here's a typical backup example that excludes common files that don't need to be backed up, such as the vast majority of Template:Filename. Note that specifying every desired file or directory in Template:Codeline is not needed; this section only acts as a filter for statements in Template:Codeline. This file is in the traditional include/exclude rsync format.

Save the following as Template:Filename: Template:File

Content in system directories; Template:Filename, Template:Filename, Template:Filename and Template:Filename are excluded because they are created by the system at runtime, while the directories themselves need to be preserved since they are not regenerated at boot. Lastly, all Template:Filename instances are skipped since they are partition-specific. For Archlinux Template:Filename 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. These files can be regenerated with Template:Filename.
Even though Template:Filename is excluded, 4 files that are not dynamically created by udev need to be preserved. These are Template:Filename, Template:Filename, Template:Filename and Template:Filename.

Backing up

Substitute Template:Filename as appropriate, and mount the destination device:

# mount /dev/sdb1 /media/backup
Tip: if the ability to boot the backup isn't important, omit the previous step and simply backup to an arbitrary directory.

Run the backup script (note that the trailing "Template:Filename" character is necessary):

# ./ /media/backup/

Boot setup

After the sync is finished, the backup destination's Template:Filename has to be modified, a boot loader needs to be installed on the backup destination, and configuration in the destination's Template:Filename requires to reflect the new location.

Modify fstab

Edit the backup destination's fstab: Template:Command

Because rsync has performed a recursive copy of the entire root filesystem, all of the Template:Codeline mounpoints are problematic and will cause the backup boot to fail. In this example, all of the offending entries are replaced with a single one: Template:Command

As before, remember to use the proper device name and filesystem type.

Install bootloader

While these instructions assume GRUB is being employed, they could easily be adapted to other bootloaders, such as LILO.

Open the GRUB console:

# grub

Direct the install towards the destination device:

root (hd1,0)
setup (hd1)
root; Template:Codeline
This should point to where the GRUB files are located--in this case, "Template:Codeline" means the second storage device (Template:Filename) and "Template:Codeline" is the first partition (Template:Filename).
setup; Template:Codeline
The command specifies where the actual boot loader is to be installed. In this example it is installed to the MBR of the second storage device.

Configure bootloader

The problem here is that even though the boot loader installs correctly, its menu entries are for the main system's partitions, not the backup system's.

It's possible to fix this by creating a custom Template:Filename for the backup destination. In order to do this, modify Template:Filename so that it copies a custom Template:Filename: Template:File

Tip: instead of replacing Template:Filename with a custom version solely for the backup, add a new GRUB entry pointing to the backup device or simply edit GRUB's menu during boot time.