masto-aio/ansible/roles/mastodon/templates/backup.sh

31 lines
941 B
Bash

#!/bin/bash
set -e
BUCKET={{s3_private_bucket_name}}
MAX_DAILY=2
DATE=$(date -I)
# copy the redis backup
aws s3 cp /srv/mastodon/redis/dump.rdb s3://${BUCKET}/backup_redis/dump-${DATE}.rdb
# push a postgres backup
docker exec -t mastodon_db pg_dumpall -c -U postgres | xz | aws s3 cp - s3://${BUCKET}/backup_postgres/dump-${DATE}.sql.xz
# rotate -- let's just keep 2
NUM_REDIS=$(aws s3 ls s3://${BUCKET}/backup_redis/ | wc -l)
NUM_POSTGRES=$(aws s3 ls s3://${BUCKET}/backup_postgres/ | wc -l)
while [[ $NUM_REDIS -gt $MAX_DAILY ]]; do
LAST=$(aws s3 ls s3://${BUCKET}/backup_redis/ | tail -1 | awk '{ print $4 }')
aws s3 rm s3://${BUCKET}/backup_redis/${LAST}
NUM_REDIS=$((NUM_REDIS - 1))
done
while [[ $NUM_POSTGRES -gt $MAX_DAILY ]]; do
LAST=$(aws s3 ls s3://${BUCKET}/backup_postgres/ | tail -1 | awk '{ print $4 }')
aws s3 rm s3://${BUCKET}/backup_postgres/${LAST}
NUM_POSTGRES=$((NUM_POSTGRES - 1))
done