Full system backup with tar
This article will show you how to do a full system backup with tar.
Backing up with tar has the advantages of using compression that can help save disk space, and simplicity. The process only requires several steps, they are:
- Boot from a LiveCD
- Change root to the Linux install
- Mount additional (if any) partitions/drives
- Add exclusions
- Use the backup script to backup
To minimize downtime the backup can alternatively be performed on a running system using LVM snapshots, if all filesystems reside on LVM volumes.
Boot with LiveCD
Many Linux bootable CDs, USBs... have the ability to let you change root to your install. While changing root is not necessary to do a backup, it provides the ability to just run the script without need to transfer it to a temporary drive or having to locate it on the filesystem. The Live medium must be of the same architecture that your Linux install currently is (i.e. i686 or x86_64).
Changing root
First you should have a scripting environment set up on your current Linux install. If you do not know what that is, it means that you are able to execute any scripts that you may have as if they are regular programs. If you do not, see this article on how to do that. What you will need to do next is change root, to learn more about what changing root is, read this. When you change root, you do not need to mount any temporary file systems (/proc
, /sys
, and /dev
). These temporary file systems get populated on boot and you actually do not want to backup them up because they can interfere with the normal (and necessary) population process which can change on any upgrade. To change root, you will need to mount your current Linux installs root partition. For example:
# mkdir /mnt/arch # mount /dev/your-partition-or-drive
Use fdisk -l
to discover you partitions and drives. Now chroot:
# cd /mnt/arch # chroot . /bin/bash
arch-chroot
to chroot into the target system - the backup process will fail as it will try to back up temporary file systems, all system memory and other interesting things. Use plain chroot
instead.This example obviously uses bash but you can use other shells if available. Now you will be in your scripted environment (this is provided that you have your ~/.bashrc
sourced on entry):
Mount other partitions
Other partitions that you use (if any) will need to be mounted in their proper places (e.g. if you have a separate partition).
Exclude file
tar has the ability to ignore specified files and directories. The syntax is one definition per line. tar also has the capability to understand regular expressions (regexps). For example:
Backup script
Backing up with is straight-forward process. Here is a basic script that can do it and provides a couple checks. You will need to modify this script to define your backup location, and exclude file (if you have one), and then just run this command after you have chroot
ed and mounted all your partitions. Note that GNU tar with --xattrs
will not preserve extended attributes.
Restoring
To restore from a previous backup, mount all relevant partitions, change the current working directory to the root directory, and execute
$ bsdtar --acls --xattrs -xpf backupfile
replacing backupfile with the backup archive. Removing all files that had been added since the backup was made must be done manually. Recreating the filesystem(s) is an easy way to do this.
Backup with parallel compression
To back up using parallel compression (SMP), use (Parallel bzip2):
# bsdtar -cvf /path/to/chosen/directory/etc-backup.tar.bz2 -I pbzip2 /etc
Store on one or more offline media, such as a USB stick, external hard drive, or CD-R. Occasionally verify the integrity of the backup process by comparing original files and directories with their backups. Possibly maintain a list of hashes of the backed up files to make the comparison quicker.
Restore corrupted files by extracting the file in a temporary working directory, and copying over individual files and directories as needed. To restore the entire directory with all its contents execute the following command as root:
# bsdtar -xvf etc-backup.tar.bz2 -C /