Skip to content

Instantly share code, notes, and snippets.

@freegenie
Created August 11, 2010 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freegenie/519750 to your computer and use it in GitHub Desktop.
Save freegenie/519750 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is supposed to be executed on the
# machine which hosts the database.
#
# What it does is to dump the entire database in
# separate directories, and then tgzip each single
# database.
# Example:
# ./mongobackup.sh /var/backup/zipped_mongodb
#
# Store username and password to access the mongodb
# instance as admin
USERNAME=""
PASSWORD=""
if test -n "$PASSWORD" ; then
PASSWORD_OPTION=" -p $PASSWORD"
fi
if test -n "$USERNAME" ; then
USERNAME_OPTION=" -u $USERNAME "
fi
if test -n "$1" ; then
DESTINATION_PATH="$1"
DESTINATION_OPTION=" -o $DESTINATION_PATH"
else
DESTINATION_PATH='dump' # mongodb's defautl
fi
COMMAND="mongodump --directoryperdb $USERNAME_OPTION $PASSWORD_OPTION $DESTINATION_OPTION"
echo $COMMAND
# Execute
$COMMAND
if test $? != 0 ; then
echo "mongodump command did not complete. Exiting."
exit 130
fi
# move to destination path, zip and clean
cd $DESTINATION_PATH
for i in $(ls) ; do
tar zcvf $i.tar.gz $i
rm -rf $i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment