Verschlüsselte FTP Backups mit duplicity und duply erstellen (Debian Squeeze)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>, Christian Schmalfeld <c [dot] schmalfeld [at] projektfarm [dot] de>
Follow me on Twitter

Mieten Sie heutzutage einen dedizierten Server, so stellen Ihnen die meisten Provider Platz für FTP Backups auf einem Ihrer Backup Systeme zur Verfügung. Dieses Tutorial zeigt Ihnen, wie Sie mit Hilfe von duplicity und duply über FTP verschlüsselte Backups auf dem abseitigen Backup Server Ihres Providers erstellen können (sodass keine kritischen Daten gelesen werden können, selbst wenn Zugriff auf den Backup Server besteht). duply ist ein duplicity Wrapper Skript, welches es uns erlaubt, duplicity ohne Interaktion zu benutzen (das heißt, Sie brauchen kein Passwort einzugeben).

Für die Richtigkeit der Inhalte dieses Tutorials gebe ich keinerlei Garantie.

1 Vorbemerkung

In diesem Tutorial nenne ich den Backupserver backup.example.com, auf dem ich ein FTP Konto mit dem Benutzernamen backupuser und dem Passwort secret habe. Ich möchte eine Sicherheitskopie des Verzeichnisses /home/exampleuser auf meinem Server machen.

2 Installation von duplicity

Stellen Sie als erster sicher, dass Ihr System aktuell ist:

apt-get update

apt-get upgrade

Installieren Sie duplicity dann folgendermaßen:

apt-get install duplicity ncftp

3 Das erste Backup

Erstellen Sie nun das erste Backup:

FTP_PASSWORD=secret duplicity /home/exampleuser ftp://backupuser@backup.example.com/

root@server1:/home/exampleuser# FTP_PASSWORD=secret
duplicity /home/exampleuser ftp://backupuser@backup.example.com/
NcFTP version is 3.2.4
GnuPG passphrase:
Local and Remote metadata are synchronized, no sync needed.
Last full backup date: none
No signatures found, switching to full backup.
Retype passphrase to confirm:
————–[ Backup Statistics ]————–
StartTime 1341339005.14 (Tue Jul 3 20:10:05 2012)
EndTime 1341339006.46 (Tue Jul 3 20:10:06 2012)
ElapsedTime 1.32 (1.32 seconds)
SourceFiles 53
SourceFileSize 13494139 (12.9 MB)
NewFiles 53
NewFileSize 13494139 (12.9 MB)
DeletedFiles 0
ChangedFiles 0
ChangedFileSize 0 (0 bytes)
ChangedDeltaSize 0 (0 bytes)
DeltaEntries 53
RawDeltaSize 13461371 (12.8 MB)
TotalDestinationSizeChange 4687992 (4.47 MB)
Errors 0

————————————————-

root@server1:/home/exampleuser#

Wie Sie sehen, werden Sie nach einem GnuPG Passwort gefragt. Geben Sie hier irgendein Passwort ein; dies muss jedes Mal getan werden, wenn Sie duplicity starten. Das Backup wird mit Hilfe von GnuPG verschlüsselt. Zugriffs- und Benutzerrechte bleiben im Backup erhalten.

Um das Backup in einem Unterverzeichnis des Backupservers zu erstellen, würden Sie den Befehl folgendermaßen ändern :

FTP_PASSWORD=secret duplicity /home/exampleuser ftp://backupuser@backup.example.com/subdirectory

Duplicity wird beim ersten Ausführen ein komplettes Backup erstellen und bei jedem weiteren ein inkrementelles Backup. Um das Erstellen eines kompletten Backups zu erzwingen können Sie die full Option benutzen:

FTP_PASSWORD=secret duplicity full /home/exampleuser ftp://backupuser@backup.example.com/

Um ein Verzeichnis vom Backup auszuschließen, z.B. /home/exampleuser/tmp, können Sie die –exclude Option benutzen:

FTP_PASSWORD=secret duplicity –exclude /home/exampleuser/tmp /home/exampleuser ftp://backupuser@backup.example.com/

Erstellen Sie eine Sicherheitskopie des gesamten root Verzeichnisses /, so vergessen Sie nicht –exclude /proc hinzuzufügen, denn andernfalls stürzt Duplicity wahrscheinlich ab.

Um mehr über die verfügbaren Duplicity Optionen zu erfahren, benutzen Sie den man Befehl:

man duplicity

4 Ein Backup wiederherstellen

Nehmen wir nun an, Sie hätten alles in /home/exampleuser gelöscht und möchten es von Ihrem FTP Backup wiederherstellen. So wird es gemacht:

FTP_PASSWORD=secret duplicity ftp://backupuser@backup.example.com/ /home/exampleuser

Beachten Sie bitte, dass der abseitige Server diesmal vor dem lokalen Verzeichnis steht!

5 Automatische Backups mit duply

Da Duplicity bei jeder Benutzung nach einem GnuPG Passwort fragt, ist es schwer, es für automatische Backups zu verwenden (z.B. mit Cron). Glücklicherweise gibt es jedoch duply, ein Wrapper Skript welches es uns erlaubt, Duplicity ohne Passwortabfrage aufzurufen.

Generieren Sie als erstes einen GnuPG Schlüssel, den duply benutzen soll (sodass Sie kein Passwort mehr eingeben müssen):

gpg –gen-key

server1:/home/exampleuser# gpg –gen-key
gpg (GnuPG) 1.4.10; Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Please select what kind of key you want:
(1) DSA and Elgamal (default)
(2) DSA (sign only)
(5) RSA (sign only)
Your selection? <– ENTER
DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) <– ENTER
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n>  = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0)
<– ENTER
Key does not expire at all
Is this correct? (y/N)
<– y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
„Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>“
Real name:
<– Ihr Name, z.B. Falko Timme
Email address: <– Ihre E-Mail Adresse, z.B. ft@falkotimme.com
Comment:
You selected this USER-ID:
„Falko Timme <ft@falkotimme.com>“

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? <– O
You need a Passphrase to protect your secret key. <– Geben Sie hier Ihr gewünschtes Passwort ein (und danach noch einmal zur Bestätigung)

Der Schlüssel wird nun generiert. Sie können hier eine zweite Konsole öffnen und einige Buchstaben eintippen, sodass der Zufallszahlengenerator genug Entropie erhält:

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
….+++++
+++++
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
….+++++
+++++
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key
7C6E958B marked as ultimately trusted
public and secret key created and signed.
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   1024D/7C6E958B 2007-12-10
Key fingerprint = 1FDC 60FB 8A27 90D8 553C  3C3E 8E1F 66F7 7C6E 958B
uid                  Falko Timme <ft@falkotimme.com>
sub   2048g/F1BB98F4 2007-12-10
server1:/home/exampleuser#

Ich habe die Schlüssel ID (7C6e958B) hervorgehoben, da Sie diese gleich brauchen werden. Laden Sie als nächstes duply herunter:

cd /tmp
wget http://downloads.sourceforge.net/project/ftplicity/duply%20%28simple%20duplicity%29/1.5.x/duply_1.5.7.tgz
tar xvfz duply_1.5.7.tgz
cp duply_1.5.7/duply /usr/local/bin

Um duply benutzen zu können brauchen Sie mindestens ein Profil. Da ich in diesem Tutorial eine Sicherheitskopie von /home/exampleuser erstellen möchte, nenne ich das Profil exampleuser. Sie können das Profil folgendermaßen erstellen:

duply exampleuser create

Dies erstellt das Verzeichnis /root/.duply/exampleuser mit der duply Konfigurationsdatei conf darin. Öffnen Sie diese Datei:

vi /root/.duply/exampleuser/conf

