diff --git a/.gitignore b/.gitignore index 118a867ef1..69319e0402 100644 --- a/.gitignore +++ b/.gitignore @@ -77,3 +77,5 @@ Nginx/default.conf Certs/StatusPageCerts/*.crt Certs/StatusPageCerts/*.key + +Backups/*.backup diff --git a/Backups/README.md b/Backups/README.md new file mode 100644 index 0000000000..b5c95b9193 --- /dev/null +++ b/Backups/README.md @@ -0,0 +1 @@ +This is where all the database backups will be stored. \ No newline at end of file diff --git a/backup.sh b/backup.sh index f30a5585b2..ad05537c5f 100644 --- a/backup.sh +++ b/backup.sh @@ -1 +1,10 @@ -docker run -it --rm --entrypoint sh debug/ubuntu \ No newline at end of file +# This file takes last 30 days backup. Make sure you run this file atleast once/day. +# 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) + +sudo docker run -it --net=host --rm \ +--env-file config.env \ +--volume=$(pwd)$DATABASE_BACKUP_DIRECTORY:/var/lib/postgresql/data \ +postgres:latest /usr/bin/pg_dump -Fc --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).backup \ No newline at end of file diff --git a/config.tpl.env b/config.tpl.env index 10cc1d6f6a..5a15034103 100644 --- a/config.tpl.env +++ b/config.tpl.env @@ -101,4 +101,19 @@ SUBSCRIPTION_PLAN_BASIC=Basic,,,0,0,1,0 SUBSCRIPTION_PLAN_GROWTH=Growth,,,0,0,2,14 SUBSCRIPTION_PLAN_ENTERPRISE=Enterprise,,,-1,-1,3,14 -STATUS_PAGE_CNAME_RECORD=statuspage.oneuptime.com \ No newline at end of file +STATUS_PAGE_CNAME_RECORD=statuspage.oneuptime.com + + +DATABASE_BACKUP_DIRECTORY=/Backups +DATABASE_BACKUP_HOST=localhost +DATABASE_BACKUP_PORT=5400 +DATABASE_BACKUP_USERNAME=postgres +DATABASE_BACKUP_PASSWORD={{ .Env.DATABASE_PASSWORD }} + + +DATABASE_RESTORE_DIRECTORY=/Backups +DATABASE_RESTORE_HOST=localhost +DATABASE_RESTORE_PORT=5400 +DATABASE_RESTORE_USERNAME=postgres +DATABASE_RESTORE_PASSWORD={{ .Env.DATABASE_PASSWORD }} +DATABASE_RESTORE_FILENAME=db-12.backup \ No newline at end of file diff --git a/restore.sh b/restore.sh index e69de29bb2..3043dd1fda 100644 --- a/restore.sh +++ b/restore.sh @@ -0,0 +1,10 @@ +# This file takes last 30 days backup. Make sure you run this file atleast once/day. +# The backup will be in the format of db-(date of the month).backup +# Before the backup, please make sure DATABASE_RESTORE_* ENV vars in config.env is set properly. + +export $(grep -v '^#' config.env | xargs) + +sudo docker run -it --net=host --rm \ +--env-file config.env \ +--volume=$(pwd)$DATABASE_RESTORE_DIRECTORY:/var/lib/postgresql/data \ +postgres:latest /usr/bin/pg_restore --dbname=postgresql://$DATABASE_RESTORE_USERNAME:$DATABASE_RESTORE_PASSWORD@$DATABASE_RESTORE_HOST:$DATABASE_RESTORE_PORT/$DATABASE_RESTORE_NAME /var/lib/postgresql/data/$DATABASE_RESTORE_FILENAME