Backup mit duply

Seit geraumer Zeit sichere ich meine Linuxserver am liebsten mit der Software duply. Der Name duply ist die Abkürzung von “simple duplicity”. Wie der Name schon verrät, wurde diese Software entwickelt, um das komplexe aber gleichzeitig auch leistungsfähige Backupframework duplicity einfach bedienbar zu machen. Und dies ist meiner Meinung nach auch hervorragend gelungen.

Backup mit duply

In diesem Artikel blogge ich die einzelnen Schritte, mit denen ich ein duply auf einem Debian 8 oder Ubuntu 16.04 einrichte.

Als erstes muss duply und ein brauchbares FTP-Programm für die Datenübertragung installiert werden.

apt-get install duply
apt-get install lftp

Danach muss ein zentrales Verzeichnis für die Konfiguration unter /etc angelegt und danach eine Backup-Konfiguration erstellt werden. Es ist wichtig, dass das Verzeichnis als erstes angelegt wird, da duply die Konfiguration sonst in das home-Verzeichnis und nicht unter /etc ablegt. Als Namen für die Konfiguration wähle ich immer den vollen Hostnamen. Dies ist zumeist eindeutig und läßt sich im Nachhinein leichter Skripten.

mkdir /etc/duply
duply hostname.domain.de create

Danach erzeugt man sich einen GPG-Schlüssel.

gpg --gen-key

Sofern man einen bereits existierenden GPG-Schlüssel verwenden möchte, kann man natürlich auch Schlüssel in GPG importieren. Dabei sollte man dann aber nicht vergessen, neben dem öffentlichen auch den privaten Schlüssel und die Ownertrusts zu importieren. Ich habe die beiden Schlüssel und die Ownertrusts für diese Zwecke immer in drei Dateien mit den Namen backup-key.asc, backup-secret-key.asc und backup-ownertrusts.asc. Diese brauche ich dann nur übermitteln und importieren.

gpg --import backup-key.asc
gpg --import backup-secret-key.asc
gpg --import-ownertrust backup-ownertrusts.asc

Sobald man den Schlüssel angelegt oder importier hat, läßt man sich die Public-Key-ID des GPG-Schlüssels anzeigen. Diese muss man nämlich später in die Konfiguration von duply eintragen, damit die Backups mit GPG verschlüsselt werden. Dies führt zu einer zusätzlichen Datensicherheit, die gerade bei FTP-Servern im Internet zu empfehlen ist.

gpg --list-keys

Danach passt man die Konfiguration von duply entsprechend an.

cd /etc/duply/hostname.domain.de/
vi conf

Die Datei sollte ungefähr wie folgt aussehen. Dabei muss die Schlüssel-ID und die FTP-VErbindung natürlich auf die eigene Umgebung angepasst werden.

# gpg encryption settings, simple settings:
#  GPG_KEY='disabled' - disables encryption alltogether
#  GPG_KEY='[,]'; GPG_PW='pass' - encrypt with keys,
#   sign if secret key of key1 is available use GPG_PW for sign & decrypt
#  Note: you can specify keys via all methods described in gpg manpage,
#        section "How to specify a user ID", escape commas (,) via backslash (\)
#        e.g. 'Mueller, Horst', 'Bernd' -> 'Mueller\, Horst, Bernd'
#        as they are used to separate the entries
#  GPG_PW='passphrase' - symmetric encryption using passphrase only
GPG_KEY='1234567'
GPG_PW='Hier kommt das Passwort fuer den eigenen key hin'
# gpg encryption settings in detail (extended settings)
#  the above settings translate to the following more specific settings
#  GPG_KEYS_ENC='[,,...]' - list of pubkeys to encrypt to
#  GPG_KEY_SIGN='|disabled' - a secret key for signing
#  GPG_PW='' - 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. Simply make sure that GPG_AGENT_INFO is set in environment.
#   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=',,...'
#GPG_KEY_SIGN=''
# set if signing key passphrase differs from encryption (key) passphrase
# NOTE: available since duplicity 0.6.14, translates to SIGN_PASSPHRASE
#GPG_PW_SIGN=''

# uncomment and set a file path or name force duply to use this gpg executable
# available in duplicity 0.7.04 and above (currently unreleased 06/2015)
#GPG='/usr/local/gpg-2.1/bin/gpg'

