Skip to content

Instantly share code, notes, and snippets.

@ssimpson89
Last active October 14, 2015 00:28
Show Gist options
  • Save ssimpson89/4280034 to your computer and use it in GitHub Desktop.
Save ssimpson89/4280034 to your computer and use it in GitHub Desktop.
s3backup script
#!/bin/bash
##Notification email address
_EMAIL=email@email.com
#Bucket name
BUCKET=BUCKETNAME
ERRORLOG=/var/log/backuplogs/backup.err`date +%F`
ACTIVITYLOG=/var/log/backuplogs/activity.log`date +%F`
##Directory which needs to be backed up
SOURCE=/backup/cpbackup/daily/*.gz
##Name of the backup in bucket
DESTINATION=`date +%F`
##Backup degree
DEGREE=5
#Clear the logs if the script is executed second time
:> ${ERRORLOG}
:> ${ACTIVITYLOG}
##Uploading the daily backup to Amazon s3
/usr/bin/s3cmd -r put ${SOURCE} s3://${BUCKET}/${DESTINATION}/ 1>>${ACTIVITYLOG} 2>>${ERRORLOG}
ret2=$?
#######################
##Deleting backup?s older than DEGREE days
## Delete from both server and amazon
#######################
for outdated in $(s3cmd ls s3://${BUCKET}/ | sed -e 's/^ *DIR *//g' | rev | cut -d'/' -f2 | rev)
do
if [ $(date -d"${outdated}" +%s) -lt "$(date --date="${DEGREE} days ago" +%s)" ]
then
/usr/bin/s3cmd -r --force del s3://${BUCKET}/${outdated} 1>>${ACTIVITYLOG} 2>>${ERRORLOG}
fi
done
##Sent email alert
msg="BACKUP NOTIFICATION ALERT FROM `hostname`"
if [ $ret2 -eq 0 ];then
msg1="Amazon s3 Backup Uploaded Successfully"
else
msg1="Amazon s3 Backup Failed!!\n Check ${ERRORLOG} for more details"
fi
echo -e "$msg1\n\nCurrent Backups in bucket:\n\n$(s3cmd ls s3://${BUCKET}/ | sed -e 's/^ *DIR *//g' | rev | cut -d'/' -f2 | rev)"|mail -s "$msg" ${_EMAIL}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment