fix script for weekly/monthly/yearly backups being overwritten

This commit is contained in:
Siddhartha 2024-01-03 15:38:45 +05:30
parent 4dc558a31a
commit 3afaf3bcd1
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,9 @@
#!/bin/bash
REPOSITORY="/Users/siddhartha/OneDrive - Karmani Service Technologies Pvt. Ltd/Backups/logseq-backup"
CURRENTDATETIME=`date +"%Y-%m-%d-%T"`
/usr/local/bin/borg create -v --stats --compression zstd,22 "$REPOSITORY"::$CURRENTDATETIME /Users/siddhartha/logseq
/usr/local/bin/borg prune -v --list "$REPOSITORY" --keep-within 6H --keep-daily=1

View File

@ -0,0 +1,52 @@
#!/bin/bash
# Function to purge old backups
purge_old_backups() {
# $1: frequency (daily, monthly, yearly)
# $2: maximum number of backups to keep
rclone --min-age $(($2 + 1))d delete backblaze:one800-backups/outline/backups/$1/
rclone --min-age $(($2 + 1))d rmdirs backblaze:one800-backups/outline/backups/$1/
}
# Get the current date components
CURRENT_YEAR=$(date +"%Y")
CURRENT_MONTH=$(date +"%m")
CURRENT_DAY=$(date +"%d")
CURRENT_WEEKDAY=$(date +"%u") # 1=Monday, 7=Sunday
# Outline API details for exporting all collections
OUTLINE_API_URL="https://outline.example.com/api/collections.export_all"
OUTLINE_API_KEY="super_secret_key"
# Export all collections and stream the output to rclone to upload to B2
# curl -s -X POST "${OUTLINE_API_URL}" -H "Authorization: Bearer ${OUTLINE_API_KEY}" -H "content-type: application/json"
# Backup outline database
docker exec -t outline_postgres pg_dumpall -c -U outline | \
rclone rcat backblaze:one800-backups/outline/backups/daily/outline-db-${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DAY}.sql
# Create weekly backups on Sunday
if [ "$CURRENT_WEEKDAY" -eq "7" ]; then
WEEK_NUMBER=$(date +"%V")
rclone copy backblaze:one800-backups/outline/backups/daily/outline-db-${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DAY}.sql backblaze:one800-backups/outline/backups/weekly/outline-db-${CURRENT_YEAR}-${CURRENT_MONTH}-W${WEEK_NUMBER}.sql
fi
# Create monthly and yearly backups on the 1st day of the month and year, respectively
if [ "$CURRENT_DAY" -eq "01" ]; then
MONTH_NAME=$(date +"%B")
rclone copy backblaze:one800-backups/outline/backups/daily/outline-db-${CURRENT_YEAR}-${CURRENT_MONTH}-01.sql backblaze:one800-backups/outline/backups/monthly/outline-db-${CURRENT_YEAR}-${MONTH_NAME}.sql
if [ "$CURRENT_MONTH" -eq "01" ]; then
YEAR_NAME=$(date +"%Y")
rclone copy backblaze:one800-backups/outline/backups/monthly/outline-db-${CURRENT_YEAR}-01-01.sql backblaze:one800-backups/outline/backups/yearly/outline-db-${YEAR_NAME}.sql
fi
fi
# Purge old backups
purge_old_backups "daily" 30
purge_old_backups "weekly" 5
purge_old_backups "monthly" 12
purge_old_backups "yearly" 5
echo "Backup and purge process completed."

View File

@ -0,0 +1,34 @@
#!/bin/bash
echo "Creating postgres database dumps..."
CURRENTDATETIME=`date +"%Y-%m-%d_%H_%M_%S"`
FILENAME_WITH_DIR="/home/ubuntu/backups/postgres/dump_"$CURRENTDATETIME
FILENAME="dump_"$CURRENTDATETIME
/usr/bin/docker exec -t postgres pg_dumpall -c -U one800 > $FILENAME_WITH_DIR.sql
if [ $? -eq 0 ]; then
echo "dump created successfully!"
else
echo "dump failed!"
fi
echo "Uploading to b2..."
/usr/local/bin/b2 upload-file one800-backups $FILENAME_WITH_DIR.sql postgres/db-dumps/$FILENAME.sql --noProgress --quiet
if [ $? -eq 0 ]; then
echo "dump uploaded to b2 successfully!"
echo "removing the local dump file..."
sleep 2
rm $FILENAME_WITH_DIR.sql
echo "removed the local dump file successfully!"
else
echo "dump upload to b2 failed!"
fi
# you can schedule this script to run every day at a certain hour via the following crontab entry
# 1 10 * * * /bin/bash /home/ubuntu/backups/scripts/postgres-dump-backup.sh >> /home/ubuntu/logs/cronlogs/postgres-dump-backup.log