Sunday, November 11, 2012

Small Script to Backup Using s3cmd

After many disappointments of using EC2 to perform regular backups of my important files on my NAS, I have decided to go back to S3 backup tools. It works better then I remembered (I guess two years can make a difference), so I wrote a little script to backup my data on a weekly basis. Basically it uses s3cmd to perform an rsync-like backup (well it's not really rsync, but it's good enough for my purposes). S3 is much cheaper especially because you pay for what you use (unlike EBS volumes) and outbound data transfer costs nothing. Here it goes:


#!/bin/bash

SCRIPTDIR=`dirname $0`

# The '--dry-run' will make sure the script won't really do 
# anything. Comment it only after carefully reviewing
# the output
DEBUG_SWITCHES="--dry-run"

# The following switches will use "reduced redundancy" backup
# (cheaper) and will delete files from backup when removed 
# locally. The files.exclude contains file patterns to exclude
# from backup
COMMON_SWITCHES="--rr --delete-removed --rexclude-from=$SCRIPTDIR/files.exclude $DEBUG_SWITCHES"

echo "Running $0"echo Backup started at `date` 

# The following will cause printing of the command before 
# executing it. Useful for logging
set -o xtrace 

# s3cmd command for each dir I need to backup.
# Need to replace "bucket" with real bucket name
s3cmd sync $COMMON_SWITCHES $DEBUG_SWITCHES /mnt/vg1/lv0/users/ s3://bucket/users/

s3cmd sync $COMMON_SWITCHES /mnt/vg1/lv0/media/Audio/ s3://bucket/media/Audio/

s3cmd sync $COMMON_SWITCHES /mnt/vg1/lv0/media/Music/ s3://bucket/media/Music/

s3cmd sync $COMMON_SWITCHES /mnt/vg1/lv0/media/Pictures/ s3://bucket/media/Pictures/
set +o xtrace 

echo Backup finished at `date`



Here is an example for my files.exclude. It should reside in the same dir as the script above:

# Exclude files start with "._" and .DS_Store (for Mac OSX)
(^|/)\._[^/]*$
(^|/)\.DS_Store$



No comments:

Post a Comment