Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Backup a mongodb database and send the resulting backup archive (tar.gz) to an S3 bucket.
- #
- # Prerequisites:
- # mongodump
- # s3cmd
- #
- # Use in crontab, e.g. for nightly backups (at 02:25):
- # 25 2 * * * /usr/local/bin/mongo-backup -c /usr/local/etc/mongo-backup.conf >> /var/log/mongo-backup.log 2>&1
- #
- # Example configuration (mongo-backup.conf):
- # host=db-host01.domain.com
- # database=app_db # leave blank to backup all databases
- # port=27017
- # username=db_admin
- # password=qwerty
- # s3_bucket_url=s3://mongo-backup/db/backups
- # lockdir=/tmp/mongo-backup.lock
- #
- check_cmd() {
- local cmds="mongodump s3cmd"
- for cmd in $cmds; do
- command -v $cmd >/dev/null 2>&1 || { echo >&2 "$cmd is required, could not find it. Aborting."; exit 1; }
- done
- if [ ! -e ~/.s3cfg ]; then
- echo "A valid s3cmd configuration file is required in ~/.s3cfg. Aborting."
- exit 1
- fi
- }
- load_user_settings() {
- if [ -z $config_file ]; then
- config_file="~/.mongo-backup"
- fi
- if [ -e $config_file ]; then
- . $config_file
- else
- echo "No configuration file found at $config_file. Aborting."
- exit 1
- fi
- required_fields="host s3_bucket_url"
- for field in $required_fields;
- do
- if [ ! ${!field} ]; then
- echo "$field must be defined, will exit."
- exit 1
- fi
- done
- }
- show_usage() {
- echo "The program will read the provided configuration file. If no configuration file is"
- echo "given, a default file will be read from ~/.mongo-backup"
- echo "Usage: $0 -c <config-file>"
- exit 1
- }
- parse_arguments() {
- while [[ "$#" -ge 1 ]]; do
- key="$1"
- case $key in
- -c|--config)
- config_file="$2"
- shift
- ;;
- -h|--help)
- show_usage
- shift
- ;;
- *)
- show_usage
- ;;
- esac
- shift
- done
- }
- set_variables() {
- # Defaults
- host=
- database=
- port=27017
- username=
- password=
- s3_bucket_url=
- lockdir=/var/tmp/mylock
- load_user_settings
- local datestamp=$(date +%Y-%m-%d)
- local timestamp=$(date +%Y%m%d_%H%M%S)
- pidfile=$lockdir/pid
- mongodump=mongodump
- s3cmd=s3cmd
- dump_name="mongodump_$timestamp"
- archive_filename="$dump_name.tar.gz"
- tmpdir=$(mktemp -d)
- dump_path="$tmpdir/$dump_name"
- tar_gz_path="$tmpdir/$archive_filename"
- remote_path="$s3_bucket_url/$archive_filename"
- echo "----------------------------------------"
- echo "Host: $host"
- echo "Database: $database"
- echo "Username: $username"
- echo "Password: <hidden>"
- echo "----------------------------------------"
- }
- log() {
- local message="$1"
- echo "$(date -u) $message"
- }
- dump_database() {
- local dump_cmd="$mongodump --port $port --host $host -o $dump_path"
- if [ ! -z $username ]; then
- dump_cmd="$dump_cmd -u $username"
- fi
- if [ ! -z $password ]; then
- dump_cmd="$dump_cmd -p $password"
- fi
- if [ ! -z $database ]; then
- dump_cmd="$dump_cmd -d $database"
- fi
- echo "$(date -u) Dumping database."
- eval $dump_cmd > /dev/null 2>&1
- }
- compress_archive() {
- log "Compressing archive."
- local compress_cmd="tar -C $tmpdir -cvzf $tar_gz_path $dump_name"
- eval $compress_cmd > /dev/null 2>&1
- }
- copy_to_remote() {
- log "Copying to remote."
- copy_cmd="$s3cmd put $tar_gz_path $remote_path"
- eval $copy_cmd > /dev/null 2>&1
- }
- init() {
- check_cmd
- parse_arguments $@
- set_variables
- }
- main() {
- log "Backup started."
- dump_database
- compress_archive
- copy_to_remote
- log "Backup completed."
- }
- # Initialize
- init $@
- # Handle trap and call main
- if ( mkdir ${lockdir} ) 2> /dev/null; then
- echo $$ > $pidfile
- trap 'rm -rf "$lockdir"; exit $?' INT TERM EXIT
- main
- rm -rf "$lockdir"
- trap - INT TERM EXIT
- else
- echo "Lock Exists: $lockdir owned by $(cat $pidfile)"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement