Skip to content

Commit

Permalink
Fix and improve deletion of old backups
Browse files Browse the repository at this point in the history
  • Loading branch information
jeansibelius committed Mar 31, 2022
1 parent ab7a2a8 commit b08bd81
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
config
*.swp
mongobackup.log
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Tool to create MongoDB backups from your MongoDB Atlas database through commandl
## Installation

1. Clone the repository to a location of your choosing.
2. Create a symlink in a folder included in your PATH (e.g. bin) for easing running of the script:
2. Make sure the script file is executable (`chmod +x /path/to/script`)
3. Create a symlink in a folder included in your PATH (e.g. bin) for easing running of the script:
1. `ln -s <PATH_TO_SCRIPT> /home/$USER/bin/mongobackup`
3. Copy the `example_config` to a file called `config` (keeping it in the same folder with the
4. Copy the `example_config` to a file called `config` (keeping it in the same folder with the
script) and edit your MongoDB Atlas details in. Be
careful not to commit your private details to any repository.

Expand Down
6 changes: 3 additions & 3 deletions example_config
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ MONGODB_PASSWORD="your_mongodb_atlas_db_user_password"
MONGODB_DB_NAMES="first_db second_db"
MONGODB_CLUSTER="address_to_your_cluster_e.g._project.plpgm.mongodb.net"

# If you want to save to a custom location, uncomment the line below and change path as desired
# Default location: /home/$USER/MONGODB_DUMP/
#CUSTOM_OUTPUT_PATH="/home/$USER/CUSTOM_FOLDER"
# Folder where the backups should be saved
# Make sure the cron job has the permissions to write to the specified path
LOCAL_OUTPUT_PATH=""

# Number of days of backups you want to retain (default: 7)
#RETAIN_LAST_X_DAYS_BACKUPS="7"
70 changes: 62 additions & 8 deletions mongodb-cron-backups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@ USR="$MONGODB_USR"
PSWD="$MONGODB_PASSWORD"
CONNECTION="$MONGODB_CLUSTER"
DB_NAMES="$MONGODB_DB_NAMES"
OUTPUT_PATH="${CUSTOM_OUTPUT_PATH:-"/home/$USER/MONGODB_DUMP/"}"
OUTPUT_PATH="$LOCAL_OUTPUT_PATH"

# Options for cron scheduling
CRONCMD="env USER=$USER ${SCRIPTPATH} create_and_delete > ${BASEDIR}/mongobackup.log 2>&1"
CRONSCHEDULE="${CRON_SCHEDULE:-"0 0 * * *"}"
CRONJOB="$CRONSCHEDULE $CRONCMD"

DAYS_TO_RETAIN="${RETAIN_LAST_X_DAYS_BACKUPS:-"7"}"

# Create backup
create_backup(){
mkdir -p $OUTPUT_PATH && cd $OUTPUT_PATH
if [ ${#OUTPUT_PATH} -lt 1 ]; then
echo "LOCAL_OUTPUT_PATH string length was less than 1. Please check that you have a valid path defined."
exit 1
fi
mkdir -p $OUTPUT_PATH && cd $OUTPUT_PATH || ( echo "ERROR: Failed to created directory." && exit 1 )
START_MESSAGE="Creating a backup of $CONNECTION using mongodump in $OUTPUT_PATH"
echo "\n${pboldcolor}$START_MESSAGE${pclear}"
logger $START_MESSAGE
Expand Down Expand Up @@ -62,17 +73,48 @@ create_backup(){
# Retain e.g. backups from last X (default: 7) days and delete rest
delete_old_backups(){
cd $OUTPUT_PATH
find -ctime +"${RETAIN_LAST_X_DAYS_BACKUPS:-"7"}" -delete
DEL_MESSAGE="Backups older than "${RETAIN_LAST_X_DAYS_BACKUPS:-"7"}" days deleted for $CONNECTION."
printf "\n${pboldcolor}$DEL_MESSAGE${pclear}\n\n"
logger $DEL_MESSAGE
echo "\n${pboldcolor}Deleting backups older than $DAYS_TO_RETAIN days in $OUTPUT_PATH${pclear}\n"
TOTAL_BACKUPS="0"
if [ $OUTPUT_PATH = $PWD ]; then
for db in $DB_NAMES; do
filename="${db}.archive.gz"
echo "Looking for files ending in .$filename..."
temp_file=$(mktemp)
find $OUTPUT_PATH -name "*.${filename}" -type f -ctime "$DAYS_TO_RETAIN" -print > $temp_file
lines=$(cat $temp_file | wc -l)
TOTAL_BACKUPS=$(( $TOTAL_BACKUPS + $lines))
if [ $lines -gt "0" ]; then
echo "Found $(cat $temp_file | wc -l) file(s). Deleting..."
cat $temp_file && cat $temp_file | xargs -r rm --
echo "Done.\n"
else
echo "No files found.\n"
fi
rm $temp_file
done
DEL_MESSAGE="Total of $TOTAL_BACKUPS backups older than $DAYS_TO_RETAIN days deleted for $CONNECTION."
printf "${pboldcolor}$DEL_MESSAGE${pclear}\n\n"
logger $DEL_MESSAGE
else
echo "Wrong directory $PWD"
fi
}

create_and_delete(){
create_backup
delete_old_backups
}

add_to_cron(){
( crontab -l | grep -v -F "$CRONCMD" ; echo "$CRONJOB" ) | crontab -
printf "Added $SCRIPTPATH to crontab.\n"
}

remove_from_cron(){
( crontab -l | grep -v -F "$CRONCMD" ) | crontab -
printf "Removed $SCRIPTPATH from crontab.\n"
}

help(){
printf \
"
Expand All @@ -96,9 +138,17 @@ Run ${pbold}mongobackup [action]${pclear} (or whatever name you specified instea
${pbold}delete${pclear}
Delete backups older than X days (as specified in the config file; defaults to 7, if not set).
${pbold}createdelete${pclear}
${pbold}create_and_delete${pclear}
Run the two above commands in sequence.
${pbold}add_to_cron${pclear}
Add the script to the current users crontab (using either the CRON_SCHEDULE from config or default daily at midnight).
Can also be re-run in order to update the schedule of the an existing cron job added by the same command.
${pbold}remove_from_cron${pclear}
Removes the script from crontab as added by add_to_cron.
${pbold}help${pclear}
Display this help file.
\n"
Expand All @@ -110,8 +160,12 @@ case "$1" in
create_backup;;
"delete")
delete_old_backups;;
"createdelete")
"create_and_delete")
create_and_delete;;
"add_to_cron")
add_to_cron;;
"remove_from_cron")
remove_from_cron;;
*)
help;;
esac

0 comments on commit b08bd81

Please sign in to comment.