Skip to content

Instantly share code, notes, and snippets.

@floreo
Last active January 7, 2018 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floreo/6afd0b3ccdd725135bb92d6ead8e591f to your computer and use it in GitHub Desktop.
Save floreo/6afd0b3ccdd725135bb92d6ead8e591f to your computer and use it in GitHub Desktop.
Server backup script using Duplicity and GnuPG
#!/usr/bin/env bash
_FTP_PASSWORD=""
_FTP_USER=""
_FTP_HOST=""
_FTP_DIRECTORY=""
_GPG_KEY=""
_BACKUP_OUTPUT="$( mktemp )"
_BACKUP_EMAIL=""
_BACKUP_TYPE="incremental"
if [ "$( date +%u )" -eq 1 ] ; then
_BACKUP_TYPE="full"
# Cleaning UP old backups // I use autofs to delete old backups, fixme: add FTP connection to delete old backups
find /mnt/autofs/ftp/${_FTP_DIRECTORY}/ -type f -mtime +15 | xargs rm
duplicity collection-status file:///mnt/autofs/ftp/${_FTP_DIRECTORY}/ | grep -Eo 'duplicity-.+[^,]' | tr ']' ' ' | tr ',' '\n' | xargs rm
fi
FTP_PASSWORD="${_FTP_PASSWORD}" duplicity ${_BACKUP_TYPE} --encrypt-key "${_GPG_KEY}" \
--exclude=/proc \
--exclude=/sys \
--exclude=/mnt \
--exclude=/tmp \
--exclude=/dev \
--exclude=/var/cache \
--exclude=/var/tmp \
/ ftp://${_FTP_USER}@${_FTP_HOST}/${_FTP_DIRECTORY} 1>${_BACKUP_OUTPUT} 2>&1
if [ -s "${_BACKUP_OUTPUT}" ] ; then
if [ -n "${_BACKUP_EMAIL}" ] ; then
mail -s "Backup on $(cat /etc/hostname)" -a"From: xxxx@xxx.fr" "${_BACKUP_EMAIL}" <${_BACKUP_OUTPUT}
rm -f -- ${_BACKUP_OUTPUT}
fi
else
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment