Guest User

Untitled

a guest
Nov 28th, 2017
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. #########################
  4. # MongoDB Backup Script #
  5. #########################
  6. #
  7. # A simple script for automate MongoDB backup
  8. # Use this script in cron routine like example:
  9. #
  10. # 0 1 * * * /path/mongodb-backup.pl --hostname localhost >/dev/null 2>&1
  11. #
  12. # Author: Tales Luna <tales.ferreira.luna@gmail.com>
  13. #
  14. #
  15. use POSIX qw(strftime);
  16. use Getopt::Long;
  17.  
  18. my $help = undef;
  19. my $compress = 1;
  20. my $hostname = undef;
  21. my $port = 27017;
  22. my $username = '';
  23. my $password = '';
  24. my $outputDir = '/data/backup';
  25. my $filename = getCurrentFilename();
  26.  
  27. # Define options
  28. GetOptions(
  29. 'help' => \$help,
  30. 'hostname=s' => \$hostname,
  31. 'port=i' => \$port,
  32. 'username=s' => \$username,
  33. 'password=s' => \$password,
  34. 'output-dir=s' => \$outputDir,
  35. 'no-compress' => \$noCompress
  36.  
  37. ) or usageAlert();
  38.  
  39.  
  40. ################################
  41. # Case option --help is called #
  42. ################################
  43. if ($help) {
  44. usageAlert();
  45. }
  46.  
  47.  
  48. ###################################################
  49. # By default, the dumped data are compressed #
  50. # if this flag passed in params, no compress data #
  51. ###################################################
  52. if ($noCompress) {
  53. $compress = 0;
  54. }
  55.  
  56.  
  57. ####################
  58. # Require hostname #
  59. ####################
  60. unless ($hostname) {
  61. usageAlert();
  62. }
  63.  
  64.  
  65. #########################
  66. # Run mongodump command #
  67. #########################
  68. sub runMongoDump() {
  69.  
  70. my $savePath = $outputDir.$hostname.'/'.$filename;
  71. # EX: ~/.mongodb-backup/data/localhost/27-11-2017-backup-1511800173
  72.  
  73. print "\n[+] Saving all MongoDB data in $savePath\n\n";
  74.  
  75. # Execute mongodump command
  76. `mongodump --host $hostname --port $port --username '$username' --password '$password' --out $savePath`;
  77.  
  78. # Verify if files exists
  79. if (-e $savePath) {
  80.  
  81. # Case compress definition is true, compress the path to .tar.gz
  82. if ($compress) {
  83. `cd $savePath/.. && tar -zcf $filename.tar.gz $filename/ && rm -rf $filename`;
  84. $savePath = $savePath.'.tar.gz';
  85. }
  86.  
  87. print "\n[+] Backup saved with successful\n";
  88. print "[+] File: $savePath\n\n";
  89. } else {
  90. print "\n[!] FAIL ON SAVE BACKUP\n\n."
  91. }
  92. }
  93.  
  94. ##########################################
  95. # Return file name based in current time #
  96. ##########################################
  97. sub getCurrentFilename() {
  98. return strftime "%d-%m-%Y-backup-".time, localtime;
  99. #EX: 27-11-2017-backup-1511800173
  100. }
  101.  
  102. ##################
  103. # Run Usage text #
  104. ##################
  105. sub usageAlert() {
  106. print <<USAGE;
  107.  
  108. --help > Show this text
  109. --hostname > MongoDB server hostname (yes)
  110. --port > MongoDB server port number (default: $port)
  111. --username > MongoDB authentication username (no)
  112. --passwotd > MongoDB authentication password (no)
  113. --database > Mongodb authentication database (no)
  114. --output-dir > Set directory to save dumped data (default: $outputDir)
  115. --no-compress > Skip compress the dumped data (default: false)
  116.  
  117. USAGE
  118. exit(0);
  119. }
  120.  
  121. runMongoDump();
Add Comment
Please, Sign In to add comment