michabbb
11/17/2015 - 1:46 AM

duplicity backup script for docker based duplicity and amazon s3 upload

duplicity backup script for docker based duplicity and amazon s3 upload

#!/bin/bash
AWS_ACCESS_KEY_ID=xxxxxxxx
AWS_SECRET_ACCESS_KEY=xxxxxxxxxx
PASSPHRASE=xxxxxxxxxxx

# directories, space separated
SOURCE="/mnt/home/"
BUCKET=s3+http://xxxxxxxxxx
BUCKETS3=s3://xxxxxxxxxxx
LOGFILE=/mnt/var/log/duplicity.log
LOGFILEREAL=/var/log/duplicity.log
# set email to receive a backup report
TMPOUTPUT=/tmp/output.tmp

DUPLICITY_CMD="docker run --hostname xxxxxxxx --rm -i --name duplicity -v /root/.cache/duplicity/:/root/.cache/duplicity/ -v /:/mnt/ -e AWS_ACCESS_KEY_ID='$AWS_ACCESS_KEY_ID' -e AWS_SECRET_ACCESS_KEY='$AWS_SECRET_ACCESS_KEY' -e PASSPHRASE='$PASSPHRASE' chardek/duplicity"

#echo $DUPLICITY_CMD

backup() {  
  INCLUDE=""
  for CDIR in $SOURCE
  do
    TMP=" --include  ${CDIR}"
    INCLUDE=${INCLUDE}${TMP}
  done
  rm -f $LOGFILEREAL
  # perform an incremental backup to root, include directories, exclude everything else, / as reference.
  BACKUP_CMD="$DUPLICITY_CMD --log-file $LOGFILE --allow-source-mismatch --full-if-older-than 30D $INCLUDE --exclude '**' / $BUCKET > $LOGFILE"
  echo $BACKUP_CMD
  eval $BACKUP_CMD
  if [ -n "$LOGFILE" ]; then
    bytes=`s3cmd du $BUCKETS3|awk {'print $1'}`
    mb=`expr $bytes / 1024 / 1024`
    gb=`expr $bytes / 1024 / 1024 / 1024`
    echo "BUCKET SIZE " >> $LOGFILEREAL
    echo "MB: $mb" >> $LOGFILEREAL
    echo "GB: $gb" >> $LOGFILEREAL
    /root/slack_notify duplicity Backup $LOGFILEREAL
  fi
}

list() {
  eval $DUPLICITY_CMD list-current-files $BUCKET
}

restore() {
  if [ $# = 2 ]; then
    eval $DUPLICITY_CMD restore --file-to-restore $1 $BUCKET $2
  else
    eval $DUPLICITY_CMD restore --file-to-restore $1 --time $2 $BUCKET $3
  fi
}

status() {
  eval $DUPLICITY_CMD collection-status $BUCKET
}

removeold() {
  eval $DUPLICITY_CMD remove-older-than 1M --force $BUCKET
}

if [ "$1" = "backup" ]; then
  backup
elif [ "$1" = "list" ]; then
  list
elif [ "$1" = "removeold" ]; then
  removeold
elif [ "$1" = "restore" ]; then
  if [ $# = 3 ]; then
    restore $2 $3
  else
    restore $2 $3 $4
  fi
elif [ "$1" = "status" ]; then
  status
else
  echo "
  duptools - manage duplicity backup
  
  USAGE:
  
  ./duptools.sh backup 
  ./duptools.sh list  
  ./duptools.sh status
  ./duptools.sh removeold
  ./duptools.sh restore file [time] dest
  "
fi