Last active
December 23, 2022 17:17
-
-
Save Siphonay/03ca0c64b5d65a886921ebdbd53651a9 to your computer and use it in GitHub Desktop.
donphan.social DB backup script
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 | |
backup_folder="${HOME}/backup" | |
# Finding the latest backup | |
latest_backup=$(find "${backup_folder}" -type f -name "donphan_social_db_backup_*" | head -1) | |
# If latest backup exists, check if enough space | |
if [ -n "${latest_backup}" ] | |
then | |
latest_backup_size=$(stat -c "%s" "${latest_backup}") | |
remaining_space=$(df -B1 "${backup_folder}" | tail -1 | cut -d' ' -f 9) | |
if [ "${remaining_space}" -lt "${latest_backup_size}" ] | |
then | |
>&2 echo "ERROR: NOT ENOUGH FREE SPACE ON DEVICE!!!" | |
exit 2 | |
fi | |
else | |
>&2 echo "WARNING: no previous backup found" | |
fi | |
# Perform backup & check if it succeeded, delete any partial backups | |
backup_time=$(date +%Y%m%d%H%M%S) | |
echo "Backing up the database…" | |
if ! pg_dump -Z 9 -Fc -U mastodon -d mastodon_production > "${backup_folder}/donphan_social_db_backup_${backup_time}" | |
then | |
>&2 echo "ERROR PERFORMING BACKUP!!!" | |
rm -f "${backup_folder}/donphan_social_db_backup_${backup_time}" | |
exit 1 | |
fi | |
echo "Uploading backup to BackBlaze…" | |
if ! b2 upload-file --noProgress donphan-social-db-backup "${backup_folder}/donphan_social_db_backup_${backup_time}" "donphan_social_db_backup_${backup_time}" | |
then | |
>&2 echo "ERROR UPLOADING BACKUP TO BACKBLAZE!!!" | |
exit 3 | |
fi | |
# Delete backups older than 14 days | |
if ! find "${backup_folder}" -type f -mtime +14 -name "donphan_social_db_backup_*" -delete | |
then | |
>&2 echo "ERROR DELETING BACKUPS OLDER THAN 14 DAYS!!!" | |
exit 4 | |
fi | |
echo "All done." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment