Guest User

Untitled

a guest
May 23rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. mkdir /db_backups
  2.  
  3. /*
  4. * Step 1: Write the Script
  5. * Now that that’s out of the way, we’re going to write a little shell script that creates a
  6. * directory for the current day, then dumps MongoDB into it. That script looks like this
  7. */
  8.  
  9. #!/bin/sh
  10. DIR=`date +%m%d%y`
  11. DEST=/db_backups/$DIR
  12. mkdir $DEST
  13. mongodump -h <your_database_host> -d <your_database_name> -u <username> -p <password> -o $DEST
  14.  
  15. /*
  16. Step 2: Set Up cron to Run the Script Nightly
  17. Now that we have our backup script ready to roll,
  18. we just need to get it running nightly to perform the backups.
  19. This is easily done by using cron to run the script.
  20.  
  21. On your server, simply open the crontab like this:
  22. */
  23.  
  24. sudo crontab -e
  25.  
  26. 45 1 * * * ../../scripts/db_backup.sh
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. OR
  30. vi mongobackup.sh
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. #!/bin/sh
  34.  
  35. #=====================================================================
  36. # Set the following variables as per your requirement
  37. #=====================================================================
  38. # Database Name to backup
  39. MONGO_DATABASE="mydb"
  40. # Database host name
  41. MONGO_HOST="127.0.0.1"
  42. # Database port
  43. MONGO_PORT="27017"
  44. # Backup directory
  45. BACKUPS_DIR="/var/backups/$MONGO_DATABASE"
  46. # Database user name
  47. DBUSERNAME="username"
  48. # Database password
  49. DBPASSWORD="passw0rd"
  50. # Authentication database name
  51. DBAUTHDB="admin"
  52. # Days to keep the backup
  53. DAYSTORETAINBACKUP="14"
  54. #=====================================================================
  55.  
  56. TIMESTAMP=`date +%F-%H%M`
  57. BACKUP_NAME="$MONGO_DATABASE-$TIMESTAMP"
  58.  
  59. echo "Performing backup of $MONGO_DATABASE"
  60. echo "--------------------------------------------"
  61. # Create backup directory
  62. if ! mkdir -p $BACKUPS_DIR; then
  63. echo "Can't create backup directory in $BACKUPS_DIR. Go and fix it!" 1>&2
  64. exit 1;
  65. fi;
  66. # Create dump
  67. mongodump -d $MONGO_DATABASE --username $DBUSERNAME --password $DBPASSWORD --authenticationDatabase $DBAUTHDB
  68. # Rename dump directory to backup name
  69. mv dump $BACKUP_NAME
  70. # Compress backup
  71. tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
  72. # Delete uncompressed backup
  73. rm -rf $BACKUP_NAME
  74. # Delete backups older than retention period
  75. find $BACKUPS_DIR -type f -mtime +$DAYSTORETAINBACKUP -exec rm {} +
  76. echo "--------------------------------------------"
  77. echo "Database backup complete!"
  78.  
  79.  
  80. step 2
  81. chomd +x mongobackup.sh
  82.  
  83. step3
  84. crontab -e
  85.  
  86. step4 Enter
  87. 00 00 * * * /path/to/script/mongobackup.sh
Add Comment
Please, Sign In to add comment