Skip to content

Instantly share code, notes, and snippets.

@samirfor
Forked from sheharyarn/mongo_backup.sh
Last active May 10, 2017 13:17
Show Gist options
  • Save samirfor/68692f2bc9bc1d4b27ba19819a1b5346 to your computer and use it in GitHub Desktop.
Save samirfor/68692f2bc9bc1d4b27ba19819a1b5346 to your computer and use it in GitHub Desktop.
Mongodump All Databases Shell Script for Cronjob
#!/bin/sh
set -e
HOST_NAME="$(hostname)"
MONGO_HOST="127.0.0.1"
MONGO_PORT=27017
MONGO_USER="admin"
MONGO_PASS="password"
MONGO_AUTHDB="admin"
TIMESTAMP=$(date +%F-%H%M)
MONGODUMP_PATH="$(which mongodump)"
BACKUPS_DIR="/tmp" # without a final slash
BACKUP_NAME="mongodb-${HOST_NAME}-${TIMESTAMP}"
[ ! -f "${MONGODUMP_PATH}" ] && \
echo >&2 "Mongodump binary not found in ${MONGODUMP_PATH}." && \
echo >&2 "Make sure you have mongodb-org-tools installed." && \
exit 1
[ ! -d "${BACKUPS_DIR}" ] && \
mkdir -p "${BACKUPS_DIR}"
"$MONGODUMP_PATH" \
--host "${MONGO_HOST}" \
--port ${MONGO_PORT} \
--username "${MONGO_USER}" \
--password "${MONGO_PASS}" \
--authenticationDatabase "${MONGO_AUTHDB}" \
--archive="${BACKUPS_DIR}/${BACKUP_NAME}.gz" \
--gzip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment