-
-
Save samirfor/68692f2bc9bc1d4b27ba19819a1b5346 to your computer and use it in GitHub Desktop.
Mongodump All Databases Shell Script for Cronjob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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