updated github path
[oweals/finalsclub.git] / fcbackups / fc_rotating_db_backup.sh
1 #!/bin/bash
2
3 ## cron example 
4 ## ## this script handles the daily rotating mongodb backup task for the finalsclub project
5 ## 30 0 * * * /home/ec2-user/fc/fcbackups/fc_rotating_db_backup.sh > /home/ec2-user/fc/fcbackups/cron_log.txt
6
7 ## this script is dependent on the cp2s3 script
8 ## http://devblog.famundo.com/articles/2007/03/12/cp2s3-a-command-line-smart-copy-script-into-s3
9 ## assumes you've installed ruby and ruby gems.  
10 ## sudo yum install ruby
11 ## sudo yum install rubygems
12 ## Installing cp2se requires the installation of the AWS::S3 Ruby library, easiers done with
13 ## sudo gem install aws-s3
14 ## sudo cp cp2s3.rb /usr/bin/cp2s3
15 ## exampe usage
16 ## cp2s3 -v -b finalsclub.org_db_backups -r test.txt
17
18
19 ## save the current working dir
20 pushd .
21 cd ~/fc/fcbackups
22
23 ## this scripts expects these vars to be set
24 ## export AWS_ACCESS_KEY_ID=<YOUR_AWS_ACCESS_KEY_ID>
25 ## export AWS_SECRET_ACCESS_KEY=<YOUR_AWS_SECRET_ACCESS_KEY>
26 if test -e .fcbackup.env ; then
27         source .fcbackup.env
28 fi
29
30
31 bucket="finalsclub.org_db_backups"
32 curdate=`date +"%Y-%m-%d"`
33 yearlydate=`date +"%Y-__-__"`
34 monthlydate=`date +"____-%m-__"`
35 dailydate=`date +"____-__-%d"`
36 mongodump=/usr/local/bin/mongodump
37
38 bakdir=db-backups/$curdate
39
40
41
42 ## create temp backup/dump dir
43 mkdir -p $bakdir 
44 cd $bakdir
45
46 ## do the db dump into the temp date dir
47 $mongodump --host localhost
48
49 ## create an archive from the dump dir
50 cd ../
51 tar czf db-dump_$curdate.tgz $curdate/dump
52
53 ## clean up by deleting the temp dir
54 rm -rf $curdate
55
56 ## rename and copy the latest dump archive up to s3, using the yeary, weekly and daily naming conventions
57 mv db-dump_$curdate.tgz db-dump_$yearlydate.tgz
58 cp2s3 -v -b $bucket/yearly -r db-dump_$yearlydate.tgz
59
60 mv db-dump_$yearlydate.tgz db-dump_$monthlydate.tgz
61 cp2s3 -v -b $bucket/monthly -r db-dump_$monthlydate.tgz
62
63 mv db-dump_$monthlydate.tgz db-dump_$dailydate.tgz
64 cp2s3 -v -b $bucket/daily -r db-dump_$dailydate.tgz
65 ## NOTE: we are keeping the last 31 daily backups on disk.
66
67 ## restore previous working dir
68 popd
69