Category Archives: Raspberry Pi

Arch linux backup with hdup

The hdup tool is a simple backup/restore utility. It uses the standard tar tool to save files into a gzipped tar archive. In addition it is possible to automatically encrypt the archives and send the archives to a remote host for safe storage.

Preliminary setup

We will build some packages by running the makepkg command. As the makepkg cannot be run as root we will need to create an ordinary user account first. This can be done with the useradd command. The user should also be added to the sudoers file with the visudo command.

$ useradd -mp <password> <username>
$ visudo
##
## User privilege specification
##
root ALL=(ALL) ALL
<username> ALL=(ALL) ALL

We will also need a set of build tools. The tools can be installed with:

 # pacman -S –needed base-devel

Building mcrypt

First we need to build the mcrypt package because hdup depends on it and it is not available in the pacman repositories as a binary package. Let’s fetch the package description (PKGBUILD) from the Arch User Repository (AUR).

$ cd ~/builds
$ curl -L -O https://aur.archlinux.org/packages/mc/mcrypt/mcrypt.tar.gz
$ tar xzf mcrypt.tar.gz

To build mcrypt first edit the PKGBUILD file in the mcrypt folder and change the target architecture to arch=(‘armv6h’) and then run makepkg.

$ cd mcrypt
$ makepkg -s

Finally install the package.

$ sudo pacman -U mcrypt-2.6.8-1-armv6h.pkg.tar.xz

Building hdup

The hdup PKGBUILD can also be downloaded from AUR.

$ cd ~/builds
$ curl -L -O https://aur.archlinux.org/packages/hd/hdup/hdup.tar.gz
$ tar xzf hdup.tar.gz

Building can be done with the makepkg command. Just edit the PKGBUILD file and change arch=(‘armv6h’).

$ cd hdup
$ makepkg -s

Install with pacman:

$ sudo pacman -U hdup-2.0.14-4-armv6h.pkg.tar.xz

Configuring hdup

First let’s create a new user account. This account will be used as the owner for the backup files and for transferring backups to a remote host via ssh (same account should be available also on the remote host).

# user=eb
# groupadd -g 200 $user
# useradd -m -u 200 -g $user -c "Backup Operator" -s /bin/bash $user
# passwd $user

For ssh login we need to create a public/private key pair. This should be done with the newly created account (‘eb’ in my case).

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/eb/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/eb/.ssh/id_rsa.
Your public key has been saved in /home/eb/.ssh/id_rsa.pub.

My remote backup host is called eb-linux. I’ll authorize ssh login to the remote host by copying my public key to the $HOME/.ssh/authorized_keys on the remote host.

$ ssh-copy-id eb-linux
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
eb@eb-linux's password:Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'eb-linux'"

and check to make sure that only the key(s) you wanted were added.

Now we are ready to create the hdup configuration file /etc/hdup/hdup.conf. The bare minimum settings are shown in the table below. The [global] section contains the backup directory (archive dir) and the backup user account (user). It is also possible to give ssh options here with the ‘proto option’ key. I will define the identity (private key) to be used to login to the remote host. The ‘allow remote’ key is needed to enable backup transmission from other hosts. I have only one host section [arch-linux] that has one key ‘dir’ specifying a list of directories to backup. On the remote host the same host section must be present but it may be empty.

Based on Arch Linux Wiki page guidance we should at least have regular backups of the configuration files (/etc/*) and the local pacman database (usually /var/lib/pacman/local/*).

Local hdup.conf Remote hdup.conf
[global]
archive dir = /home/eb/backups
user = eb
proto option = -i /home/eb/.ssh/id_rsa
allow remote = yes
[arch-linux]
dir = /etc/,/var/lib/pacman/local/
[global]
archive dir = /home/eb/backups
user = eb
allow remote = yes
[arch-linux]

Testing

Ok, let’s first make a full backup to our local backup folder. The full backup is selected with the parameter ‘monthly’. The second parameter selects the host section. It is also possible to create incremental backups with ‘weekly’ (since last monthly) and ‘daily’ (since last weekly).

# hdup monthly arch-linux
hdup: Running without -P and probably a non patched tar.
hdup: This will lead to incomplete backups.
hdup: arch-linux: STARTING BACKUP.
/usr/bin/tar: /home/eb/backups/arch-linux/etc/filelist: file name read contains nul character
/usr/bin/tar: Removing leading `/' from member names
Hdup version.: 2.0.14
Host.........: arch-linux
Date.........: 11-04-2015
Scheme.......: monthly
Archive......: arch-linux.11-04-2015.monthly.tar.gz
Encryption...: no
Bytes written: 5.6M
Elapsed......: 0:00:13
Status.......: successfully performed backup

The backup file can be found from /home/eb/backups/<host>/<date>.

# ls -l /home/eb/backups/arch-linux/11-04-2015/
total 5728
-rw-r----- 1 backup backup 5864736 Apr 11 18:31 arch-linux.11-04-2015.monthly.tar.gz

Next let’s try the same backup but this time saving the backup to the remote host.

# hdup monthly arch-linux @eb@eb-linux
hdup: Running without -P and probably a non patched tar.
hdup: This will lead to incomplete backups.
hdup: arch-linux: STARTING BACKUP.
/usr/bin/tar: /home/eb/backups/arch-linux/etc/filelist: file name read contains nul character
/usr/bin/tar: Removing leading `/' from member names
/usr/sbin/hdup (remote) : arch-linux: STARTING REMOTE BACKUP. 
Hdup version.:  2.0.14
Host.........:  arch-linux
Date.........:  11-04-2015
Scheme.......:  monthly
Archive......:  arch-linux.11-04-2015.monthly.tar.gz
Encryption...:  no
Bytes written:  5.6M
Elapsed......:  0:00:14
Status.......:  successfully performed backup

Seems to work just fine. Later on we can automate the backup process by creating cron jobs for the monthly, weekly and daily backups.

As a final test let’s try to restore the newly created full backup from the remote host.

First let’s create a restore directory on the local host.

 $ mkdir /home/eb/restores

Then let’s login to the remote host and run the restore command.

# hdup restore arch-linux 11-04-2015 /home/eb/restores @eb@arch-linux
hdup: arch-linux: STARTING RESTORE.
eb@arch-linux's password:
/usr/sbin/hdup (remote) : arch-linux: STARTING REMOTE RESTORE.
/usr/sbin/hdup (remote) : WARNING: No lockfile found!
Hdup version.:  2.0.14
Host.........:  arch-linux
Date.........:  11-04-2015
Scheme.......:  restore
Archives.....:  arch-linux.11-04-2015.monthly.tar.gz
Status.......:  successfully restored backup
Elapsed......:  0:00:10

Then in the local host we can run diff to make sure we got everything back.

# cd /home/eb/restores
# diff -qr /var/lib/pacman/local var/lib/pacman/local