Backing up partitions over network with FSArchiver

Use case in a nutshell: One your host have a partition to backup, there's the second host under Linux with enought free space, you want to do backup to this remote Linux host.

OK, let's start with a warning. Backup and restore might be dangerous operations that potentially can cause loose of data. There's a lot of things that can go wrong, especially while streaming over network. Also it's necessary to be very careful executing these operations in so manual way as described below. There should be some paid services that reduce risks of loosing data. I use described approach only because I have enough space for backups on my desktop, do redundant backups, don't want to use paid services (just because I can do it by my own) and don't backup any business related data. Thankfully I have never lost my data for about 7 years of using backup/restore tools.

In the previous article I described Making backups of ext4 partitions with FSArchiver. In this one I'm introducing Network File System (NFS) to make it possible to do remote backups. The whole process can be divided to 3 steps:

  1. Set up NFS server.
  2. Set up NFS client.
  3. Back up/Restore with FSArchiver.

You will need:

  1. Linux host with enough space to store backups.
  2. Linux Live CD run on host whose partition need to be backed up.
  3. Understand of what you are doing.

The scripts below were tested with Debian Wheezy and Ubuntu 12.04 and need to be modified if for non-Debian like Linux distributions that doesn't have apt command.

Setting up NFS Server

NFS server should be installed and configured on Linux machine that you want to use for actual storage of backups. Configuration is quite primitive and insecure, so be sure that you trust the network you use. Here is example bash script to install NFS server. You need to specify the location where you want backups to be stored and limit access by IP or network address (in example below only hosts from network can access NFS server).

BACKUP_DIR="/backups"       # Change me if necessary
CLIENTS=""   # Change me

EXPORTS_RECORD="$BACKUP_DIR $CLIENTS(rw,sync,no_subtree_check)"
sudo apt-get install nfs-kernel-server nfs-common
[ ! -d $BACKUP_DIR ] && sudo mkdir -p $BACKUP_DIR
sudo chmod 777 $BACKUP_DIR
sudo chown nobody:nogroup $BACKUP_DIR
if [ "`tail -n1 /etc/exports`" != "$EXPORTS_RECORD" ]; then
    sudo sh -c "echo \"$EXPORTS_RECORD\" >> /etc/exports"
sudo exportfs -a
sudo service nfs-kernel-server restart

Unfortunately it's often necessary to reboot host after execution of these commands, so the host really need to be Linux host, you would not be able to use some live CD.

You also might consider removing nfs-kernel-server package if you don't need NFS server after creation backups.

Setting up NFS client

NFS client should be installed and configured on machine that have partitions to be backed up. FSArchiver is a Linux tool and should be run in Linux. Although it doesn't mean that you need Linux to be installed, you can use live CDs like Debian live-CD or Ubuntu live-CD (examples below were tested with these two). Here is the example bash script to install NFS client. You need to specify NFS server IP address and the same directory you specified while setting up server as remote directory.

SERVER_IP=      # Change me
REMOTE_DIR="/backups"       # Change me if necessary

FSTAB_RECORD="$SERVER_IP:$REMOTE_DIR $LOCAL_DIR nfs rw,async,nodev,nosuid,user 0 0"
sudo apt-get update
sudo apt-get install nfs-common
[ ! -d $LOCAL_DIR ] && sudo mkdir -p $LOCAL_DIR
if [ "`tail -n1 /etc/fstab`" != "$FSTAB_RECORD" ]; then
    sudo sh -c "echo $FSTAB_RECORD >> /etc/fstab"
sudo mount $LOCAL_DIR
df -h

Making backup

Finally it's necessary to install fsarchiver (in Ubuntu live CD it might be necessary to enable universe repository first)

sudo apt-get install nfs-common fsarchiver

and run it for backing up, for instance:

sudo fsarchiver -j4 -z5 -L"Ubuntu 12.04. (/dev/sda1)" -s2037 savefs \
/mnt/nfs/backups/ubuntu20131218.fsa /dev/sda1

Here /dev/sda1 partition will be backed up in 4 threads with compression rate equal to 5 (max is 9), it will have the specified label, splitted to files of 2037MB, with the first part named as /mnt/nfs/backups/ubuntu20131218.fsa. Output for this command:

Archive will be split into volumes of 2135949312 bytes (1.99 GB)
Statistics for filesystem 0
* files successfully processed:....regfiles=87985, directories=13675, symlinks=53530, hardlinks=0, specials=82
* files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0

While error counters are 0 you are OK.

But how have I determined that I need to backup /dev/sda1 partition? I used df -h command to determine partition by it's size and used/available space.

Also consider creating checksum to ensure that backup wasn't corrupted after creation.

Restore backup

Before restore (and/or after backing up) you might want to get some information about created backup archive. To get it run in terminal fsarchiver with command archinfo as shown below (with output)

fsarchiver archinfo /mnt/nfs/backups/ubuntu20131218.fsa 
====================== archive information ======================
Archive type:           filesystems
Filesystems count:      1
Archive id:             52b17c45
Archive file format:        FsArCh_002
Archive created with:       0.6.12
Archive creation date:      2013-12-19_04-13-31
Archive label:          Ubuntu 12.04. (/dev/sda1)
Minimum fsarchiver version:
Compression level:      5 (bzip2 level 2)
Encryption algorithm:       none

===================== filesystem information ====================
Filesystem id in archive:   0
Filesystem format:      ext4
Filesystem label:       
Filesystem uuid:        64c0f523-21f8-41fd-be4c-dd2950fa0204
Original device:        /dev/sda1
Original filesystem size:   10.70 GB (11490541568 bytes)
Space used in filesystem:   2.41 GB (2592837632 bytes)

From the output you need to determine filesystem id in archive. If you created backup with only one partition inside (as shown below) then ID should always be 0, but it never hurts to check.

Then determine the partition to where you want this backup be restored with df -h command as described in the end of previous section. All data at this partition will be overwritten by backup.

To restore backup run fsarchiver with restfs command as shown below (followed with output):

sudo fsarchiver restfs /mnt/nfs/backups/ubuntu20131218.fsa id=0,dest=/dev/sda1
Statistics for filesystem 0
* files successfully processed:....regfiles=87985, directories=13675, symlinks=53530, hardlinks=0, specials=82
* files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
Tagged as : Linux