#!/bin/bash # Title: Incremental Back-up script # Description: Create back-ups of dirs and dbs by copying them to Perfacilis' back-up servers # We strongly recommend to put this in /etc/cron.hourly # Version: 0.5 # Usage: bash /etc/cron.hourly/backup readonly BACKUP_LOCAL_DIR=/ext/local-backup readonly BACKUP_DIRS=(/etc /home /ext $BACKUP_LOCAL_DIR) readonly RSYNC_PROFILE="/mnt/nas-backup" readonly RSYNC_DEFAULTS="-trlqz4 --delete --delete-excluded --prune-empty-dirs" readonly RSYNC_EXCLUDE=(pvc-ab1fbe1d-fb06-4bfb-a4bd-b73c664d7a9f_nzbget_store-data-ssd/ temp/ tmp/ .cache/ log/ logs/ *.log) readonly INCREMENTS=14 log() { MSG=`echo $1` logger -p local0.notice -t `basename $0` -- $MSG # Interactive shell if tty -s; then echo $MSG fi } prepare_local_dir() { if [ ! -d $BACKUP_LOCAL_DIR ]; then mkdir -p $BACKUP_LOCAL_DIR touch -d "January 1 1990" $BACKUP_LOCAL_DIR fi } prepare_remote_dir() { local RSYNC_OPTS=$(get_rsync_opts) local EMPTYDIR=$(mktemp -d) rsync ${RSYNC_OPTS//--delete* /} $EMPTYDIR/ $RSYNC_PROFILE/current rm -rf $EMPTYDIR } get_next_increment() { local LAST NEXT if [ -f $BACKUP_LOCAL_DIR/last ]; then LAST=$(cat $BACKUP_LOCAL_DIR/last | tr -d "\n") fi if [ -z "$LAST" ]; then echo 0 return fi NEXT=$(($LAST+1)) if [ "$NEXT" -gt "$INCREMENTS" ]; then echo 0 return fi echo $NEXT } get_rsync_opts() { local EXCLUDE=`dirname $0`/rsync.exclude if [ ! -f $EXCLUDE ]; then printf '%s\n' "${RSYNC_EXCLUDE[@]}" > $EXCLUDE fi echo "$RSYNC_DEFAULTS --exclude-from=$EXCLUDE" } backup_packagelist() { log "Back-up list of installed packages" dpkg --get-selections > $BACKUP_LOCAL_DIR/packagelist.txt } backup_folders() { local RSYNC_OPTS=$(get_rsync_opts) local DIR TARGET RSYNC local INC=$(get_next_increment) local VANISHED='^(file has vanished: |rsync warning: some files vanished before they could be transferred)' log "Moving back-up to target: ${INC/#0/current}" for DIR in ${BACKUP_DIRS[@]}; do TARGET=${DIR/#\//} TARGET=${TARGET//\//_} RSYNC="rsync $RSYNC_OPTS" if [ "$INC" -gt 0 ]; then RSYNC="rsync $RSYNC_OPTS --backup --backup-dir=/$INC/$TARGET" fi log "- $DIR" $RSYNC $DIR/ $RSYNC_PROFILE/current/$TARGET 2>&1 | (egrep -v "$VANISHED" || true) done } signoff_increment() { echo $(get_next_increment) > $BACKUP_LOCAL_DIR/last } cleanup() { rm -f `dirname $0`/rsync.exclude rm -f `dirname $0`/rsync.secret } main() { log "Backup initiated at `date`" trap "cleanup" EXIT prepare_local_dir prepare_remote_dir backup_packagelist backup_folders signoff_increment log "Backup completed at `date`" } main