Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- #########################
- # MongoDB Backup Script #
- #########################
- #
- # A simple script for automate MongoDB backup
- # Use this script in cron routine like example:
- #
- # 0 1 * * * /path/mongodb-backup.pl --hostname localhost >/dev/null 2>&1
- #
- # Author: Tales Luna <tales.ferreira.luna@gmail.com>
- #
- #
- use POSIX qw(strftime);
- use Getopt::Long;
- my $help = undef;
- my $compress = 1;
- my $hostname = undef;
- my $port = 27017;
- my $username = '';
- my $password = '';
- my $outputDir = '/data/backup';
- my $filename = getCurrentFilename();
- # Define options
- GetOptions(
- 'help' => \$help,
- 'hostname=s' => \$hostname,
- 'port=i' => \$port,
- 'username=s' => \$username,
- 'password=s' => \$password,
- 'output-dir=s' => \$outputDir,
- 'no-compress' => \$noCompress
- ) or usageAlert();
- ################################
- # Case option --help is called #
- ################################
- if ($help) {
- usageAlert();
- }
- ###################################################
- # By default, the dumped data are compressed #
- # if this flag passed in params, no compress data #
- ###################################################
- if ($noCompress) {
- $compress = 0;
- }
- ####################
- # Require hostname #
- ####################
- unless ($hostname) {
- usageAlert();
- }
- #########################
- # Run mongodump command #
- #########################
- sub runMongoDump() {
- my $savePath = $outputDir.$hostname.'/'.$filename;
- # EX: ~/.mongodb-backup/data/localhost/27-11-2017-backup-1511800173
- print "\n[+] Saving all MongoDB data in $savePath\n\n";
- # Execute mongodump command
- `mongodump --host $hostname --port $port --username '$username' --password '$password' --out $savePath`;
- # Verify if files exists
- if (-e $savePath) {
- # Case compress definition is true, compress the path to .tar.gz
- if ($compress) {
- `cd $savePath/.. && tar -zcf $filename.tar.gz $filename/ && rm -rf $filename`;
- $savePath = $savePath.'.tar.gz';
- }
- print "\n[+] Backup saved with successful\n";
- print "[+] File: $savePath\n\n";
- } else {
- print "\n[!] FAIL ON SAVE BACKUP\n\n."
- }
- }
- ##########################################
- # Return file name based in current time #
- ##########################################
- sub getCurrentFilename() {
- return strftime "%d-%m-%Y-backup-".time, localtime;
- #EX: 27-11-2017-backup-1511800173
- }
- ##################
- # Run Usage text #
- ##################
- sub usageAlert() {
- print <<USAGE;
- --help > Show this text
- --hostname > MongoDB server hostname (yes)
- --port > MongoDB server port number (default: $port)
- --username > MongoDB authentication username (no)
- --passwotd > MongoDB authentication password (no)
- --database > Mongodb authentication database (no)
- --output-dir > Set directory to save dumped data (default: $outputDir)
- --no-compress > Skip compress the dumped data (default: false)
- USAGE
- exit(0);
- }
- runMongoDump();
Add Comment
Please, Sign In to add comment