Created
January 2, 2021 05:45
-
-
Save itsjimbo/cc43fb8b7b0f64138853f0a6c6fc163b to your computer and use it in GitHub Desktop.
Mongo db backup script - tested on OSX
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/bash | |
###################################################################### | |
# | |
# MONGODB backup script | |
# Notes - Use directory name to get date input instead of relying on file | |
# timestamp | |
# - Backup FIRST then remove older backups - if you don't have much | |
# space then you can reverse the process - but this ensure if failure | |
# occurs older backup will not get removed until fixed | |
# | |
#################################################################### | |
export PATH=/bin:/usr/bin:/usr/local/bin | |
TODAY=`date +"%Y-%m-%d"` | |
## Number of days to keep local backup copy | |
BACKUP_RETENTION_DAYS=7 | |
DBDELDATE=`gdate +"%Y%m%d" --date="${BACKUP_RETENTION_DAYS} days ago"` | |
echo "info: script will remove backups older than $DBDELDATE" | |
###################################################################### | |
# | |
# Set backup directory and host params | |
# | |
DB_BACKUP_PATH='/backup/mongo' | |
MONGO_HOST='localhost' | |
MONGO_PORT='27017' | |
###################################################################### | |
# | |
# Set AUTH_ENABLED to 1 if MONGO requires a username/password | |
# and add MONGO_USER and MONGO_PASSWD values correctly | |
AUTH_ENABLED=1 | |
MONGO_USER='root' | |
MONGO_PASSWD='root' | |
###################################################################### | |
# | |
# 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='test local db1 db2 db3' | |
###################################################################### | |
# | |
# Backup now | |
mkdir -p ${DB_BACKUP_PATH}/${TODAY} | |
AUTH_PARAM="" | |
if [ ${AUTH_ENABLED} -eq 1 ]; then | |
AUTH_PARAM=" --username ${MONGO_USER} --password ${MONGO_PASSWD} " | |
fi | |
if [ ${DATABASE_NAMES} = "ALL" ]; then | |
echo "You have choose to backup all databases" | |
mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} --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} --out ${DB_BACKUP_PATH}/${TODAY}/ | |
done | |
fi | |
###################################################################### | |
# | |
# Remove backups older than {BACKUP_RETENTION_DAYS} days ######## | |
if [ ! -z ${DB_BACKUP_PATH} ]; then | |
for i in `ls -d ${DB_BACKUP_PATH}/* | cut -f1`; | |
do | |
local_dir=$(basename "$i" | sed -e 's/-//g') | |
local_dir_date=$( date -j -f "%Y%m%d" "$local_dir" +"%s" ) | |
cutoff_date=$( date -j -f "%Y%m%d" "$DBDELDATE" +"%s" ) | |
if (( $local_dir_date < $cutoff_date )); then | |
echo " ==> removing stale backup ${i}" | |
rm -rf "${i}" | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment