Merge pull request #455 from nextcloud/enh/47/auto-backup

add option to enable daily backups
This commit is contained in:
Simon L 2022-04-06 13:54:00 +02:00 committed by GitHub
commit 888d16d790
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 314 additions and 16 deletions

View file

@ -178,6 +178,7 @@ if [ "$BORG_MODE" = restore ]; then
if ! rsync --stats --archive --human-readable -vv --delete \
--exclude "nextcloud_aio_mastercontainer/session/"** \
--exclude "nextcloud_aio_mastercontainer/certs/"** \
--exclude "nextcloud_aio_mastercontainer/data/daily_backup_running" \
--exclude "nextcloud_aio_mastercontainer/data/configuration.json" \
/tmp/borg/nextcloud_aio_volumes/ /nextcloud_aio_volumes; then
echo "Something failed while restoring from backup."

View file

@ -26,6 +26,7 @@ RUN apt-get update; \
openssl \
sudo \
dpkg-dev \
netcat \
; \
rm -rf /var/lib/apt/lists/*
@ -84,10 +85,12 @@ RUN mkdir /var/log/supervisord; \
COPY Caddyfile /
COPY start.sh /usr/bin/
COPY backup-time-file-watcher.sh /
COPY cron.sh /
COPY supervisord.conf /
RUN chmod +x /usr/bin/start.sh; \
chmod +x /cron.sh
chmod +x /cron.sh; \
chmod +x /backup-time-file-watcher.sh
USER root

View file

@ -0,0 +1,26 @@
#!/bin/bash
restart_process() {
set -x
pkill cron.sh
set +x
}
file_present() {
if [ -f "/mnt/docker-aio-config/data/daily_backup_time" ]; then
if [ "$FILE_PRESENT" = 0 ]; then
restart_process
fi
FILE_PRESENT=1
else
if [ "$FILE_PRESENT" = 1 ]; then
restart_process
fi
FILE_PRESENT=0
fi
}
while true; do
file_present
sleep 5
done

View file

@ -1,12 +1,93 @@
#!/bin/sh
set -eux
#!/bin/bash
while true; do
if [ -f "/mnt/docker-aio-config/data/daily_backup_time" ]; then
set -x
BACKUP_TIME="$(cat "/mnt/docker-aio-config/data/daily_backup_time")"
DAILY_BACKUP=1
set +x
else
BACKUP_TIME="04:00"
DAILY_BACKUP=0
fi
if [ -f "/mnt/docker-aio-config/data/daily_backup_running" ]; then
LOCK_FILE_PRESENT=1
else
LOCK_FILE_PRESENT=0
fi
# Allow to continue directly if e.g. the mastercontainer was updated. Otherwise wait for the next execution
if [ "$LOCK_FILE_PRESENT" = 0 ]; then
while [ "$(date +%H:%M)" != "$BACKUP_TIME" ]; do
sleep 1
done
fi
if [ "$DAILY_BACKUP" = 1 ]; then
echo "Daily backup has started"
# Delete all active sessions and create a lock file
rm -f "/mnt/docker-aio-config/session/"*
sudo -u www-data touch "/mnt/docker-aio-config/data/daily_backup_running"
# Check if apache is running/stopped, watchtower is stopped and backupcontainer is stopped
APACHE_PORT="$(docker inspect nextcloud-aio-apache --format "{{.HostConfig.PortBindings}}" | grep -oP '[0-9]+' | head -1)"
while docker ps --format "{{.Names}}" | grep -q "^nextcloud-aio-apache$" && ! nc -z nextcloud-aio-apache "$APACHE_PORT"; do
echo "Waiting for apache to become available"
sleep 30
done
while docker ps --format "{{.Names}}" | grep -q "^nextcloud-aio-watchtower$"; do
echo "Waiting for watchtower to stop"
sleep 30
done
while docker ps --format "{{.Names}}" | grep -q "^nextcloud-aio-borgbackup$"; do
echo "Waiting for borgbackup to stop"
sleep 30
done
# Update the mastercontainer
sudo -u www-data php /var/www/docker-aio/php/src/Cron/UpdateMastercontainer.php
# Wait for watchtower to stop
if ! docker ps --format "{{.Names}}" | grep -q "^nextcloud-aio-watchtower$"; then
echo "Something seems to be wrong: Watchtower should be started at this step."
else
while docker ps --format "{{.Names}}" | grep -q "^nextcloud-aio-watchtower$"; do
echo "Waiting for watchtower to stop"
sleep 30
done
fi
# Execute the backup itself and some related tasks
sudo -u www-data php /var/www/docker-aio/php/src/Cron/DailyBackup.php
# Delete the lock file
rm -f "/mnt/docker-aio-config/data/daily_backup_running"
# Wait for the nextcloud container to start and send if the backup was successful
if ! docker ps --format "{{.Names}}" | grep -q "^nextcloud-aio-nextcloud$"; then
echo "Something seems to be wrong: Nextcloud should be started at this step."
else
while docker ps --format "{{.Names}}" | grep -q "^nextcloud-aio-nextcloud$" && ! nc -z nextcloud-aio-nextcloud 9000; do
echo "Waiting for the Nextcloud container to start"
sleep 30
done
fi
sudo -u www-data php /var/www/docker-aio/php/src/Cron/BackupNotification.php
echo "Daily backup has finished"
fi
# Make sure to delete the lock file always
rm -f "/mnt/docker-aio-config/data/daily_backup_running"
# Check for updates and send notification if yes
sudo -u www-data php /var/www/docker-aio/php/src/Cron/cron.php
# Remove dangling images
sudo -u www-data docker image prune -f
sudo -u www-data php /var/www/docker-aio/php/src/Cron/UpdateNotification.php
# Remove sessions older than 24h
find "/mnt/docker-aio-config/session/" -mindepth 1 -mmin +1440 -delete
sleep 1d
# Remove dangling images
sudo -u www-data docker image prune -f
done

View file

@ -28,3 +28,10 @@ stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
command=/cron.sh
[program:backup-time-file-watcher]
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
command=/backup-time-file-watcher.sh

View file

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
set -eu
while true; do

View file

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
# version_greater A B returns whether A > B
version_greater() {
@ -230,6 +230,11 @@ if ! [ -f "/mnt/ncdata/skip.update" ]; then
php /var/www/html/occ maintenance:mimetype:update-db
fi
fi
# Performing update of all apps if daily backups are enabled, running and successful
if [ "$DAILY_BACKUP_RUNNING" = 'yes' ]; then
php /var/www/html/occ app:update --all
fi
fi
# Check if appdata is present