Zimbra Copy to Amazon S3

I was kinda against using S3 for backups or anything as it has a cost.
By rotating my backups keeping under the gb store limit I manage to only pay for transfers and that’s always under 1$ USD.
Now I have 4 weeks of backups stored on S3 thanks to the below script that will :-
* Put the file to S3
* rotate the 4th oldest backup file and delete it
* email me the results.

First set up an S3 account,
second setup s3cmd tool (http://s3tools.org/s3cmd)

Edit the following to suit:-

LLOC=/var/log
PFILE=`/bin/cat /tmp/curzmbk`
BKBKT="s3://basstech/backups/"
DATE=`date +%Y-%m-%d`
BKLOC="/backup"
MAILADDRESS@SERVER

 

So here is the script to do it :-

#!/bin/bash

LLOC=/var/log
PFILE=`/bin/cat /tmp/curzmbk`
BKBKT="s3://bucket/backups/"
DATE=`date +%Y-%m-%d`
BKLOC="/backup"


function loghd() {
  echo "Copy and file rotation to S3" > $LLOC/S3-CPY-$DATE.log
  echo "Started at:- `date +%c`" >> $LLOC/S3-CPY-$DATE.log
  echo "--------------------------------------------" >> $LLOC/S3-CPY-$DATE.log
}

function lstbkt() {
  /usr/bin/s3cmd ls $@
}

function putflbkt() {
  echo  "Putting File $@" >> $LLOC/S3-CPY-$DATE.log
  /usr/bin/s3cmd put $BKLOC/$PFILE $@ >> $LLOC/S3-CPY-$DATE.log
  echo "Status  $?" >> $LLOC/S3-CPY-$DATE.log
}

function rotatebk() {
  echo "--------------------------------------------" >> $LLOC/S3-CPY-$DATE.log
  /usr/bin/s3cmd ls $BKBKT > /tmp/bkbktlst
  echo "Current Files in $BKBKT" >> $LLOC/S3-CPY-$DATE.log
  cat /tmp/bkbktlst >> $LLOC/S3-CPY-$DATE.log
  BKCNT=`/usr/bin/wc -l /tmp/bkbktlst | awk ' { print $1 }'`
  if [ $BKCNT -ge 3 ]; then
    echo "--------------------------------------------" >> $LLOC/S3-CPY-$DATE.log
    OLDBK=`cat /tmp/bkbktlst | sort | sed -n 1p | awk '{print $4}'`
    echo "Roating out oldest backup file:-" >> $LLOC/S3-CPY-$DATE.log
    echo "$OLDBK" >> $LLOC/S3-CPY-$DATE.log
    /usr/bin/s3cmd del $OLDBK  >> $LLOC/S3-CPY-$DATE.log
    echo "Status :- $?" >> $LLOC/S3-CPY-$DATE.log
    rm -f /tmp/bkbktlst
  else
    echo "Less then two backup files, not roating" >> $LLOC/S3-CPY-$DATE.log
    rm -f /tmp/bkbktlst
  fi
}

function lognd() {
  echo "--------------------------------------------" >> $LLOC/S3-CPY-$DATE.log
  echo "Copy to S3 ended on `date +%c`" >> $LLOC/S3-CPY-$DATE.log
  echo "--------------------------------------------" >> $LLOC/S3-CPY-$DATE.log
}

function mailrpt() {
  /bin/cat $LLOC/S3-CPY-$DATE.log | /bin/mail -s "S3 Copy Report for `date +%c`" MAILADDRESS@SERVER
}


loghd
putflbkt  $BKBKT
rotatebk
lognd
mailrpt
Advertisements

2 Responses to “Zimbra Copy to Amazon S3”

  1. Hi there!

    What’s the ‘PFILE’ variable?

  2. undersys Says:

    The PFILE variable is set by the cold backup script :
    https://lpig.wordpress.com/2011/09/18/zimbra-cold-backup-script/

    It outputs the name of the backup created.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: