Created
April 15, 2024 09:02
-
-
Save RobiMez/cda5d1a647066d4c55072a90ee831efd to your computer and use it in GitHub Desktop.
A simple script to back up a mongo db server and send statuses to a telegram group
This file contains hidden or 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 | |
# Capture the start time | |
start_time=$(date +%s) | |
# Telegram bot token and chat ID | |
bot_token="<TOKEN HERE>" | |
chat_id="<CHAT ID HERE>" | |
# MongoDB base command | |
mongodump_base_command="mongodump --uri=\"mongodb://< URI HERE >"" | |
# Function to send a message via Telegram bot | |
send_message() { | |
local message=$1 | |
curl -s -X POST "https://api.telegram.org/bot$bot_token/sendMessage" -d chat_id="$chat_id" -d text="$message" | |
} | |
# Get current datetime | |
DateRn=$(date +"%Y-%m-%d_%H-%M-%S") | |
send_message "Backup has started at $DateRn . Performing preflight checks..." | |
# Create the Backups directory if it doesn't exist | |
mkdir -p ./Backups | |
# Create the todays Backups directory if it doesn't exist | |
mkdir -p ./Backups/$DateRn | |
backup_dir_main="./Backups/$DateRn" | |
# Backup directory names | |
backup_dir_archive="./Backups/$DateRn/${DateRn}_archive" | |
backup_dir_normal="./Backups/$DateRn/${DateRn}_normal" | |
# Create the backup directories if they don't exist | |
mkdir -p $backup_dir_archive | |
mkdir -p $backup_dir_normal | |
send_message "Preflight checks completed successfully at $DateRn. Starting MongoDB dump..." | |
# MongoDB dump commands | |
mongodump_command_archive="$mongodump_base_command --archive=$backup_dir_archive/${DateRn}_archive.archive" | |
mongodump_command_normal="$mongodump_base_command -o $backup_dir_normal" | |
# Run the MongoDB dump commands and capture their exit statuses | |
$mongodump_command_archive | |
exit_status_archive=$? | |
$mongodump_command_normal | |
exit_status_normal=$? | |
send_message "MongoDB dump ran at $DateRn . Compressing the backup archives..." | |
# Compress the backup archives using 7z with lower priority | |
nice -n 19 7z a -mx=9 $backup_dir_main/${DateRn}_archive.7z $backup_dir_archive | |
send_message "MongoDB Archive mode dump compressed" | |
nice -n 19 7z a -mx=9 $backup_dir_main/${DateRn}_normal.7z $backup_dir_normal | |
send_message "MongoDB Normal mode dump compressed" | |
send_message "All MongoDB dumps compressed. Sending archives to telegram and deleting uncompressed..." | |
# Message for archive mode | |
if [ $exit_status_archive -eq 0 ]; then | |
message_archive="MongoDB dump in archive mode created & sent on '$DateRn' successfully." | |
# Send archive file via Telegram bot | |
curl -s -X POST "https://api.telegram.org/bot$bot_token/sendDocument" -F chat_id="$chat_id" -F document=@"$backup_dir_main/${DateRn}_archive.7z" | |
# Remove temporary archive dir | |
rm -r $backup_dir_archive | |
else | |
message_archive="MongoDB dump in archive mode failed with exit status $exit_status_archive." | |
fi | |
send_message "$message_archive" | |
# Message for normal mode | |
if [ $exit_status_normal -eq 0 ]; then | |
message_normal="MongoDB dump in normal mode created & sent on '$DateRn' successfully." | |
# Send normal file via Telegram bot | |
curl -s -X POST "https://api.telegram.org/bot$bot_token/sendDocument" -F chat_id="$chat_id" -F document=@"$backup_dir_main/${DateRn}_normal.7z" | |
# Remove temporary normal file | |
rm -r $backup_dir_normal | |
else | |
message_normal="MongoDB dump in normal mode failed with exit status $exit_status_normal." | |
fi | |
send_message "$message_normal" | |
# Cleanup old backups and archives (keep only last 10 days) | |
deleted_directories=$(find ./Backups/* -type d -ctime +10 -exec echo {} \;) | |
find ./Backups/* -type d -ctime +10 -exec rm -r {} \; | |
# Message for deleted directories | |
if [ ! -z "$deleted_directories" ]; then | |
message_deleted="Deleted directories older than 10 days: $deleted_directories" | |
# Send message about deleted directories via Telegram bot | |
send_message "$message_deleted" | |
fi | |
# List existing directories | |
existing_directories=$(find ./Backups/* -type d -exec echo {} \;) | |
# Message for existing directories | |
if [ ! -z "$existing_directories" ]; then | |
message_existing="Existing directories: $existing_directories" | |
# Send message about existing directories via Telegram bot | |
send_message "$message_existing" | |
fi | |
# Capture the end time | |
end_time=$(date +%s) | |
# Calculate the duration | |
duration=$((end_time - start_time)) | |
# Send message about existing directories via Telegram bot | |
send_message "Cron job completed at $(date). Took: $duration seconds." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment