Last active
September 16, 2024 14:05
-
-
Save alighafoori/4e7ca93d4329a0066c82e44e77c094b3 to your computer and use it in GitHub Desktop.
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 | |
MONGO_CONTAINER_NAME="mongo" # Or use the actual container name | |
# Variables | |
BOT_TOKEN="<your bot token>" | |
CHAT_ID="<chatId>" | |
FILE_PATH="/mongodb_backups/waitlist_backup.archive" # Path where the archive will be stored on the host machine | |
FILE_PREFIX="part_" # Prefix for the split parts | |
PART_SIZE="49M" # Size of each part (49 MB to stay within Telegram bot limit) | |
# Dynamically get the container ID | |
MONGO_CONTAINER_ID=$(docker ps -qf "name=$MONGO_CONTAINER_NAME") | |
BACKUP_PATH="/backup" # Backup directory inside the container | |
# 0. Check if the backup directory exists inside the container, create it if it doesn't | |
docker exec $MONGO_CONTAINER_ID sh -c "[ ! -d \"$BACKUP_PATH\" ] && mkdir -p \"$BACKUP_PATH\"" | |
# 1. Create the MongoDB backup inside the container | |
docker exec $MONGO_CONTAINER_ID mongodump --db=<your database> --collection=<your collection> --archive=$BACKUP_PATH/waitlist_backup.archive --username <mongo username> --password <mongo password> --authenticationDatabase admin | |
# 2. Copy the backup archive from the container to the local machine | |
docker cp $MONGO_CONTAINER_ID:$BACKUP_PATH/waitlist_backup.archive $FILE_PATH | |
# 3. Split the file into smaller parts | |
echo "Splitting the file..." | |
split -b $PART_SIZE "$FILE_PATH" "$FILE_PREFIX" | |
# 4. Send each part through Telegram | |
echo "Sending parts to Telegram..." | |
for file in ${FILE_PREFIX}*; do | |
echo "Sending $file..." | |
curl -F chat_id="$CHAT_ID" -F document=@"$file" "https://api.telegram.org/bot$BOT_TOKEN/sendDocument" | |
sleep 2 # Add a delay to avoid hitting Telegram rate limits | |
done | |
echo "All parts sent successfully!" | |
# 5. Clean up: Delete the backup archive in the container | |
echo "Deleting backup archive in the container..." | |
docker exec $MONGO_CONTAINER_ID rm $BACKUP_PATH/waitlist_backup.archive | |
# 6. Clean up: Delete the backup archive and the split parts from the local machine | |
echo "Deleting backup files from the local machine..." | |
rm -f "$FILE_PATH" ${FILE_PREFIX}* | |
echo "Backup and cleanup completed!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment