【MongoDB】MongoDB备份脚本
2022-08-01 21:34 abce 阅读(259) 评论(0) 编辑 收藏 举报#!/bin/bash ################################################################################ #0 * * * * /root/dhmon/mongo-backup.sh >> /root/dhmon/full_mongo_backup.log 2>&1 ################################################################################ export PATH=/bin:/usr/bin:/usr/local/bin TODAY=`date +"%Y%m%d%H%M%S"` ###################################################################### DB_BACKUP_PATH='/databak/mdb' MONGO_HOST='8.8.8.8' MONGO_PORT='27017' # If mongodb is protected with username password. # Set AUTH_ENABLED to 1 # and add MONGO_USER and MONGO_PASSWD values correctly AUTH_ENABLED=1 MONGO_USER='abcd' MONGO_PASSWD='abcd' # Set DATABASE_NAMES to "ALL" to backup all databases. # or specify databases names seprated with space to backup # specific databases only. DATABASE_NAMES='ALL' #DATABASE_NAMES='mydb db2 newdb' ## Number of days to keep local backup copy BACKUP_RETAIN_DAYS=15 ###################################################################### ###################################################################### # Create backup directory if ! mkdir -p ${DB_BACKUP_PATH}/${TODAY}; then echo "Can't create backup directory in ${DB_BACKUP_PATH}. Go and fix it!" 1>&2 exit 1; fi; AUTH_PARAM="" if [ ${AUTH_ENABLED} -eq 1 ]; then AUTH_PARAM=" --username ${MONGO_USER} --password ${MONGO_PASSWD} " fi if [ ${DATABASE_NAMES} = "ALL" ]; then echo "Running backup for all databases at $(date +"%Y-%m-%d:%H-%M-%S"). " echo "--------------------------------------------" mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} --gzip --oplog --out ${DB_BACKUP_PATH}/${TODAY}/ else echo "Running backup for selected databases" for DB_NAME in ${DATABASE_NAMES} do mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} --db ${DB_NAME} ${AUTH_PARAM} --gzip --out ${DB_BACKUP_PATH}/${TODAY}/ done fi ######## Remove backups older than {BACKUP_RETAIN_DAYS} days ######## if [ ! -z ${DB_BACKUP_PATH} ]; then cd ${DB_BACKUP_PATH} find $DB_BACKUP_PATH -type f -mtime +$BACKUP_RETAIN_DAYS -exec rm {} + echo "--------------------------------------------" echo "Database backup complete at $(date +"%Y-%m-%d:%H-%M-%S")!" fi ######################### End of script ##############################
https://pranavprakash.net/2017/02/04/automate-mongodb-backups-using-cron-and-shell-script/