Die wichtigsten Einstellungen in der Datei sind:

  • GPG_KEY: die ID Ihres GnuPG Schlüssels;
  • GPG_PW: das Passwort, das Sie bei der Erstellung des GnuPG Schlüssels eingegeben haben;
  • TARGET: der Backupserver (inkl. des FTP Benutzernamens);
  • TARGET_PASS: das FTP Passwort auf dem Backupserver;
  • SOURCE: das Quellverzeichnis (also das Verzeichnis, von dem Sie ein Backup anlegen möchten);
  • MAX_AGE: das Alter des ältesten Backups; ältere Backups werden gelöscht;
  • VERBOSITY: die Menge der Informationen, die von duply auf dem Bildschirm angezeigt wird;
  • TEMP_DIR: ein Verzeichnis für temporäre Dateien; wenn Sie ein Backup wiederherstellen muss hier mindestens genug Platz für die größte Datei des Backups sein.

Meine Datei sieht folgendermaßen aus:

# gpg encryption settings, simple settings:
# GPG_KEY='disabled' - disables encryption alltogether
# GPG_KEY='<key1>[,<key2>]'; GPG_PW='pass' - encrypt with keys, sign
# with key1 if secret key available and use GPG_PW for sign & decrypt
# GPG_PW='passphrase' - symmetric encryption using passphrase only
GPG_KEY='7C6E958B'
GPG_PW='gpg_key_password'
# gpg encryption settings in detail (extended settings)
# the above settings translate to the following more specific settings
# GPG_KEYS_ENC='<keyid1>,[<keyid2>,...]' - list of pubkeys to encrypt to
# GPG_KEY_SIGN='<keyid1>|disabled' - a secret key for signing
# GPG_PW='<passphrase>' - needed for signing, decryption and symmetric
# encryption. If you want to deliver different passphrases for e.g.
# several keys or symmetric encryption plus key signing you can use
# gpg-agent. Add '--use-agent' to the duplicity parameters below.
# also see "A NOTE ON SYMMETRIC ENCRYPTION AND SIGNING" in duplicity manpage
# notes on en/decryption
# private key and passphrase will only be needed for decryption or signing.
# decryption happens on restore and incrementals (compare archdir contents).
# for security reasons it makes sense to separate the signing key from the
# encryption keys. https://answers.launchpad.net/duplicity/+question/107216
#GPG_KEYS_ENC='<pubkey1>,<pubkey2>,...'
#GPG_KEY_SIGN='<prvkey>'
# set if signing key passphrase differs from encryption (key) passphrase
# NOTE: available since duplicity 0.6.14, translates to SIGN_PASSPHRASE
#GPG_PW_SIGN='<signpass>'

# gpg options passed from duplicity to gpg process (default='')
# e.g. "--trust-model pgp|classic|direct|always"
# or "--compress-algo=bzip2 --bzip2-compress-level=9"
# or "--personal-cipher-preferences AES256,AES192,AES..."
#GPG_OPTS=''

# disable preliminary tests with the following setting
#GPG_TEST='disabled'

# credentials & server address of the backup target (URL-Format)
# syntax is
# scheme://[user:password@]host[:port]/[/]path
# probably one out of
# file://[/absolute_]path
# ftp[s]://user[:password]@other.host[:port]/some_dir
# hsi://user[:password]@other.host/some_dir
# cf+http://container_name
# imap[s]://user[:password]@host.com[/from_address_prefix]
# rsync://user[:password]@other.host[:port]::/module/some_dir
# # rsync over ssh (only keyauth)
# rsync://user@other.host[:port]/relative_path
# rsync://user@other.host[:port]//absolute_path
# # for the s3 user/password are AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY
# s3://[user:password]@host/bucket_name[/prefix]
# s3+http://[user:password]@bucket_name[/prefix]
# # scp and sftp are aliases for the ssh backend
# ssh://user[:password]@other.host[:port]/some_dir
# tahoe://alias/directory
# webdav[s]://user[:password]@other.host/some_dir
# ATTENTION: characters other than A-Za-z0-9.-_.~ in user,password,path have
# to be replaced by their url encoded pendants, see
# http://en.wikipedia.org/wiki/Url_encoding
# if you define the credentials as TARGET_USER, TARGET_PASS below
# duply will url_encode them for you
#TARGET='scheme://user[:password]@host[:port]/[/]path'
TARGET='ftp://backupuser@backup.example.com/'
# optionally the username/password can be defined as extra variables
# setting them here _and_ in TARGET results in an error
#TARGET_USER='_backend_username_'
TARGET_PASS='secret'

# base directory to backup
SOURCE='/home/exampleuser'

# exclude folders containing exclusion file (since duplicity 0.5.14)
# Uncomment the following two lines to enable this setting.
#FILENAME='.duplicity-ignore'
#DUPL_PARAMS="$DUPL_PARAMS --exclude-if-present '$FILENAME'"

# Time frame for old backups to keep, Used for the "purge" command.
# see duplicity man page, chapter TIME_FORMATS)
MAX_AGE=1M

# Number of full backups to keep. Used for the "purge-full" command.
# See duplicity man page, action "remove-all-but-n-full".
#MAX_FULL_BACKUPS=1

# activates duplicity --full-if-older-than option (since duplicity v0.4.4.RC3)
# forces a full backup if last full backup reaches a specified age, for the
# format of MAX_FULLBKP_AGE see duplicity man page, chapter TIME_FORMATS
# Uncomment the following two lines to enable this setting.
#MAX_FULLBKP_AGE=1M
#DUPL_PARAMS="$DUPL_PARAMS --full-if-older-than $MAX_FULLBKP_AGE "

# sets duplicity --volsize option (available since v0.4.3.RC7)
# set the size of backup chunks to VOLSIZE MB instead of the default 25MB.
# VOLSIZE must be number of MB's to set the volume size to.
# Uncomment the following two lines to enable this setting.
#VOLSIZE=50
#DUPL_PARAMS="$DUPL_PARAMS --volsize $VOLSIZE "

# verbosity of output (error 0, warning 1-2, notice 3-4, info 5-8, debug 9)
# default is 4, if not set
VERBOSITY=4

# temporary file space. at least the size of the biggest file in backup
# for a successful restoration process. (default is '/tmp', if not set)
TEMP_DIR=/tmp

# Modifies archive-dir option (since 0.6.0) Defines a folder that holds
# unencrypted meta data of the backup, enabling new incrementals without the
# need to decrypt backend metadata first. If empty or deleted somehow, the
# private key and it's password are needed.
# NOTE: This is confidential data. Put it somewhere safe. It can grow quite
# big over time so you might want to put it not in the home dir.
# default '~/.cache/duplicity/duply_<profile>/'
# if set '${ARCH_DIR}/<profile>'
#ARCH_DIR=/some/space/safe/.duply-cache

# DEPRECATED setting
# sets duplicity --time-separator option (since v0.4.4.RC2) to allow users
# to change the time separator from ':' to another character that will work
# on their system. HINT: For Windows SMB shares, use --time-separator='_'.
# NOTE: '-' is not valid as it conflicts with date separator.
# ATTENTION: only use this with duplicity < 0.5.10, since then default file
# naming is compatible and this option is pending depreciation
#DUPL_PARAMS="$DUPL_PARAMS --time-separator _ "

# DEPRECATED setting
# activates duplicity --short-filenames option, when uploading to a file
# system that can't have filenames longer than 30 characters (e.g. Mac OS 8)
# or have problems with ':' as part of the filename (e.g. Microsoft Windows)
# ATTENTION: only use this with duplicity < 0.5.10, later versions default file
# naming is compatible and this option is pending depreciation
#DUPL_PARAMS="$DUPL_PARAMS --short-filenames "

# more duplicity command line options can be added in the following way
# don't forget to leave a separating space char at the end
#DUPL_PARAMS="$DUPL_PARAMS --put_your_options_here "

Das könnte dich auch interessieren …