Skip to content

Instantly share code, notes, and snippets.

@nigelheap
Forked from chrisfree/drush-s3cmd.sh
Last active July 5, 2017 23:44
Show Gist options
  • Save nigelheap/2d4efc765b41805c35ba0bca4614688f to your computer and use it in GitHub Desktop.
Save nigelheap/2d4efc765b41805c35ba0bca4614688f to your computer and use it in GitHub Desktop.
Backup a Drupal site to Amazon S3 using Drush
# arg1 : bucket
# arg2 : project path
# arg3 : backup path
# arg4 : max s3
# Switch to the docroot.
cd $2
# Backup the database.
/usr/bin/drush sql-dump --gzip --result-file=$3/database-`date +%F-%T`.sql
# Switch to the backups directory.
cd $3
# Store the recently created db's filename as a variable.
database=$(ls -t | head -n1)
# Upload to Amazon S3, using s3cmd (https://github.com/s3tools/s3cmd).
s3cmd put $database s3://$1/$database
# Delete databases older than 10 days.
find $3 -mtime +10 -type f -delete
# remove old files from s3 .. if arg4 is set
if [[ $4 != "" ]]
then
# clean up s3 backups
s3cmd ls s3://$1/ | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date -d"-$4" +%s`
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk {'print $4'}`
if [[ $fileName != "" ]]
then
s3cmd del "$fileName"
echo "Deleted $fileName"
fi
fi
done;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment