Last active
November 12, 2025 03:10
-
-
Save shugo/76e5e97f8c8942f49a22755b673025f1 to your computer and use it in GitHub Desktop.
Backup Mastodon DB and media
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 | |
| set -euo pipefail | |
| # Configurable variables | |
| INSTANCE_DIR="/home/mastodon/live" | |
| SYSTEM_DIR="/home/mastodon/live/public/system" | |
| BACKUP_DIR="/home/mastodon/backup" | |
| BACKUP_LIFETIME_DAYS=30 | |
| DATE_FORMAT="%Y%m%d" | |
| DB_USER="mastodon" | |
| DB_NAME="mastodon_production" | |
| RCLONE_DB_DEST="r2:mastodon-db" | |
| RCLONE_MEDIA_DEST="r2:mastodon-media" | |
| # Error handling | |
| trap 'echo "An error occurred. Exiting." && exit 1' ERR | |
| backup_date="$(date +$DATE_FORMAT)" | |
| # Database backup | |
| echo -n "Backup DB... " | |
| DB_BACKUP_FILE="$BACKUP_DIR/db_$backup_date.sql.gz" | |
| pg_dump -Fc -U $DB_USER $DB_NAME | gzip -c >"$DB_BACKUP_FILE" | |
| find "$BACKUP_DIR" -mtime +$BACKUP_LIFETIME_DAYS -name "db_*.gz" -exec rm -f {} \; | |
| echo "Success!" | |
| # Sync backup | |
| echo -n "Backup to R2... " | |
| rclone copy --max-age 30h --no-traverse "$BACKUP_DIR" $RCLONE_DB_DEST | |
| rclone copy --max-age 30h --no-traverse --exclude="/cache/**" "$SYSTEM_DIR" $RCLONE_MEDIA_DEST | |
| echo "Success!" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://riq0h.jp/2023/07/22/204725/ を参考に一部修正