Wednesday, February 15, 2012

Script Backups to Amazon s3

Recently I had to create a shell script to place rman backups on Amazon S3 storage. Here is how I did it.

Dependencies: On the host unix box you need to install the s3 toolkit and the pem file.

To install the s3cmd toolkit on linux.
As root cd /etc/yum.repos.d
wget http://s3tools.org/repo/RHEL_5/s3tools.repo
yum install s3cmd
s3cmd –configure
Access key and Secret key are your identifiers for Amazon S3


For more information on s3cmd set, check out http://s3tools.org/s3cmd

#!/bin/sh
export DATE_STRING=`date +%Y%m%d`
export DATE_STRING_OLD=`date -d last-month +%Y%m%d`
export file1="/myfiledir/*_$DATE_STRING"

echo 'deleting old backup files in the S3 bucket'
array=(`s3cmd ls s3://myBackupFolder/ | awk ' ($4 ~ /'$DATE_STRING_OLD'/){ print $4 }'` )
len=${#array[*]}
i=0
while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
s3cmd del ${array[$i]}
let i++
done

echo 'copying backup files into the S3 bucket'
array=(`ls $file1`)
len=${#array[*]}
i=0
while [ $i -lt $len ]; do
s3cmd put ${array[$i]} s3://myBackupFolder/
let i++
done

No comments:

Post a Comment