Script: Automatically Backup a Server to Amazon S3

Posted on October 12 , 2015

Want an easy way to backup your server to Amazon S3?  Simply use this shell script as a cron job (I currently have it set to run every 3 days).  This specific script assumes a YUM-based software repository, MySQL, and Apache.  However, it can easily be changed for different environments.

Update (Dec 9 2015): Now that Amazon S3 supports an “Infrequent Access” class of objects, I’ve updated the script to use it.  Since these backups certainly fit that bill, this can significantly decrease your monthly costs.

The script will backup:

  • The list of packages you have installed
  • A complete MySQL dump
  • All Apache configuration files
  • All web files (assumes /var/www)

Of course, this script assumes you’ve already created a single [S3 ACCOUNT]-vps1-backup bucket.  I’d also recommend setting a lifecycle policy on the bucket, automatically deleting objects after a certain # of days (I use 90).


now=$(date +"%Y-%m-%d")

yum list installed > yum.list
s3cmd put --recursive --preserve --storage-class STANDARD_IA yum.list s3://[S3 ACCOUNT]-vps1-backup/$now/yum.list

mysqldump --all-databases --complete-insert --user='root' --password='[MYSQL PASSWORD]' > backup.sql
s3cmd put --recursive --preserve --storage-class STANDARD_IA backup.sql s3://[S3 ACCOUNT]-vps1-backup/$now/backup.sql

s3cmd put --recursive --preserve --storage-class STANDARD_IA /etc/httpd s3://[S3 ACCOUNT]-vps1-backup/$now/
s3cmd put --recursive --preserve --storage-class STANDARD_IA /etc/my.cnf s3://[S3 ACCOUNT]-vps1-backup/$now/my.cnf

s3cmd put --recursive --preserve /var/www/[DOMAIN NAME] s3://3riverdev-vps1-backup/$now/

Virtual IT Director

Instead of dealing with data backups on your own, consider offloading the stress!  We’d love to discuss our fully managed services.

Leave a Reply

Your email address will not be published. Required fields are marked *