# 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..."
#   or "--homedir ~/.duply" - keep keyring and gpg settings duply specific
#   or "--pinentry-mode loopback" - needed for GPG 2.1+ _and_
#      also enable allow-loopback-pinentry in your .gnupg/gpg-agent.conf
#GPG_OPTS=''

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

# backend, credentials & location of the backup target (URL-Format)
# generic syntax is
#   scheme://[user[:password]@]host[:port]/[/]path
# eg.
#   sftp://bob:secret@backupserver.com//home/bob/dupbkp
# for details and available backends see duplicity manpage, section URL Format
#   http://duplicity.nongnu.org/duplicity.1.html#sect7
# NOTE:
#   some backends (eg. cloudfiles) need additional env vars to be set to
#   work properly, when in doubt consult the man page mentioned above.
# ATTENTION:
#   characters other than A-Za-z0-9.-_.~ in the URL 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 try to url_encode them for you if the need arises.
#TARGET='scheme://user[:password]@host[:port]/[/]path'
TARGET='ftp://user:passwort@ftphost.domain.de/'
# 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='_backend_password_'
# alternatively you might export the auth env vars for your backend here
# when in doubt consult (if existing) the NOTE section of your backend on
#  http://duplicity.nongnu.org/duplicity.1.html for details
# eg. for cloud files backend it might look like this (uncomment for use!)
#export CLOUDFILES_USERNAME='someuser'
#export CLOUDFILES_APIKEY='somekey'
#export CLOUDFILES_AUTHURL ='someurl'

# base directory to backup
SOURCE='/'

# a command that runs duplicity e.g.
#  shape bandwidth use via trickle
#  "trickle -s -u 640 -d 5120" # 5Mb up, 40Mb down"
#DUPL_PRECMD=""

# override the used python interpreter, defaults to "python2"
#   e.g. "python" or "/usr/bin/python2.7"
#PYTHON="python2"

# 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=3M

# 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=3

# Number of full backups for which incrementals will be kept for.
# Used for the "purge-incr" command.
# See duplicity man page, action "remove-all-inc-of-but-n-full".
#MAX_FULLS_WITH_INCRS=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=5

# 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_/'
# if set  '${ARCH_DIR}/'
#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 "

Bei Bedarf, kann man dann noch Verzeichnisse vom Backup ausschließen.

vi exclude

Anzeige der bereits gelaufenen Backups:

duply $(hostname -f) status

Starten eines Full-Backups:

duply $(hostname -f) full

Löschen veralteter Backups:

duply $(hostname -f) purge-full --force

Einrichtung einer täglichen, inkrementellen Sicherung mit anschließendem löschen der veralteten Backups:
Um duply jede Nacht ein Backup machen zu lassen, muss ein Cron-Job angelegt werden. Dazu legt man am besten die Datei /etc/cron.d/duply mit dem folgenden Befehlt an.

vi /etc/cron.d/duply

Wenn man jede Nacht um 2 Uhr eine Sicherung machen möchte, sollte in dieser Datei die folgende Zeile eingetragen werden.

0 2 * * * root /usr/sbin/duply.sh >/dev/null 2>&1

Danach muss die Datei /usr/sbin/duply.sh angelegt werden, die vom Cron-Job aufgerufen wird.

vi /usr/sbin/duply.sh

In dieser sollte dann das folgende stehen.

#!/bin/sh
#
# Script created on 04-10-2015 KPU
#
# This script was created to make Duplicity backups.
#
# 05.01.2016 K.Purrucker
# Ergaenzung: Es wird immer der Hostname als Konfigname verwendet
#

duply $(hostname -f) backup >>/var/log/duply.log
duply $(hostname -f) purge-full --force >>/var/log/duply.log

exit 0

Danach muss noch cron neu gestartet, die Datei ausführbar gemacht und das Log-File angelegt werden.

chmod a+x /usr/sbin/duply.sh
service cron restart
touch /var/log/duply.log

Beispiel: Rücksicherung einer 7 Tage alten Version der Datei etc/fstab in das Verzeichnis /root/:

duply $(hostname -f) fetch etc/fstab /root/fstab 7D

Beitrag veröffentlicht

in

von

Schlagwörter:

Kommentare

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

WordPress Cookie Hinweis von Real Cookie Banner