data backup cleanup
This commit is contained in:
72
scripts/database-backup/db_backup_script.sh
Normal file
72
scripts/database-backup/db_backup_script.sh
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Cron job check the logs
|
||||
|
||||
## Executing the script
|
||||
# bash db_backup_script.sh
|
||||
|
||||
## command used to back up
|
||||
# docker exec DOCKER_CONTAINER pg_dump -U postgres -Fc DATABASE_NAME > BACKUP_FILE.DUMP
|
||||
|
||||
## command used to restore
|
||||
# directory where the backup is saved in in the docker container: /var/lib/postgresql/data
|
||||
# for example: docker exec postgres pg_restore -U postgres -d vikunja-backup /var/lib/postgresql/data/backup/vikunja-2024-01-31.dump
|
||||
# docker exec DOCKER_CONTAINER pg_restore -U postgres -d DATABASE_NAME /var/lib/postgresql/data/backup/BACKUP_FILE.DUMP
|
||||
|
||||
# Date format
|
||||
YEAR=$(date +"%Y")
|
||||
MONTH=$(date +"%m")
|
||||
DAY=$(date +"%d")
|
||||
NOW=$(date +"%Y-%m-%d")
|
||||
|
||||
BACKUP_DIRECTORY="/home/debian/docker/compose/project/db/postgres/data"
|
||||
|
||||
# Docker container with DB to backup
|
||||
DB_1="postgres"
|
||||
DB_2="postgres-with-pg-vector"
|
||||
|
||||
back_up_db() {
|
||||
|
||||
# sql to list all databases
|
||||
DATABASE_NAME=$(docker exec $1 psql -U postgres -t -c 'SELECT datname FROM pg_database WHERE datistemplate = false;')
|
||||
|
||||
# pg_dump command
|
||||
PGDUMP="docker exec $1 pg_dump -U postgres -Fc"
|
||||
|
||||
for i in $DATABASE_NAME; do
|
||||
|
||||
# ignoring postgres db
|
||||
if [[ "$i" != "postgres" ]]; then
|
||||
|
||||
echo "Backing up database $i"
|
||||
# backup path to file
|
||||
BACKFILE="$BACKUP_DIRECTORY/backup/$i-$NOW.dump"
|
||||
$PGDUMP $i > $BACKFILE
|
||||
|
||||
fi
|
||||
|
||||
done
|
||||
}
|
||||
|
||||
clean_up_backups() {
|
||||
|
||||
echo "Looking for dumps to prune in folder: $BACKUP_DIRECTORY/backup/"
|
||||
|
||||
# keep one backup per year, month and last two days
|
||||
BACKUP_YEAR="*-$YEAR-02-01.dump"
|
||||
BACKUP_MONTH="*-$YEAR-$MONTH-01.dump"
|
||||
BACKUP_DAY="*-$YEAR-$MONTH-$DAY.dump"
|
||||
BACKUP_DAY_1="*-$YEAR-$MONTH-$((DAY-1)).dump"
|
||||
FILE_TO_DELETE=$(find $BACKUP_DIRECTORY/backup/ -type f \( ! -name $BACKUP_YEAR -a ! -name $BACKUP_MONTH -a ! -name $BACKUP_DAY -a ! -name $BACKUP_DAY_1 \))
|
||||
|
||||
# delete such files
|
||||
for i in $FILE_TO_DELETE; do
|
||||
echo "Pruning $i"
|
||||
rm $i
|
||||
done
|
||||
}
|
||||
|
||||
back_up_db $DB_1
|
||||
back_up_db $DB_2
|
||||
|
||||
clean_up_backups
|
||||
25
scripts/database-backup/overleaf_backup_script.sh
Normal file
25
scripts/database-backup/overleaf_backup_script.sh
Normal file
@@ -0,0 +1,25 @@
|
||||
# Date format
|
||||
NOW=$(date +"%Y-%m-%d")
|
||||
|
||||
OVERLEAF_DIRECTORY="/home/debian/docker/compose/project/service/overleaf-toolkit"
|
||||
|
||||
# Backup config
|
||||
$OVERLEAF_DIRECTORY/bin/backup-config -m tar $OVERLEAF_DIRECTORY/backup/$NOW-overleaf-config-backup.tar
|
||||
# Backup mongo
|
||||
tar --create --file $OVERLEAF_DIRECTORY/backup/$NOW-overleaf-mongo-backup.tar $OVERLEAF_DIRECTORY/data/mongo
|
||||
# Backup sharelatex
|
||||
tar --create --file $OVERLEAF_DIRECTORY/backup/$NOW-overleaf-sharelatex-backup.tar $OVERLEAF_DIRECTORY/data/sharelatex
|
||||
|
||||
# Pruning
|
||||
clean_up_backups() {
|
||||
|
||||
# list all files older than 3 days
|
||||
FILE_TO_DELETE=$(find $OVERLEAF_DIRECTORY/backup/ -type f -mtime 3)
|
||||
|
||||
# delete such files
|
||||
for i in $FILE_TO_DELETE; do
|
||||
rm $i
|
||||
done
|
||||
}
|
||||
|
||||
clean_up_backups
|
||||
Reference in New Issue
Block a user