Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Backup-Skript für Duplicity

profile picture
Author
Hetzner Online
Published
2019-03-08
Time to read
4 minutes reading time

Introduction

Das Skript hält die Backups über einen Zeitraum von 2 Monaten vor. Ältere Backups werden gelöscht. Am jeweils ersten des Monats wird neues ein Vollbackup erstellt.

Über die Variable BDIRS können im Skript getrennte Backups für einzelne Verzeichnisse angelegt werden. Diese sollten dann entsprechend von Sicherungen darüberliegender Verzeichnisse ausgenommen werden.

Das Skript kann z.B. unter /usr/local/sbin/backup.sh abgelegt und mittels chmod 755 /usr/local/sbin/backup.sh ausführbar gemacht werden. Vor Benutzung müssen die Zugangsdaten wie Nutzername, Passwort und Hostname entsprechend angepasst werden. Ebenso wie die GPG Passphrase, die zur Verschlüsselung der Backups genutzt wird.

Hinweise

Dieses Skript muß zuerst mit der Option full aufgerufen werden, damit ein erstes Vollbackup erstellt wird.

Ab GPG version 2.1 muß die Option --pinentry-mode loopback hinzugefügt werden, da sonst die Passwortübergabe nicht mehr möglich ist. Siehe auch die duplicity mailing list.

Skript

#!/bin/bash
#
# Simple script for creating backups with Duplicity.
# Full backups are made on the 1st day of each month or with the 'full' option.
# Incremental backups are made on any other days.
#
# USAGE: backup.sh [full]
#

# get day of the month
DATE=`date +%d`

# Set protocol (use scp for sftp and ftp for FTP, see manpage for more)
BPROTO=scp

# set user and hostname of backup account
BUSER='u10000'
BHOST='u10000.your-backup.de'

# Setting the password for the Backup account that the
# backup files will be transferred to.
# for sftp a public key can be used, see:
# https://docs.hetzner.com/robot/storage-box/backup-space-ssh-keys/
#BPASSWORD='yourpass'

# directories to backup (use . for /)
BDIRS="etc home srv ."
TDIR=`hostname -s`
LOGDIR='/var/log/duplicity'

# Setting the pass phrase to encrypt the backup files. Will use symmetrical keys in this case.
PASSPHRASE='yoursecretgpgpassphrase'
export PASSPHRASE

# encryption algorithm for gpg, disable for default (CAST5)
# see available ones via 'gpg --version'
ALGO=AES

##############################

if [ $ALGO ]; then
 GPGOPT="--gpg-options '--cipher-algo $ALGO'"
fi

if [ $BPASSWORD ]; then
 BAC="$BPROTO://$BUSER:$BPASSWORD@$BHOST"
else
 BAC="$BPROTO://$BUSER@$BHOST"
fi

# Check to see if we're at the first of the month.
# If we are on the 1st day of the month, then run
# a full backup. If not, then run an incremental
# backup.

if [ $DATE = 01 ] || [ "$1" = 'full' ]; then
 TYPE='full'
else
 TYPE='incremental'
fi

for DIR in $BDIRS
do
  if [ $DIR = '.' ]; then
    EXCLUDELIST='/usr/local/etc/duplicity-exclude.conf'
  else
    EXCLUDELIST="/usr/local/etc/duplicity-exclude-$DIR.conf"
  fi

  if [ -f $EXCLUDELIST ]; then
    EXCLUDE="--exclude-filelist $EXCLUDELIST"
  else
    EXCLUDE=''
  fi

  # first remove everything older than 2 months
  if [ $DIR = '.' ]; then
   CMD="duplicity remove-older-than 2M -v5 --force $BAC/$TDIR-system >> $LOGDIR/system.log"
  else
   CMD="duplicity remove-older-than 2M -v5 --force $BAC/$TDIR-$DIR >> $LOGDIR/$DIR.log"
  fi
  eval $CMD

  # do a backup
  if [ $DIR = '.' ]; then
    CMD="duplicity $TYPE -v5 $GPGOPT $EXCLUDE / $BAC/$TDIR-system >> $LOGDIR/system.log"
  else
    CMD="duplicity $TYPE -v5 $GPGOPT $EXCLUDE /$DIR $BAC/$TDIR-$DIR >> $LOGDIR/$DIR.log"
  fi
  eval  $CMD

done

# Check the manpage for all available options for Duplicity.
# Unsetting the confidential variables
unset PASSPHRASE
unset FTP_PASSWORD

exit 0

Ausschlüsse

Dateien oder Verzeichnisse, die nicht gesichert werden sollen, werden im Skript per exclude-filelist an duplicity übergeben. Hier kann ein Ausschlußliste für jedes zu sichernde Verzeichnis angelegt werden. Für das Wurzelverzeichnis sind diese in der Datei /usr/local/etc/duplicity-exclude.conf eingetragen, für andere Verzeichnisse unter /usr/local/etc/duplicity-exclude-$DIR.conf (z.B. /usr/local/etc/duplicity-exclude-home.conf). Diese könnte z.B. folgenden Inhalt haben:

  • /dev
  • /proc
  • /sys
  • /tmp
  • /etc
  • /home
  • /srv
  • /var/cache

Automatisieren

Für eine regelmäßig Ausführung wird ein Cronjob benötigt. Dazu kann das Skript entweder unter /etc/cron.daily (täglich), /etc/cron.weekly (wöchentlich) oder /etc/cron.monthly (monatlich) abgelegt werden. Für eine genaue Einstellung des Zeitpunkts kann auch eine Crontab verwendet werden. Hierfür wird eine Datei unter /etc/cron.d/ erstellt:

# /etc/cron.d/duplicity
0 0 * * * root /usr/local/sbin/backup.sh >/dev/null 2>&1
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue

Discover our

Storage Box

Access your storage from everywhere and at any time via PC, smartphone, and tablet.

Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more