From da0b3fd6532fc5581b2d9bdba8f65c04b6ed28fe Mon Sep 17 00:00:00 2001 From: Ricthofen Date: Tue, 15 Dec 2020 20:15:41 -0500 Subject: [PATCH 1/2] add shell scripts for timescale backup --- deployments/scripts/backup_timescale.sh | 28 +++++++++++++++++++++++++ deployments/scripts/create_nfs_mount.sh | 18 ++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 deployments/scripts/backup_timescale.sh create mode 100644 deployments/scripts/create_nfs_mount.sh diff --git a/deployments/scripts/backup_timescale.sh b/deployments/scripts/backup_timescale.sh new file mode 100644 index 00000000..8826545c --- /dev/null +++ b/deployments/scripts/backup_timescale.sh @@ -0,0 +1,28 @@ +#!/bin/bash +STR_DATE="$(date '+%Y_%m_%d')" +DIR_TIMESCALEDB="/data/backup/timescaledb" +FILE_BACKUP="$DIR_TIMESCALEDB/timescaledb_dump_$STR_DATE.sql" +CLEAN_CYCLE=5 + +# timescaledb info +PRODUCTION_DB_USER="$(cat /data/secrets/production/pss_db_user)" +PRODUCTION_DB_PASSWORD="$(cat /data/secrets/production/pss_db_password)" +PRODUCTION_DB_HOST="incrudibles-production.db.pdl.cmu.edu" +PRODUCTION_DB_PORT="32003" +PRODUCTION_DB_NAME="postgresql://$PRODUCTION_DB_USER:$PRODUCTION_DB_PASSWORD@$PRODUCTION_DB_HOST:$PRODUCTION_DB_PORT/pss_database?sslmode=disable" + +# install pg_dump if not installed +which pg_dump > /dev/null +if [ $? -ne 0 ]; then + sudo /usr/bin/apt update -y + sudo /usr/bin/apt install postgresql-client +fi + +# create the timescaledb backup dir if not exists +/bin/mkdir $DIR_TIMESCALEDB + +# dump the timescaledb +/usr/bin/pg_dump -d $PRODUCTION_DB_NAME -f $FILE_BACKUP + +# delete redundant outdated backups +/usr/bin/find $DIR_TIMESCALEDB/* -mtime +$CLEAN_CYCLE -delete diff --git a/deployments/scripts/create_nfs_mount.sh b/deployments/scripts/create_nfs_mount.sh new file mode 100644 index 00000000..7544e865 --- /dev/null +++ b/deployments/scripts/create_nfs_mount.sh @@ -0,0 +1,18 @@ +#!/bin/bash +REMOTE_DIR="basket.pdl.local.cmu.edu:/cmudb-testingteam-backup" +LOCAL_DIR="/data/backup" + +# install nfs-common if not installed +which mount > /dev/null +if [ $? -ne 0 ]; then + sudo apt update -y + sudo apt install nfs-common +fi + +# create the local backup dir if not exists +mkdir -p $LOCAL_DIR + +# mount the local backup dir to remote nfs if not mounted +if [ "$(stat -f -L -c %T $LOCAL_DIR)" != "nfs" ]; then + mount $REMOTE_DIR $LOCAL_DIR +fi From 6717021ea8ac53c8be7e43f7fc51572d56ac2266 Mon Sep 17 00:00:00 2001 From: Ricthofen Date: Tue, 15 Dec 2020 20:16:46 -0500 Subject: [PATCH 2/2] add comment for crontab --- deployments/scripts/backup_timescale.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/deployments/scripts/backup_timescale.sh b/deployments/scripts/backup_timescale.sh index 8826545c..44965ca4 100644 --- a/deployments/scripts/backup_timescale.sh +++ b/deployments/scripts/backup_timescale.sh @@ -1,4 +1,8 @@ #!/bin/bash + +# This script is run on daily basis as a cronjob +# 0 0 * * * /bin/bash /data/scripts/backup_timescale.sh > /dev/null + STR_DATE="$(date '+%Y_%m_%d')" DIR_TIMESCALEDB="/data/backup/timescaledb" FILE_BACKUP="$DIR_TIMESCALEDB/timescaledb_dump_$STR_DATE.sql"