Backup Directories To Amazon S3 Script

This is a small script I wrote today, to automate my backups, which I do on Amazon S3. This is fairly short, yet useful bash script that utilize the s3cmd to do the actual sending of the files.

#! /bin/bash

# 2008 (C) Guy Rutenberg
#This is a script to backup a given folder and send them to Amazon S3

#string trailing slash (if exists)
DIR="${1/%\//}"
TARNAME="${DIR}-$(date +%Y%m%d).tar.bz2"
if [ -e ${TARNAME} ]
then
	echo "Refusing to overwrite existing file - ${TARNAME}"
	exit
fi
echo "Creating tarball"
tar -vc "$1" | bzip2 -9 -c > "${TARNAME}"

echo "Sending to Amazon S3"
s3cmd put -v "${TARNAME}" s3://guyru-backup/

echo "Deleting intermediate files"
rm "${TARNAME}"

To use the script just save it somewhere and make it executable. Remember to substitute guyru-backup with your own bucket name. Notice that you must have s3cmd installed (for Gentoo it’s available under net-misc/s3cmd). s3cmd is one of the best interfaces to the S3 service I’ve seen, so you’ll probably want to install it anyway.

The script usage instructions are pretty simple. Just call the script and pass the path to the folder you want to backup as the first argument.

I intend to use this script to fully automate my backups using cron (maybe this way I’ll remember to the backups regularly).

I hope you will find this script useful. If you have any suggestions or comment please send them.

Leave a Reply

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

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.