Encrypted cloud backups with Duplicity

Data Safe

Article from Issue 156/2013
Author(s):

If you're looking for a secure and portable backup technique, try combining the trusty command-line utility Duplicity with an available cloud account.

The Duplicity command-line tool is a popular option for storing backups in insecure environments, because it offers encryption by default. Encrypted backups can fall into the hands of third parties without risk. Many users configure Duplicity to back up to a local file server, but you can also send your files to an FTP server, an SSH server, or even a cloud-based Amazon (S3) or Ubuntu (U1) system. Backing up to the cloud offers the protection of a remote location, and it makes the archive available worldwide. In this article, I describe how to use Duplicity to back up files to the cloud.

About Duplicity

When launched, Duplicity first generates a full backup; later backups store incremental changes, called deltas, in what Duplicity refers to as volumes. Space-saving hard links then refer to the files on the full backup.

Thus, in principle, you could just create one full backup and then use incremental backups for the changes. The developers of Duplicity warn customers, however: Not only can a mistake in one incremental part ruin the entire backup [1], but restoring files takes quite a long time if the software needs to run through all the incremental backups.

If you prefer a GUI-based approach, Déjà Dup provides a graphical interface for Duplicity that is very easy to use and covers many essential functions. It is part of Gnome and is available in distributions such as Ubuntu and Fedora, but it cannot be used on systems without a graphical interface. Other command-line tools, such as Duply, Backupninja, and Dupinanny, also aim to simplify the handling of Duplicity  [2].

Turnkey

If you do not have your own GPG key, or if you want to create one for your backups, you can do so by running

gpg --gen-key

(Figure 1). In the commentary, you can store the purpose of the key, if desired; you will need the passphrase later when creating and decrypting encrypted backups. Additionally, you can sign your archives using

Figure 1: A key for the backup can be quickly and easily produced using gpg --gen-key.
gpg --sign-key <key-id>

to create a digital signature.

The passphrase protects the key in case it falls into the wrong hands. To avoid losing access to your data, you need to keep it in a safe place. External media, such as USB sticks, can be useful places to save the public and secret key.

The gpg -k and gpg -K commands display the matching public key. Typing

$ gpg --output /media/<user>/<USB-Stick>/backup key_pub.gpg --armor --export <key-ID>
$ gpg --output /media/<user>/<USB-Stick>/backupkey_sec.gpg --armor --export-secret-keys <key-ID>

exports both keys. Typing

gpg --import

installs the master keys on another system.

Four Goals

The script in Listing 1 creates encrypted backups of home directories for four different targets: a local directory, an SSH server (via the Paramiko back end), Ubuntu One, and Amazon's S3 (Figure 2).

Figure 2: If you create a bucket in Amazon's S3 cloud storage, you can store encrypted backups with Duplicity.

Listing 1

backup.sh

01 #!/bin/bash
02 # Passphrase for GPG key
03 export PASSPHRASE=<Passphrase>
04
05 # Key ID of the GPG key
06 export GPG_KEY=<key-ID>
07
08 # Amazon access credentials
09 export AWS_ACCESS_KEY_ID=<Key-ID>
10 export AWS_SECRET_ACCESS_KEY=<secret key>
11
12 # Backup source
13 SOURCE=/home
14
15 # Backup targets
16 # Local backup
17 LOCAL_BACKUP=file:///home/user/backup
18 # Backup via SSH
19 SSH_BACKUP=sftp://user@remote/backup
20 # Backup to Ubuntu One
21 UBUNTU_ONE=u1+http://<target_folder>
22 # Amazon S3
23 AMAZON_S3=s3+http://<Bucket-Name>
24
25 # Duplicity commands
26 # Local
27 duplicity --encrypt-key ${GPG_KEY} ${SOURCE} ${LOCAL_BACKUP}
28 # SSH
29 export FTP_PASSWORD=<SSH-Password>
30 duplicity --encrypt-key ${GPG_KEY} ${SOURCE} ${SSH_BACKUP}
31 unset FTP_PASSWORD
32 # Ubuntu One
33 duplicity --encrypt-key ${GPG_KEY} ${SOURCE} ${UBUNTU_ONE}
34 # Amazon S3
35 duplicity --encrypt-key ${GPG_KEY} --s3-use-new-style --s3-european-buckets ${SOURCE} ${AMAZON_S3}
36
37 # Reset Passwords
38 unset AWS_ACCESS_KEY_ID
39 unset AWS_SECRET_ACCESS_KEY
40 unset PASSPHRASE

In this case, you need to keep several points in mind. Duplicity expects absolute paths with local backups; however, if the backup destination is an SSH server, the hostname is followed by a relative path to the home directory of the logged-in user  – in Listing 1, the backup thus ends up in /home/user/backup.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News