]> git.parisson.com Git - docker-django-scripts.git/commitdiff
back to one dump only, copy scripts for incremental dump method
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Mon, 3 May 2021 11:11:52 +0000 (13:11 +0200)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Mon, 3 May 2021 11:11:52 +0000 (13:11 +0200)
dev/local/restore_db.sh
dev/local/restore_db_inc.sh [new file with mode: 0644]
prod/local/backup_db.sh
prod/local/backup_db_inc.sh [new file with mode: 0644]

index 1360fa6cdd2de23a208cda1790850508334134db..711da1564b07784be9493947e26a70cdd55c6de4 100755 (executable)
@@ -7,16 +7,6 @@ set -e
 
 echo "Restoring..."
 
-# Migrate backup to date-based format
-if [ -f /srv/backup/postgres.dump ]; then
-    echo 'A backup without date was found. Moving it to postgres_old.dump'
-    mv /srv/backup/postgres.dump /srv/backup/postgres_old.dump
-    if [ ! -f /srv/backup/postgres_latest.dump ]; then
-      cd /srv/backup
-      ln -s postgres_old.dump postgres_latest.dump
-    fi
-fi
-
 # import database functions of type
 if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
     gunzip < /srv/backup/mysql.dump.gz | mysql -h db $MYSQL_DATABASE -uroot -p$MYSQL_ROOT_PASSWORD
@@ -29,7 +19,7 @@ elif [ ! -z "$POSTGRES_PASSWORD" ]; then
     echo "Creating new db..."
     createdb -hdb -Upostgres -T template0 postgres
     echo "Importing dump..."
-    pg_restore -C -c -hdb -Upostgres -dpostgres /srv/backup/${1:-postgres_latest.dump}
+    pg_restore -C -c -hdb -Upostgres -dpostgres /srv/backup/postgres.dump
 fi
 
 echo "Restore done!"
diff --git a/dev/local/restore_db_inc.sh b/dev/local/restore_db_inc.sh
new file mode 100644 (file)
index 0000000..1360fa6
--- /dev/null
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+/srv/bin/misc/wait-for-it/wait-for-it.sh -h db -p $DB_PORT;
+
+# Stop execution if some command fails
+set -e
+
+echo "Restoring..."
+
+# Migrate backup to date-based format
+if [ -f /srv/backup/postgres.dump ]; then
+    echo 'A backup without date was found. Moving it to postgres_old.dump'
+    mv /srv/backup/postgres.dump /srv/backup/postgres_old.dump
+    if [ ! -f /srv/backup/postgres_latest.dump ]; then
+      cd /srv/backup
+      ln -s postgres_old.dump postgres_latest.dump
+    fi
+fi
+
+# import database functions of type
+if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
+    gunzip < /srv/backup/mysql.dump.gz | mysql -h db $MYSQL_DATABASE -uroot -p$MYSQL_ROOT_PASSWORD
+elif [ ! -z "$POSTGRES_PASSWORD" ]; then
+    export PGPASSWORD=$POSTGRES_PASSWORD
+    echo "Killing clients..."
+    psql -hdb -Upostgres -dpostgres -c "SELECT pid, (SELECT pg_terminate_backend(pid)) as killed from pg_stat_activity WHERE state LIKE 'idle';"
+    echo "Dropping db..."
+    dropdb -hdb -Upostgres postgres
+    echo "Creating new db..."
+    createdb -hdb -Upostgres -T template0 postgres
+    echo "Importing dump..."
+    pg_restore -C -c -hdb -Upostgres -dpostgres /srv/backup/${1:-postgres_latest.dump}
+fi
+
+echo "Restore done!"
index c6727de248081d8b104ae2b2a5134077b9ff65e8..07c38c3e3fbf9072c67c79f729635decc43e420c 100755 (executable)
@@ -7,26 +7,12 @@
 # Stop execution if some command fails
 set -e
 
-# Migrate backup to date-based format
-if [ -f /srv/backup/postgres.dump ]; then
-    echo 'A backup without date was found. Moving it to postgres_old.dump'
-    mv /srv/backup/postgres.dump /srv/backup/postgres_old.dump
-    if [ ! -f /srv/backup/postgres_latest.dump ]; then
-      cd /srv/backup
-      ln -s postgres_old.dump postgres_latest.dump
-    fi
-fi
-
 if [ ! -z "$MYSQL_PASSWORD" ]; then
     export MYSQL_PWD=$MYSQL_PASSWORD
     mysqldump $MYSQL_DATABASE -hdb -u$MYSQL_USER | gzip > /srv/backup/mysql.dump.gz
 elif [ ! -z "$POSTGRES_PASSWORD" ]; then
     export PGPASSWORD=$POSTGRES_PASSWORD
-    now=$(date +"%m_%d_%Y_%H_%M_%S")
-    pg_dump -Fc -hdb -Upostgres -dpostgres > /srv/backup/postgres_$now.dump
-    cd /srv/backup
-    rm -f postgres_latest.dump
-    ln -s postgres_$now.dump postgres_latest.dump
+    pg_dump -Fc -hdb -Upostgres -dpostgres > /srv/backup/postgres.dump
 fi
 
 echo "Backup done!"
diff --git a/prod/local/backup_db_inc.sh b/prod/local/backup_db_inc.sh
new file mode 100644 (file)
index 0000000..c6727de
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+# dump postgres or mysql functions of the database
+# export POSTGRES="$(dpkg --get-selections  | grep postgres 2>&1)"
+# export MYSQL="$(dpkg --get-selections  | grep mysql 2>&1)"
+
+# Stop execution if some command fails
+set -e
+
+# Migrate backup to date-based format
+if [ -f /srv/backup/postgres.dump ]; then
+    echo 'A backup without date was found. Moving it to postgres_old.dump'
+    mv /srv/backup/postgres.dump /srv/backup/postgres_old.dump
+    if [ ! -f /srv/backup/postgres_latest.dump ]; then
+      cd /srv/backup
+      ln -s postgres_old.dump postgres_latest.dump
+    fi
+fi
+
+if [ ! -z "$MYSQL_PASSWORD" ]; then
+    export MYSQL_PWD=$MYSQL_PASSWORD
+    mysqldump $MYSQL_DATABASE -hdb -u$MYSQL_USER | gzip > /srv/backup/mysql.dump.gz
+elif [ ! -z "$POSTGRES_PASSWORD" ]; then
+    export PGPASSWORD=$POSTGRES_PASSWORD
+    now=$(date +"%m_%d_%Y_%H_%M_%S")
+    pg_dump -Fc -hdb -Upostgres -dpostgres > /srv/backup/postgres_$now.dump
+    cd /srv/backup
+    rm -f postgres_latest.dump
+    ln -s postgres_$now.dump postgres_latest.dump
+fi
+
+echo "Backup done!"