1
mirror of https://github.com/jakejarvis/mastodon-utils.git synced 2025-06-27 17:25:42 -04:00

keep weekly and monthly backups (and rotate old ones)

This commit is contained in:
2023-01-07 12:18:36 -05:00
parent 6461633e18
commit 352a57c1dc
7 changed files with 75 additions and 52 deletions

View File

@ -20,16 +20,12 @@ if [ "$(systemctl is-active mastodon-web.service)" = "active" ]; then
echo ""
fi
if [ ! -d "$BACKUPS_ROOT" ]; then
sudo mkdir -p "$BACKUPS_ROOT"
fi
# TODO: ugly, do better
# TODO: unsafe & ugly, do better.
TEMP_DIR=$(sudo mktemp -d)
sudo chmod 777 "$TEMP_DIR"
echo "* Backing up Postgres..."
sudo -u postgres pg_dump -Fc mastodon_production -f "$TEMP_DIR/postgres.dump"
sudo -Hiu postgres pg_dump -Fc mastodon_production -f "$TEMP_DIR/postgres.dump"
echo "* Backing up Redis..."
sudo cp /var/lib/redis/dump.rdb "$TEMP_DIR/redis.rdb"
@ -42,22 +38,48 @@ sudo mkdir -p "$TEMP_DIR/certs"
sudo cp -r /etc/letsencrypt/{archive,live,renewal} "$TEMP_DIR/certs/"
echo "* Compressing..."
ARCHIVE_DEST="$BACKUPS_ROOT/mastodon-$(date "+%Y.%m.%d-%H.%M.%S").tar.gz"
sudo tar --owner=0 --group=0 -czvf "$ARCHIVE_DEST" -C "$TEMP_DIR" .
sudo chown "$MASTODON_USER":"$MASTODON_USER" "$ARCHIVE_DEST"
TEMP_ARCHIVE="$(sudo mktemp)"
sudo tar --owner=0 --group=0 -czvf "$TEMP_ARCHIVE" -C "$TEMP_DIR" .
sudo mkdir -p "$BACKUPS_ROOT"/{daily,weekly,monthly}
ARCHIVE_FILENAME="mastodon-$(date "+%Y.%m.%d").tar.gz"
# weekly backup (every Sunday)
if [ "$(date +"%u")" -eq 7 ]; then
WEEKLY_DEST="$BACKUPS_ROOT/weekly/$ARCHIVE_FILENAME"
sudo cp -f "$TEMP_ARCHIVE" "$WEEKLY_DEST"
echo "* Saved weekly backup to '$WEEKLY_DEST'"
fi
# monthly backup (first day of the month)
if [ "$(date +"%d")" -eq 1 ]; then
MONTHLY_DEST="$BACKUPS_ROOT/monthly/$ARCHIVE_FILENAME"
sudo cp -f "$TEMP_ARCHIVE" "$MONTHLY_DEST"
echo "* Saved monthly backup to '$MONTHLY_DEST'"
fi
# daily backup (always)
DAILY_DEST="$BACKUPS_ROOT/daily/$ARCHIVE_FILENAME"
sudo cp -f "$TEMP_ARCHIVE" "$DAILY_DEST"
echo "* Saved daily backup to '$DAILY_DEST'"
echo "* Fixing permissions..."
sudo chown -R "$MASTODON_USER":"$MASTODON_USER" "$BACKUPS_ROOT"
echo "* Rotating old backups..."
# NOTE: keep all monthly backups for now
# keep last 4 weekly backups
find "$BACKUPS_ROOT/weekly" -mindepth 1 -type f -mtime +3 -delete
# keep last 5 daily backups
find "$BACKUPS_ROOT/daily" -mindepth 1 -type f -mtime +4 -delete
# sync backups dir with s3 bucket if s3cmd is installed & BACKUP_S3_BUCKET env var is set
# https://www.linode.com/docs/products/storage/object-storage/guides/s3cmd/
if [ -n "${BACKUP_S3_BUCKET:+x}" ] && command -v s3cmd >/dev/null 2>&1; then
echo "* Uploading to S3..."
sudo s3cmd sync --delete-removed "$BACKUPS_ROOT/" "s3://$BACKUP_S3_BUCKET" || :
else
echo "⚠ Skipping S3 upload; check that 's3cmd' is present in \$PATH, and \$BACKUP_S3_BUCKET is set."
fi
echo "* Removing temp files..."
sudo rm -rf --preserve-root "$TEMP_DIR"
echo "* Saved archive to '$ARCHIVE_DEST'"
if [ -s /usr/local/bin/linode-cli ]; then
echo "* Uploading to S3..."
sudo /usr/local/bin/linode-cli obj put "$ARCHIVE_DEST" jarvis-backup
fi
echo "* Fixing permissions..."
sudo chown -R "$MASTODON_USER":"$MASTODON_USER" "$BACKUPS_ROOT"
echo "* 🎉 done! (keep this archive safe!)"

