This is an updated version of my previous backups script – Backup Directories to Amazon S3 Script. The new script works much better and safer. Unlike the old script, the new one creates the tarballs in a temporary file under /tmp
, and allows more control over the backup process.
#! /bin/bash
# Copyright (C) 2008 by Guy Rutenberg
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
PROG=s3backup
DESCRIPTION="Backup directories to Amazon S3"
SYNOPSIS="${PROG} [options] DIR BUCKET"
VERSION=0.2
print_usage () {
echo "Usage: ${SYNOPSIS}"
echo "Save a backup of DIR to Amazon S3 bucket BUCKET"
}
print_help () {
print_usage
cat << EOF
Options:
-h, --help show this help message and exit
--version show program's version number and exit
-f, --full-path use the path to dir in archive instead of only dir
-v, --verbose be verbose about progress
Example:
s3backup ~/Documents guyru-backup/
EOF
}
print_version () {
echo "$PROG $VERSION"
echo "Copyright (C) 2008 Guy Rutenberg <http://www.guyrutenberg.com>"
}
# print only if verbose
vecho () {
if (( "$VERBOSE" >= "$1" )) ; then
echo "$2"
fi
}
VERBOSE=0
TEMP=`getopt -o hvf --long help,version,verbose,full-path -n '${PROG}' -- "$@"`
if [ $? != 0 ] ; then
print_usage
exit 1
fi
eval set -- "$TEMP"
while true ; do
case "$1" in
-h|--help) print_help; exit ;;
--version) print_version; exit ;;
-f|--full-path) FULL_PATH=1; shift ;;
-v|--verbose) let VERBOSE++ ; shift ;;
--) shift; break;;
esac
done
# no directory passed
if [ -z "$1" ]; then
echo "$PROG: missing DIR"
echo ""
print_usage
exit 1
fi
# no bucket passed
if [ -z "$2" ]; then
echo "$PROG: missing BUCKET"
echo ""
print_usage
exit 2
fi
BUCKET=$2
DIR=$(basename "$1")
DIR_PATH=$(dirname "$1")
DATE=$(date +%Y%m%d)
if [ -z "$FULL_PATH" ] ; then
cd "$DIR_PATH"
TAR_PATH="$DIR"
else
TAR_PATH="$1"
fi
vecho 1 "Creating tarball"
TARNAME=`mktemp`
TAR_OPTIONS="-c"
if (( $VERBOSE > 0 )); then
TAR_OPTIONS+="v"
fi
tar $TAR_OPTIONS "$TAR_PATH" | bzip2 -9 -c > "${TARNAME}"
vecho 1 "Sending to Amazon S3"
S3_OPTIONS=""
if (( $VERBOSE > 0 )); then
S3_OPTIONS+="v"
fi
s3cmd put $S3_OPTIONS "$TARNAME" "s3://${BUCKET}$DIR-$DATE.tar.bz2"
vecho 1 "Deleting intermediate files"
rm "$TARNAME"
# vim:sw=4:softtabstop=4:expandtab
The script can also be downloaded from s3backup.gz.
Script usage is pretty straight forward, just pass the folder to backup as the first argument to the script, and an S3 bucket name, to which the backup will be saved as the second. You can also consult s3backup --help
.
Note that you can add a prefix to your backups by adding it to the S3 bucket. For example
s3backup ~/Documents bucketname/prefix/