Created
June 11, 2015 23:10
-
-
Save Silvenga/76dc9ee36ad476f4b37f to your computer and use it in GitHub Desktop.
backup.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
export PASSPHRASE={{ grains['backup.password'] }} | |
export AWS_ACCESS_KEY_ID={{ grains['backup.aws_id'] }} | |
export AWS_SECRET_ACCESS_KEY={{ grains['backup.aws_key'] }} | |
FOLDER={{ grains['id'] }} | |
TARGET=s3+http://{{ grains['backup.aws_bucket'] }}/$FOLDER | |
SOURCE=/ | |
LOGLOC=/var/log/nightly-backup | |
EXCLUDE=/tmp/excludeList.txt | |
cat > $EXCLUDE <<EOF | |
- /dev | |
- /proc | |
- /home/*/.cache | |
- /home/*/.ccache | |
- /lost+found | |
- /media | |
- /mnt | |
- /run | |
- /tmp | |
- /boot | |
- /selinux | |
- /root/.cache | |
- /root/.ccache | |
- /sys | |
- /var/cache | |
- /var/log | |
- /var/lock | |
- /var/run | |
- /var/tmp | |
EOF | |
function errorHandler () { | |
echo "Error">>$LOGLOC | |
errors=1 | |
} | |
####################################################################### | |
# Start Script | |
####################################################################### | |
echo "Started At: `date`." >>$LOGLOC | |
echo "Starting Backup." >>$LOGLOC | |
echo "" >>$LOGLOC | |
####################################################################### | |
# Clean | |
####################################################################### | |
echo "Cleaning Apt-get">>$LOGLOC | |
apt-get clean>>$LOGLOC | |
[[ "$?" != "0" ]] && errorHandler | |
####################################################################### | |
# Init backup | |
####################################################################### | |
if type mysql >/dev/null 2>&1; then | |
echo "Dumping databases..." >>$LOGLOC | |
USER="root" | |
PASSWORD="{{ grains['mysql.password'] }}" | |
OUTPUTDIR="/var/db-backups" | |
mkdir -p $OUTPUTDIR | |
databases=`mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database` | |
for db in $databases; do | |
echo "Optimizing databse $db">>$LOGLOC | |
mysqlcheck --optimize --user=$USER --password=$PASSWORD --databases $db > /dev/null | |
echo "Dumping databse $db">>$LOGLOC | |
mysqldump --force --opt --user=$USER --password=$PASSWORD --databases $db 1> "$OUTPUTDIR/$db.bak.sql" 2>/dev/null | |
done | |
echo "Done." >>$LOGLOC | |
else | |
echo "Skipping Database dump." >>$LOGLOC | |
fi | |
echo "" >>$LOGLOC | |
####################################################################### | |
# Backup | |
####################################################################### | |
echo "Backing up files..." >>$LOGLOC | |
# --allow-source-mismatch | |
OPTS="--exclude-filelist $EXCLUDE --s3-use-new-style" | |
duplicity --full-if-older-than 5D $OPTS $SOURCE $TARGET >>$LOGLOC | |
[[ "$?" != "0" ]] && errorHandler | |
echo "Done." >>$LOGLOC | |
echo "" >>$LOGLOC | |
####################################################################### | |
# Clean-up | |
####################################################################### | |
echo "Cleaning old backups..." >>$LOGLOC | |
duplicity remove-all-but-n-full 2 --force $TARGET >>$LOGLOC | |
[[ "$?" != "0" ]] && errorHandler | |
echo "Done." >>$LOGLOC | |
echo "" >>$LOGLOC | |
####################################################################### | |
# Finish up | |
####################################################################### | |
unset AWS_ACCESS_KEY_ID | |
unset AWS_SECRET_ACCESS_KEY | |
unset PASSPHRASE | |
# Email log | |
if [ -n "$errors" ] ; then | |
echo "Backup Finished, Emailing Log..." >>$LOGLOC | |
mail -s 'Daily Backup Report' {{ grains['backup.email'] }} <$LOGLOC | |
else | |
echo "Backup Finished, no errors, not emailing log..." >>$LOGLOC | |
fi | |
# Remove old backup log, and backup current log | |
rm $LOGLOC.log | |
mv $LOGLOC $LOGLOC.log | |
# Done! | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment