oneuptime/backup.sh

24 lines
1.2 KiB
Bash
Raw Normal View History

2023-07-31 00:48:48 +00:00
# This file takes last 30 days backup. Make sure you run this file at least once/day.
2022-12-12 09:55:30 +00:00
# The backup will be in the format of db-(date of the month).backup
# Before the backup, please make sure DATABASE_BACKUP_* ENV vars in config.env is set properly.
export $(grep -v '^#' config.env | xargs)
2022-12-30 14:01:49 +00:00
echo "Starting backup...."
2022-12-31 11:24:00 +00:00
git pull
2023-07-30 20:43:44 +00:00
# Backup as SQL, CANNOT be used with pg_restore.
2022-12-31 11:23:17 +00:00
sudo docker run --net=host --rm \
2022-12-12 09:55:30 +00:00
--env-file config.env \
--volume=$(pwd)$DATABASE_BACKUP_DIRECTORY:/var/lib/postgresql/data \
2023-01-31 09:40:57 +00:00
postgres:latest /usr/bin/pg_dump --format=plain --clean --create --dbname=postgresql://$DATABASE_BACKUP_USERNAME:$DATABASE_BACKUP_PASSWORD@$DATABASE_BACKUP_HOST:$DATABASE_BACKUP_PORT/$DATABASE_BACKUP_NAME --file=/var/lib/postgresql/data/db-$(date +%d).sql
# Backup as Tar can be used with pg_restore
sudo docker run --net=host --rm \
--env-file config.env \
--volume=$(pwd)$DATABASE_BACKUP_DIRECTORY:/var/lib/postgresql/data \
postgres:latest /usr/bin/pg_dump --format=tar --clean --create --dbname=postgresql://$DATABASE_BACKUP_USERNAME:$DATABASE_BACKUP_PASSWORD@$DATABASE_BACKUP_HOST:$DATABASE_BACKUP_PORT/$DATABASE_BACKUP_NAME --file=/var/lib/postgresql/data/db-$(date +%d).tar
2022-12-30 14:01:49 +00:00
echo "Backup completed successfully!"