View File

@ -3,17 +3,17 @@
# exit when any step fails
set -euo pipefail
# initialize paths (and silence warnings about things not existing yet because that's why we're running the installer.)
# shellcheck disable=SC1091
. "$(dirname "${BASH_SOURCE[0]}")"/../init.sh >/dev/null
# can't say you weren't warned :)
if [ "$MY_NAME_IS_JAKE_JARVIS" != "pinky promise" ]; then
if [ "${MY_NAME_IS_JAKE_JARVIS:=}" != "pinky promise" ]; then
echo "🚨 LISTEN UP!!!! YOU PROBABLY WANT THIS SCRIPT INSTEAD:"
echo "https://github.com/jakejarvis/mastodon-installer/blob/main/install.sh"
exit 69
fi
# initialize paths (and silence warnings about things not existing yet because that's why we're running the installer.)
# shellcheck disable=SC1091
. "$(dirname "${BASH_SOURCE[0]}")"/../init.sh >/dev/null
# check for existing installation
if [ -d "$APP_ROOT" ]; then
echo "$APP_ROOT already exists. Are you sure Mastodon isn't already installed?"
@ -288,7 +288,7 @@ as_mastodon touch "$LOGS_ROOT"/cron.log
(
sudo crontab -l
echo -e "\n$INSTALLER_WUZ_HERE
@weekly bash -c \"$UTILS_ROOT/scripts/weekly_cleanup.sh >> $LOGS_ROOT/cron.log 2>&1\"
@weekly bash -c \"$UTILS_ROOT/scripts/purge.sh >> $LOGS_ROOT/cron.log 2>&1\"
@weekly bash -c \"$UTILS_ROOT/scripts/backup.sh >> $LOGS_ROOT/cron.log 2>&1\"
# automatically renew Let's Encrypt certificates

View File

@ -2,12 +2,12 @@
# cronjob ran once per week at 3 AM on Sunday; see https://crontab.guru/#0_3_*_*_0
# syntax for crontab -e:
# 0 3 * * 0 bash -c "/home/mastodon/utils/scripts/weekly_cleanup.sh >> /home/mastodon/logs/cron.log 2>&1"
# 0 3 * * 0 bash -c "/home/mastodon/utils/scripts/purge.sh >> /home/mastodon/logs/cron.log 2>&1"
# exit when any step fails
set -o pipefail
echo -e "\n===== weekly_cleanup.sh: started at $(date '+%Y-%m-%d %H:%M:%S') =====\n"
echo -e "\n===== purge.sh: started at $(date '+%Y-%m-%d %H:%M:%S') =====\n"
# initialize paths
# shellcheck disable=SC1091
@ -17,4 +17,4 @@ tootctl media remove --days 14
tootctl media remove --prune-profiles --days 90
tootctl preview_cards remove --days 90
echo -e "\n===== weekly_cleanup.sh: finished at $(date '+%Y-%m-%d %H:%M:%S') =====\n"
echo -e "\n===== purge.sh: finished at $(date '+%Y-%m-%d %H:%M:%S') =====\n"

View File

@ -3,17 +3,17 @@
# exit when any step fails
set -euo pipefail
# initialize paths
# shellcheck disable=SC1091
. "$(dirname "${BASH_SOURCE[0]}")"/../init.sh
# can't say you weren't warned :)
if [ "$MY_NAME_IS_JAKE_JARVIS" != "pinky promise" ]; then
if [ "${MY_NAME_IS_JAKE_JARVIS:=}" != "pinky promise" ]; then
echo "🚨 LISTEN UP!!!! YOU PROBABLY WANT THIS SCRIPT INSTEAD:"
echo "https://github.com/jakejarvis/mastodon-installer/blob/main/upgrade.sh"
exit 69
fi
# initialize paths
# shellcheck disable=SC1091
. "$(dirname "${BASH_SOURCE[0]}")"/../init.sh
# pull latest mastodon source
cd "$APP_ROOT"
as_mastodon git fetch --all
@ -43,7 +43,7 @@ as_mastodon nvm install
as_mastodon nvm use
as_mastodon npm install --global yarn
# install deps
as_mastodon bundle install --jobs "$(getconf _NPROCESSORS_ONLN)"
as_mastodon bundle check || as_mastodon bundle install --jobs "$(getconf _NPROCESSORS_ONLN)"
as_mastodon yarn install --pure-lockfile
# compile new assets