Added DB backup and restore

This commit is contained in:
Jonathan Ervine 2020-08-17 22:31:12 +08:00
parent e6f7c4c595
commit 2439655437
4 changed files with 34 additions and 13 deletions

View File

@ -1,21 +1,20 @@
# Base on latest (edge) alpine image # Base on latest (edge) alpine image
FROM harbor.ervine.dev/public/x86_64/alpine:v3.12 FROM harbor.ervine.dev/public/x86_64/alpine:v3.12
MAINTAINER “Jonathan Ervine” <docker@ervine.org> LABEL "Maintainer Jonathan Ervine <docker@ervine.org>"
# Install updates # Install updates
ENV LANG='en_US.UTF-8' \ ENV LANG='en_US.UTF-8' \
LANGUAGE='en_US.UTF-8' \ LANGUAGE='en_US.UTF-8' \
TERM='xterm' \ TERM='xterm'
VERSION='develop'
RUN echo http://dl-4.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories && \ RUN echo http://dl-4.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories && \
apk -U update && \ apk -U update && \
apk -U upgrade --ignore alpine-baselayout && \ 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 /tmp/src && \
rm -rf /var/cache/apk/* rm -rf /var/cache/apk/*
COPY start.sh /usr/local/bin/start.sh COPY start.sh /usr/local/bin/start.sh
CMD [ "/usr/local/bin/start.sh" ] CMD [ "/usr/local/bin/start.sh", "$LIVE_DB", "$BACXKUP_DB" ]

11
db-backup.py Normal file
View File

@ -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()

11
db-restore.py Normal file
View File

@ -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()

View File

@ -2,18 +2,18 @@
## ##
## Script to automate the copy of Sonarr databases ## 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' echo -e 'Starting the sidecar container to periodically backup the sonarr databases'
chown 1003:1003 /mnt/app-local-config chown 1003:1003 /mnt/app-local-config
chown 1003:1003 /app-remote-config/* chown 1003:1003 /app-remote-config/*
echo -e 'Copy the config from the remote share to the ramdisk' echo -e 'Copy the config from the remote share to the locali drive'
cp -fvp /app-remote-config/*.* /mnt/app-local-config touch /mnt/app-local-config/$LIVE_DB
echo -e 'Set up the filesystem freeze, copy, unfreeze loop' 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 while true; do
sleep 890 sleep 890
date date
sync /mnt/app-local-config/*.* python3 /app-remote-config/db-backup.py $LIVE_DB $BACKUP_DB
fsfreeze --freeze /mnt/app-local-config
sleep 10
cp -fvp /mnt/app-local-config/*.* /app-remote-config/
fsfreeze --unfreeze /mnt/app-local-config
done done