Skip to content

Instantly share code, notes, and snippets.

@isislovecruft
Created December 19, 2011 20:38
Show Gist options
  • Save isislovecruft/1498765 to your computer and use it in GitHub Desktop.
Save isislovecruft/1498765 to your computer and use it in GitHub Desktop.
Bash script to automate encrypted Duplicity backups, runs a check on available bandwidth first.
#!/bin/bash
#
# autobackup.sh v.0.1.0
# Written by Isis Lovecruft, isis@patternsinthevoid.net
#
# Backup script to automate SSH, SCP, SFTP, FTP, and IMAP backups through
# Duplicity. Duplicity encrypts backup file through GPG before sending
# files through protocol. This script runs a check on the available
# bandwidth, and only runs the backup when the bandwidth available is
# above a configurable password.
SPEED=$(curl -w %{speed_download} -o /dev/null -s http://speedtest.sea01.softlayer.com/speedtest/speedtest/random1000x1000.jpg)
INT=${SPEED/\.*}
KBPS=$(echo $[INT / 1024])
THRESHOLD="200"
# Uncomment and set the following in order to not type your password.
# This is incredibly insecure, as your password is then stored
# plaintext.
#export PASSPHRASE='passphrase'
# The $SPEED variable downloads a nice image of snow (into /dev/null so that
# it isn't actually saved anywhere on disk). It also gives us a write-out
# (-w %{speed_download}) for the average available bandwidth (incoming) in
# bytes per second.
#
# $INT turns the float $SPEED into an integer.
#
# $KBPS, as I'm sure you can surmise, turns the bytes per second into
# kilobits per second.
#
# $THRESHOLD can be changed to fit the user's preferences, and it defines
# the minimum bandwidth which should be available for a duplicity backup to
# take place.
if [[ "$#" == "0" ]]; then
echo ""
echo "Usage: ./backup.sh <BACKUP_TO_LOCATION_1> <BACKUP_TO_LOCATION_2> ... <BACKUP_TO_LOCATION_N>"
echo ""
echo "Backup locations can be locally stored on the same disk (not recommended), "
echo "or may be stored remotely. For remote backups, duplicity provides several "
echo "options for transport, including SCP, FTP, and IMAP, please see 'man duplicity' "
echo "for more information. Also, all duplicity backups are automatically GnuPG "
echo "encrypted, so transportation is much safer than it would be otherwise. "
echo ""
echo "This script will first create backups as root (which is why it prompts for "
echo "a password), and then it backs up only your home directory (in case the home "
echo "directory is encrypted) to the BACKUP_TO_LOCATION-<your user name>."
echo ""
exit 1
fi
# Make sure this script is run as root.
if [[ `id -u` != 0 ]]; then
echo ""
echo "Sorry, backups must be made as root in order for files in the / directory to"
echo "be backed up safely. Please do 'sudo su' and try running this script again."
echo "Exiting..."
echo ""
exit 1
fi
# Set up BACKUP_TO_LOCATION positional parameters.
for i in "$@"; do
$(BACK_UP_LOCATION_$i)=$i
echo ""
echo "Would you like to make a full backup, or add an incremental backup to the "
echo "last full backup stored?"
select fullinc in "Full" "Incremental"; do
case $fullinc in
Full)
echo "Please confirm that this is where you wish to store your full backup: ("$BACKUP_TO_LOCATION_$i")?"
select yn in "Yes" "No"; do
case $yn in
Yes)
if [[ $KBPS -gt $THRESHOLD ]]; then
duplicity full -vN --ssh-askpass --exclude /proc --exclude /mnt --exclude /media --exclude /tmp / $BACKUP_TO_LOCATION_$i
fi
break;;
No)
break;;
esac
done
break;;
Incremental)
echo "Please confirm that this is where you wish to store your incremental backup: ("$BACKUP_TO_LOCATION_$i")?"
select yn in "Yes" "No"; do
case $yn in
Yes)
if [[ $KBPS -gt $THRESHOLD ]]; then
duplicity incremental -vN --ssh-askpass --exclude /proc --exclude /mnt --exclude /media --exclude /tmp / $BACKUP_TO_OCATION_$i
fi
break;;
No)
break;;
esac
done
esac
done
done
unset PASSPHRASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment