Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Backup a mongodb database and send the resulting backup archive (tar.gz) to an S3 bucket.
  4. #
  5. # Prerequisites:
  6. # mongodump
  7. # s3cmd
  8. #
  9. # Use in crontab, e.g. for nightly backups (at 02:25):
  10. # 25 2 * * * /usr/local/bin/mongo-backup -c /usr/local/etc/mongo-backup.conf >> /var/log/mongo-backup.log 2>&1
  11. #
  12. # Example configuration (mongo-backup.conf):
  13. # host=db-host01.domain.com
  14. # database=app_db # leave blank to backup all databases
  15. # port=27017
  16. # username=db_admin
  17. # password=qwerty
  18. # s3_bucket_url=s3://mongo-backup/db/backups
  19. # lockdir=/tmp/mongo-backup.lock
  20. #
  21.  
  22. check_cmd() {
  23. local cmds="mongodump s3cmd"
  24. for cmd in $cmds; do
  25. command -v $cmd >/dev/null 2>&1 || { echo >&2 "$cmd is required, could not find it. Aborting."; exit 1; }
  26. done
  27.  
  28. if [ ! -e ~/.s3cfg ]; then
  29. echo "A valid s3cmd configuration file is required in ~/.s3cfg. Aborting."
  30. exit 1
  31. fi
  32. }
  33.  
  34. load_user_settings() {
  35. if [ -z $config_file ]; then
  36. config_file="~/.mongo-backup"
  37. fi
  38.  
  39. if [ -e $config_file ]; then
  40. . $config_file
  41. else
  42. echo "No configuration file found at $config_file. Aborting."
  43. exit 1
  44. fi
  45.  
  46. required_fields="host s3_bucket_url"
  47. for field in $required_fields;
  48. do
  49. if [ ! ${!field} ]; then
  50. echo "$field must be defined, will exit."
  51. exit 1
  52. fi
  53. done
  54. }
  55.  
  56. show_usage() {
  57. echo "The program will read the provided configuration file. If no configuration file is"
  58. echo "given, a default file will be read from ~/.mongo-backup"
  59. echo "Usage: $0 -c <config-file>"
  60. exit 1
  61. }
  62.  
  63. parse_arguments() {
  64. while [[ "$#" -ge 1 ]]; do
  65. key="$1"
  66. case $key in
  67. -c|--config)
  68. config_file="$2"
  69. shift
  70. ;;
  71. -h|--help)
  72. show_usage
  73. shift
  74. ;;
  75. *)
  76. show_usage
  77. ;;
  78. esac
  79. shift
  80. done
  81. }
  82.  
  83. set_variables() {
  84. # Defaults
  85. host=
  86. database=
  87. port=27017
  88. username=
  89. password=
  90. s3_bucket_url=
  91.  
  92. lockdir=/var/tmp/mylock
  93.  
  94. load_user_settings
  95.  
  96. local datestamp=$(date +%Y-%m-%d)
  97. local timestamp=$(date +%Y%m%d_%H%M%S)
  98.  
  99. pidfile=$lockdir/pid
  100. mongodump=mongodump
  101. s3cmd=s3cmd
  102.  
  103. dump_name="mongodump_$timestamp"
  104. archive_filename="$dump_name.tar.gz"
  105. tmpdir=$(mktemp -d)
  106. dump_path="$tmpdir/$dump_name"
  107. tar_gz_path="$tmpdir/$archive_filename"
  108. remote_path="$s3_bucket_url/$archive_filename"
  109.  
  110. echo "----------------------------------------"
  111. echo "Host: $host"
  112. echo "Database: $database"
  113. echo "Username: $username"
  114. echo "Password: <hidden>"
  115. echo "----------------------------------------"
  116. }
  117.  
  118. log() {
  119. local message="$1"
  120. echo "$(date -u) $message"
  121. }
  122.  
  123. dump_database() {
  124. local dump_cmd="$mongodump --port $port --host $host -o $dump_path"
  125. if [ ! -z $username ]; then
  126. dump_cmd="$dump_cmd -u $username"
  127. fi
  128.  
  129. if [ ! -z $password ]; then
  130. dump_cmd="$dump_cmd -p $password"
  131. fi
  132.  
  133. if [ ! -z $database ]; then
  134. dump_cmd="$dump_cmd -d $database"
  135. fi
  136.  
  137. echo "$(date -u) Dumping database."
  138. eval $dump_cmd > /dev/null 2>&1
  139. }
  140.  
  141. compress_archive() {
  142. log "Compressing archive."
  143. local compress_cmd="tar -C $tmpdir -cvzf $tar_gz_path $dump_name"
  144. eval $compress_cmd > /dev/null 2>&1
  145. }
  146.  
  147. copy_to_remote() {
  148. log "Copying to remote."
  149. copy_cmd="$s3cmd put $tar_gz_path $remote_path"
  150. eval $copy_cmd > /dev/null 2>&1
  151. }
  152.  
  153. init() {
  154. check_cmd
  155. parse_arguments $@
  156. set_variables
  157. }
  158.  
  159. main() {
  160. log "Backup started."
  161. dump_database
  162. compress_archive
  163. copy_to_remote
  164. log "Backup completed."
  165. }
  166.  
  167. # Initialize
  168. init $@
  169.  
  170. # Handle trap and call main
  171. if ( mkdir ${lockdir} ) 2> /dev/null; then
  172. echo $$ > $pidfile
  173. trap 'rm -rf "$lockdir"; exit $?' INT TERM EXIT
  174.  
  175. main
  176.  
  177. rm -rf "$lockdir"
  178. trap - INT TERM EXIT
  179. else
  180. echo "Lock Exists: $lockdir owned by $(cat $pidfile)"
  181. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement