fc_rotating_db_backup.sh 2.0 KB

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