Skip to content

Instantly share code, notes, and snippets.

@talesluna-zz
Last active November 28, 2017 00:08
Show Gist options
  • Save talesluna-zz/ccdd380c4ee41900ce7608f504b8f959 to your computer and use it in GitHub Desktop.
Save talesluna-zz/ccdd380c4ee41900ce7608f504b8f959 to your computer and use it in GitHub Desktop.
A simple script for automate MongoDB backup
#!/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 <[email protected]>
#
#
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment