From 1f819a3e937f6f6b2df454a8260a17c3ca33d90a Mon Sep 17 00:00:00 2001 From: Philippe Villiers Date: Mon, 18 Jun 2018 08:39:53 +0200 Subject: [PATCH] Add MongoDB support. --- ChangeLog | 4 ++++ VERSION | 2 +- backup-manager | 2 +- backup-manager.conf.tpl | 38 ++++++++++++++++++++++++++++++++ lib/actions.sh | 3 +++ lib/backup-methods.sh | 48 +++++++++++++++++++++++++++++++++++++++++ lib/externals.sh | 2 ++ 7 files changed, 97 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index ab43205..a002b58 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Backup Manager 0.7.16 + [ doekia ] + * Add MongoDB support + Backup Manager 0.7.15 [ Philippe Villiers ] * Documentation typos fixes diff --git a/VERSION b/VERSION index def4250..bf7b715 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.7.15 +0.7.16 diff --git a/backup-manager b/backup-manager index b296ed7..331cfa9 100755 --- a/backup-manager +++ b/backup-manager @@ -26,7 +26,7 @@ set -e RELEASE="true" -VERSION="0.7.15" +VERSION="0.7.16" # All the paths we provide libdir="/usr/lib/backup-manager" diff --git a/backup-manager.conf.tpl b/backup-manager.conf.tpl index 16ca081..01d8767 100644 --- a/backup-manager.conf.tpl +++ b/backup-manager.conf.tpl @@ -73,6 +73,7 @@ export BM_ARCHIVE_NICE_LEVEL="10" # - tarball-incremental # - mysql # - pgsql +# - mongodb # - svn # - pipe # - none @@ -262,6 +263,43 @@ export BM_PGSQL_FILETYPE="bzip2" # command line.) export BM_PGSQL_EXTRA_OPTIONS="" +############################################################## +# Backup method: mongodb +############################################################# + +# This method is dedicated to MongoDB databases. +# Enter here the list of databases to backup. +# Wildcard: __ALL__ (will dump all the databases in one archive) +export BM_MONGODB_DATABASES="__ALL__" + +# The user who is allowed to read every databases filled in BM_MYSQL_DATABASES +# Typical sysbackup user can be created by the following command: +# mongo --quiet --username=root admin +# > use admin +# > db.createUser({user:"sysbackup",pwd:"somesecret",roles:["backup","clusterAdmin","readAnyDatabase"]}); +# > quit() +export BM_MONGODB_BACKUPLOGIN="sysbackup" + +# its password +export BM_MONGODB_BACKUPPASS="" + +# the host where the database is +export BM_MONGODB_HOST="localhost" + +# the port where MySQL listen to on the host +export BM_MONGODB_PORT="27017" + +# Extra options to append to mysqldump +# (take care to what you do; this will be silently added to the +# command line.) +export BM_MONGODB_EXTRA_OPTIONS="" + +# Make separate backups of each database? +export BM_MONGODB_SEPARATELY="true" + +# Specify DBs to exclude here (separated by space) +export BM_MONGODB_DBEXCLUDE="" + ############################################################## # Backup method: svn ############################################################# diff --git a/lib/actions.sh b/lib/actions.sh index c35735a..af7adee 100644 --- a/lib/actions.sh +++ b/lib/actions.sh @@ -34,6 +34,9 @@ function make_archives() pgsql) backup_method_pgsql "$method" ;; + mongodb) + backup_method_mongodb "$method" + ;; tarball|tarball-incremental) backup_method_tarball "$method" ;; diff --git a/lib/backup-methods.sh b/lib/backup-methods.sh index 629a7df..62f0605 100644 --- a/lib/backup-methods.sh +++ b/lib/backup-methods.sh @@ -1100,3 +1100,51 @@ function backup_method_pipe() index=$(($index + 1)) done } + +function backup_method_mongodb() +{ + method="$1" + debug "backup_method_mongodb ($method)" + + info "Using method \"\$method\"." + if [[ ! -x $mongodump ]]; then + error "The \"mongodb\" method is chosen, but \$mongodump is not found." + fi + + # SECURITY CAVEAT: Because of https://jira.mongodb.org/browse/SERVER-5897 the password is disclosed thru ps during the backup + base_command="echo $BM_MONGODB_BACKUPPASS | $mongodump --authenticationDatabase=admin --quiet --username=$BM_MONGODB_BACKUPLOGIN --host=$BM_MONGODB_HOST:$BM_MONGODB_PORT $BM_MONGODB_EXTRA_OPTIONS --gzip --archive " + base_command="$mongodump --authenticationDatabase=admin --quiet --username=$BM_MONGODB_BACKUPLOGIN --host=$BM_MONGODB_HOST:$BM_MONGODB_PORT $BM_MONGODB_EXTRA_OPTIONS --gzip --archive --password=$BM_MONGODB_BACKUPPASS" + + # get each DB name if backing up separately + if [ "$BM_MONGODB_DATABASES" = "__ALL__" ]; then + if [ "$BM_MONGODB_SEPARATELY" = "true" ]; then + if [[ ! -x $mongo ]]; then + error "Can't find "$mongo" but this is needed when backing up databases separately." + fi + + DBNAMES=$(echo 'var _=db.auth("'${BM_MONGODB_BACKUPLOGIN}'","'${BM_MONGODB_BACKUPPASS}'");_=db.adminCommand({listDatabases:1,nameOnly:true}).databases.forEach(function(d){print(d.name);});' | $mongo --quiet --host $BM_MONGODB_HOST:$BM_MONGODB_PORT admin) + + # if DBs are excluded + for exclude in $BM_MONGODB_DBEXCLUDE + do + DBNAMES=$(echo $DBNAMES | sed "s/\b$exclude\b//g") + done + + BM_MONGODB_DATABASES=$DBNAMES + fi + fi + + for database in $BM_MONGODB_DATABASES + do + if [[ "$database" = "__ALL__" ]]; then + file_to_create="$BM_REPOSITORY_ROOT/${BM_ARCHIVE_PREFIX}-all-mongodb-databases.$TODAY.archive.gz" + compress="none" + command="$base_command" + else + file_to_create="$BM_REPOSITORY_ROOT/${BM_ARCHIVE_PREFIX}-mongodb-${database}.$TODAY.archive.gz" + compress="none" + command="$base_command -d $database" + fi + __create_file_with_meta_command + done +} diff --git a/lib/externals.sh b/lib/externals.sh index 149f9d5..747a38a 100644 --- a/lib/externals.sh +++ b/lib/externals.sh @@ -22,3 +22,5 @@ svnadmin=$(which svnadmin 2> /dev/null) || true logger=$(which logger 2> /dev/null) || true nice_bin=$(which nice 2> /dev/null) || true dd=$(which dd 2> /dev/null) || true +mongodump=$(which mongodump 2> /dev/null) || true +mongo=$(which mongo 2> /dev/null) || true