From 2439655437cea3e2832e4baf657938ada70df8bf Mon Sep 17 00:00:00 2001 From: Jonathan Ervine Date: Mon, 17 Aug 2020 22:31:12 +0800 Subject: [PATCH] Added DB backup and restore --- Dockerfile | 9 ++++----- db-backup.py | 11 +++++++++++ db-restore.py | 11 +++++++++++ start.sh | 16 ++++++++-------- 4 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 db-backup.py create mode 100644 db-restore.py diff --git a/Dockerfile b/Dockerfile index 8865c7b..4b281ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,20 @@ # Base on latest (edge) alpine image FROM harbor.ervine.dev/public/x86_64/alpine:v3.12 -MAINTAINER “Jonathan Ervine” +LABEL "Maintainer Jonathan Ervine " # Install updates ENV LANG='en_US.UTF-8' \ LANGUAGE='en_US.UTF-8' \ - TERM='xterm' \ - VERSION='develop' + TERM='xterm' RUN echo http://dl-4.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories && \ apk -U update && \ apk -U upgrade --ignore alpine-baselayout && \ - apk -U add nfs-utils e2fsprogs util-linux&& \ + apk -U add nfs-utils e2fsprogs util-linux python3 && \ rm -rf /tmp/src && \ rm -rf /var/cache/apk/* COPY start.sh /usr/local/bin/start.sh -CMD [ "/usr/local/bin/start.sh" ] +CMD [ "/usr/local/bin/start.sh", "$LIVE_DB", "$BACXKUP_DB" ] diff --git a/db-backup.py b/db-backup.py new file mode 100644 index 0000000..8f17709 --- /dev/null +++ b/db-backup.py @@ -0,0 +1,11 @@ +import sys, sqlite3 + +def progress(status, remaining, total): + print(f'Copied {total-remaining} of {total} pages...') + +con = sqlite3.connect('sys.argv[1]') +bck = sqlite3.connect('sys.argv[2]') +with bck: + con.backup(bck, pages=1, progress=progress) +bck.close() +con.close() diff --git a/db-restore.py b/db-restore.py new file mode 100644 index 0000000..e9134f8 --- /dev/null +++ b/db-restore.py @@ -0,0 +1,11 @@ +import sys, sqlite3 + +def progress(status, remaining, total): + print(f'Copied {total-remaining} of {total} pages...') + +con = sqlite3.connect('sys.argv[2]') +bck = sqlite3.connect('sys.argv[1]') +with bck: + con.backup(bck, pages=1, progress=progress) +bck.close() +con.close() diff --git a/start.sh b/start.sh index 919c847..0587171 100755 --- a/start.sh +++ b/start.sh @@ -2,18 +2,18 @@ ## ## Script to automate the copy of Sonarr databases ## +LIVE_DB=$1 +BACKUP_DB=$2 echo -e 'Starting the sidecar container to periodically backup the sonarr databases' chown 1003:1003 /mnt/app-local-config chown 1003:1003 /app-remote-config/* -echo -e 'Copy the config from the remote share to the ramdisk' -cp -fvp /app-remote-config/*.* /mnt/app-local-config -echo -e 'Set up the filesystem freeze, copy, unfreeze loop' +echo -e 'Copy the config from the remote share to the locali drive' +touch /mnt/app-local-config/$LIVE_DB +python3 /app-remote-config/db-restore.py $BACKUP_DB $LIVE_DB +find /app-remote-config/ -type -f -not -iname $BACKUP_DB -exec cp -fvp '{}' '/mnt/app-local-config/{}' ';' +echo -e 'Perform periodic backup of database' while true; do sleep 890 date - sync /mnt/app-local-config/*.* - fsfreeze --freeze /mnt/app-local-config - sleep 10 - cp -fvp /mnt/app-local-config/*.* /app-remote-config/ - fsfreeze --unfreeze /mnt/app-local-config + python3 /app-remote-config/db-backup.py $LIVE_DB $BACKUP_DB done