Skip to content

Commit

Permalink
only use borg
Browse files Browse the repository at this point in the history
  • Loading branch information
mortbauer committed Mar 5, 2024
1 parent 1ee1bde commit 9076c8f
Showing 1 changed file with 52 additions and 37 deletions.
89 changes: 52 additions & 37 deletions create-backup
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ function send_error_email () {
trap send_error_email ERR

destination=/backup_storage
remote_destination=/backup_storage_remote

echo "Removing old backups..."
echo "$(date "+%m-%d-%Y %T"): Removing old backups..."

find $destination/daily -maxdepth 1 -mtime +7 -type f -exec rm -v {} \;
find $destination/weekly -maxdepth 1 -mtime +31 -type f -exec rm -v {} \;
find $destination/monthly -maxdepth 1 -mtime +365 -type f -exec rm -v {} \;


# dump the postgres databases
echo "Starting to dump postgresql databases"
echo "$(date "+%m-%d-%Y %T"): Starting to dump postgresql databases"
mkdir -p /backup/postgresql_dbs
pg_dumpall --globals-only -Oxf /backup/postgresql_dbs/globals.sql
databases=$(psql -U postgres -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d')
Expand All @@ -38,7 +39,7 @@ for db_name in $databases; do
done

# dump mysql dbs
echo "Starting to dump mariadb databases"
echo "$(date "+%m-%d-%Y %T"): Starting to dump mariadb databases"
mkdir -p /backup/mysql_dbs
for db_name in $(mysql -h"$MYSQLDUMP_HOST" -u"$MYSQLDUMP_USER" -p"$MYSQLDUMP_PASSWORD" -e "show databases;" | tail -n +2);
do
Expand All @@ -55,7 +56,7 @@ do
global_exit=$(( dump_single_exit > global_exit ? dump_single_exit : global_exit ))
done

echo "Creating redis dump"
echo "$(date "+%m-%d-%Y %T"): Creating redis dump"
redis-cli -h redis save
mkdir -p /backup/redis
cp /redis_data/dump.rdb /backup/redis/
Expand All @@ -75,50 +76,64 @@ cp /redis_data/dump.rdb /backup/redis/
#
# pg_dumpall -Oxvf /backup/postgres_dump.sql

echo "Compressing files..."

dir=daily

if [ `date +%d` -eq 1 ]; then
dir=monthly
elif [ `date +%w` -eq 0 ]; then
dir=weekly
fi

filename=$destination/$dir/backup_$(date +%Y-%m-%d).tar.gz

mkdir -p $destination/$dir
tar -zcpf $filename /backup

echo "Wrote file to $filename"

remote_destination=/backup_storage_remote

echo "Removing old remote backups..."

find $remote_destination/daily -maxdepth 1 -mtime +7 -type f -exec rm -v {} \;
find $remote_destination/weekly -maxdepth 1 -mtime +31 -type f -exec rm -v {} \;
find $remote_destination/monthly -maxdepth 1 -mtime +365 -type f -exec rm -v {} \;

mkdir -p $remote_destination/$dir
# echo "$(date "+%m-%d-%Y %T"): Compressing files..."
#
# dir=daily
#
# if [ `date +%d` -eq 1 ]; then
# dir=monthly
# elif [ `date +%w` -eq 0 ]; then
# dir=weekly
# fi
#
# filename=$destination/$dir/backup_$(date +%Y-%m-%d).tar.gz
#
# mkdir -p $destination/$dir
# tar -zcpf $filename /backup
#
# echo "$(date "+%m-%d-%Y %T"): Wrote file to $filename"
#
# echo "$(date "+%m-%d-%Y %T"): Removing old remote backups..."
#
# find $remote_destination/daily -maxdepth 1 -mtime +7 -type f -exec rm -v {} \;
# find $remote_destination/weekly -maxdepth 1 -mtime +31 -type f -exec rm -v {} \;
# find $remote_destination/monthly -maxdepth 1 -mtime +365 -type f -exec rm -v {} \;
#
# mkdir -p $remote_destination/$dir
#
# echo "$(date "+%m-%d-%Y %T"): Copy backup to remote"
#
# cp $filename $remote_destination/$dir/
#
# echo "$(date "+%m-%d-%Y %T"): Copied backup to remote"
#
echo "$(date "+%m-%d-%Y %T"): Create borg backup"
export BORG_PASSPHRASE="$(cat /run/secrets/borg_passphrase)"

echo "Copy backup to remote"
export BORG_REPO=/backup_storage/borgrepo

cp $filename $remote_destination/$dir/
borg create --compression lz4 ::'{hostname}-{now}' /backup \
--exclude /backup/nextcloud_data/nextcloud.log

echo "Copied backup to remote"
echo "$(date "+%m-%d-%Y %T"): Prune old borg backup"
borg prune \
--global-archives '{hostname}-*' \
--keep-daily 6 \
--keep-weekly 3 \
--keep-monthly 5 \

echo "Create borg backup"
echo "$(date "+%m-%d-%Y %T"): Create remote borg backup"

export BORG_PASSPHRASE="$(cat /run/secrets/borg_passphrase)"
export BORG_REPO=/backup_storage_remote/borgrepo

borg create --compression lz4 ::'{hostname}-{now}' /backup \
--exclude /backup/nextcloud_data/nextcloud.log

echo "Prune old borg backup"
echo "$(date "+%m-%d-%Y %T"): Prune remote old borg backup"
borg prune \
--prefix '{hostname}-' \
--global-archives '{hostname}-*' \
--keep-daily 6 \
--keep-weekly 3 \
--keep-monthly 5 \

echo "$(date "+%m-%d-%Y %T"): Backup finished"

0 comments on commit 9076c8f

Please sign in to comment.