Skip to content

Instantly share code, notes, and snippets.

@Xetera
Created March 15, 2021 18:34
Show Gist options
  • Save Xetera/0092f7c21cd9301fa4132b34e7d5f49a to your computer and use it in GitHub Desktop.
Save Xetera/0092f7c21cd9301fa4132b34e7d5f49a to your computer and use it in GitHub Desktop.
Backing up mongodb with restic to an S3 bucket
#!/usr/bin/env bash
# This backup file is run daily as a cronjob like:
# 0 0 * * * bash ~/backup.sh >> /var/log/restic_backups.log 2>&1
restic_repository="your-s3-repository-url-here"
# There is probably a way to do this conveniently through stdin but I don't know if
# that's more efficient through a file because mongodb does some questionable stuff
# with piping mongodump to stdout vs saving backups to a file
backup_path="/path/to/emit/backup/file"
mongodb_uri="your-mongodb-connection-string-here"
mongodump --uri "$mongodb_uri" --archive="$backup_path" || exit 1
# backup target file
restic -r "$restic_repository" backup "$backup_path" --tag backup --verbose
# forget everything older than 1 month to save space
restic -r "$restic_repository" forget --tag backup --keep-within=1m
# remove dummy backup file
rm "$backup_path"
printf "Finished backing up %s\n\n" "$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment