Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Last active July 19, 2023 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpablocs/00e83b2847cf882715fb4442337206d7 to your computer and use it in GitHub Desktop.
Save juanpablocs/00e83b2847cf882715fb4442337206d7 to your computer and use it in GitHub Desktop.
mongo dump bash

Mongo dump bash

download all db with gzip mongo backup and delete backups old 10 days before

#!/bin/bash
BACKUP_DIR="$HOME/Backup/my-site"
DATE=$(date +"%Y%m%d")
BACKUP_FILE="$BACKUP_DIR/my-site_$DATE.gz"
DAYS_TO_KEEP=10
REMOTE_HOST="..."
REMOTE_PORT="27017"

# execute mongodump and compress with gzip
# mongodump --archive | gzip > $BACKUP_FILE
docker run --rm -v $BACKUP_DIR:/backup mongo mongodump --host $REMOTE_HOST --port $REMOTE_PORT --archive | gzip > $BACKUP_FILE

find "$BACKUP_DIR" -type f -mtime +$DAYS_TO_KEEP -print0 | while IFS= read -r -d '' file; do
    rm -f "$file"
done

echo "backup complete: $BACKUP_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment