2022-07-11 03:47:25 +08:00
#!/bin/bash
2022-08-31 20:00:37 +08:00
echo "Daily backup script has started"
# Daily backup and backup check cannot be run at the same time
if [ " $DAILY_BACKUP " = 1 ] && [ " $CHECK_BACKUP " = 1 ] ; then
echo "Daily backup and backup check cannot be run at the same time. Exiting..."
exit 1
fi
2022-07-11 03:47:25 +08:00
# Delete all active sessions and create a lock file
# But don't kick out the user if the mastercontainer was just updated since we block the interface either way with the lock file
2022-08-31 20:00:37 +08:00
if [ " $LOCK_FILE_PRESENT " = 0 ] || ! [ -f "/mnt/docker-aio-config/data/daily_backup_running" ] ; then
2023-02-12 02:30:02 +08:00
find "/mnt/docker-aio-config/session/" -mindepth 1 -delete
2022-07-11 03:47:25 +08:00
fi
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
2023-08-16 19:29:25 +08:00
APACHE_PORT = " $( docker inspect nextcloud-aio-apache --format "{{.HostConfig.PortBindings}}" | grep -o '[0-9]\+' | head -1) "
2022-07-11 03:47:25 +08:00
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
if [ " $AUTOMATIC_UPDATES " = 1 ] ; then
2022-08-31 20:00:37 +08:00
echo "Starting mastercontainer update..."
echo "(The script might get exited due to that. In order to update all the other containers correctly, you need to run this script with the same settings a second time.)"
2022-07-11 03:47:25 +08:00
sudo -u www-data php /var/www/docker-aio/php/src/Cron/UpdateMastercontainer.php
fi
# Wait for watchtower to stop
2023-02-20 06:21:04 +08:00
if [ " $AUTOMATIC_UPDATES " = 1 ] ; then
if ! docker ps --format "{{.Names}}" | grep -q " ^nextcloud-aio-watchtower $" ; then
echo "Something seems to be wrong: Watchtower should be started at this step."
fi
2022-07-11 03:47:25 +08:00
while docker ps --format "{{.Names}}" | grep -q " ^nextcloud-aio-watchtower $" ; do
echo "Waiting for watchtower to stop"
sleep 30
done
fi
# Stop containers if required
2022-08-31 20:00:37 +08:00
# shellcheck disable=SC2235
if [ " $CHECK_BACKUP " != 1 ] && ( [ " $DAILY_BACKUP " != 1 ] || [ " $STOP_CONTAINERS " = 1 ] ) ; then
echo "Stopping containers..."
2022-07-11 03:47:25 +08:00
sudo -u www-data php /var/www/docker-aio/php/src/Cron/StopContainers.php
fi
# Execute the backup itself and some related tasks (also stops the containers)
if [ " $DAILY_BACKUP " = 1 ] ; then
2022-08-31 20:00:37 +08:00
echo "Creating daily backup..."
2022-07-11 03:47:25 +08:00
sudo -u www-data php /var/www/docker-aio/php/src/Cron/CreateBackup.php
2023-02-20 06:21:04 +08:00
if ! docker ps --format "{{.Names}}" | grep -q " ^nextcloud-aio-borgbackup $" ; then
echo "Something seems to be wrong: the borg container should be started at this step."
fi
while docker ps --format "{{.Names}}" | grep -q " ^nextcloud-aio-borgbackup $" ; do
echo "Waiting for backup container to stop"
sleep 30
done
2022-07-11 03:47:25 +08:00
fi
2022-08-31 20:00:37 +08:00
# Execute backup check
if [ " $CHECK_BACKUP " = 1 ] ; then
echo "Starting backup check..."
sudo -u www-data php /var/www/docker-aio/php/src/Cron/CheckBackup.php
fi
2022-07-11 03:47:25 +08:00
# Start and/or update containers
if [ " $AUTOMATIC_UPDATES " = 1 ] ; then
2022-08-31 20:00:37 +08:00
echo "Starting and updating containers..."
2022-07-11 03:47:25 +08:00
sudo -u www-data php /var/www/docker-aio/php/src/Cron/StartAndUpdateContainers.php
else
if [ " $START_CONTAINERS " = 1 ] ; then
2022-08-31 20:00:37 +08:00
echo "Starting containers without updating them..."
2022-07-11 03:47:25 +08:00
sudo -u www-data php /var/www/docker-aio/php/src/Cron/StartContainers.php
fi
fi
# Delete the lock file
rm -f "/mnt/docker-aio-config/data/daily_backup_running"
2022-09-19 05:10:03 +08:00
# Send backup notification
# shellcheck disable=SC2235
if [ " $DAILY_BACKUP " = 1 ] && ( [ " $AUTOMATIC_UPDATES " = 1 ] || [ " $START_CONTAINERS " = 1 ] ) ; then
2022-07-11 03:47:25 +08:00
# 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
2022-08-15 20:00:48 +08:00
if [ " $( docker inspect nextcloud-aio-nextcloud --format "{{.State.Restarting}}" ) " = "true" ] ; then
echo "Nextcloud container restarting. Skipping this check!"
break
fi
2022-07-11 03:47:25 +08:00
done
fi
2022-08-31 20:00:37 +08:00
echo "Sending backup notification..."
2023-10-09 23:18:40 +08:00
sudo -E -u www-data php /var/www/docker-aio/php/src/Cron/BackupNotification.php
2022-07-11 03:47:25 +08:00
fi
2022-08-31 20:00:37 +08:00
echo "Daily backup script has finished"