Skip to content

Commit

Permalink
Add MongoDB support.
Browse files Browse the repository at this point in the history
  • Loading branch information
kissifrot committed Jun 18, 2018
1 parent 6cf5bb1 commit 1f819a3
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Backup Manager 0.7.16
[ doekia ]
* Add MongoDB support

Backup Manager 0.7.15
[ Philippe Villiers ]
* Documentation typos fixes
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.15
0.7.16
2 changes: 1 addition & 1 deletion backup-manager
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 38 additions & 0 deletions backup-manager.conf.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export BM_ARCHIVE_NICE_LEVEL="10"
# - tarball-incremental
# - mysql
# - pgsql
# - mongodb
# - svn
# - pipe
# - none
Expand Down Expand Up @@ -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
#############################################################
Expand Down
3 changes: 3 additions & 0 deletions lib/actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function make_archives()
pgsql)
backup_method_pgsql "$method"
;;
mongodb)
backup_method_mongodb "$method"
;;
tarball|tarball-incremental)
backup_method_tarball "$method"
;;
Expand Down
48 changes: 48 additions & 0 deletions lib/backup-methods.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions lib/externals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1f819a3

Please sign in to comment.