-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathborg_backup_nextcloud.bash
executable file
·56 lines (50 loc) · 2.02 KB
/
borg_backup_nextcloud.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
##################################
### Based on https://www.c-rieger.de/backup-mit-de-duplizierung/
##################################
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
source "${SCRIPTPATH}/borg_backup_settings.bash"
startTime=$(date +%s)
currentDate=$(date --date @"$startTime" +"%Y%m%d_%H%M%S")
currentDateReadable=$(date --date @"$startTime" +"%d.%m.%Y - %H:%M:%S")
logDirectory="${LOG_DIR}"
logFile="${logDirectory}/${currentDate}.log"
backupDiscMount="${BACKUP_DEST_DIR}"
borgRepository="${backupDiscMount}/data"
borgBackupDirs="${DATA_DIR}"
dockerContainerName="ncp_container"
if [ ! -d "${logDirectory}" ]
then
mkdir -p "${logDirectory}"
fi
errorecho() { cat <<< "$@" 1>&2; }
exec > >(tee -i "${logFile}")
exec 2>&1
if [ "$(id -u)" != "0" ]
then
errorecho "ERROR: This script has to be run as root!"
exit 1
fi
echo -e "\n###### Starting the Backup: ${currentDateReadable} ######\n"
echo -e "maintenance mode is being activated"
sudo docker exec -u www-data ${dockerContainerName} php /var/www/nextcloud/occ maintenance:mode --on
echo -e "\nBackup in progress"
borg create --stats \
$borgRepository::"${currentDate}" \
$borgBackupDirs
echo
echo -e "maintenance mode is being deactivated"
sudo docker exec -u www-data ${dockerContainerName} php /var/www/nextcloud/occ maintenance:mode --off
borg prune --progress --stats $borgRepository --keep-within=7d --keep-weekly=2 --keep-monthly=6
borg compact $borgRepository
endTime=$(date +%s)
endDateReadable=$(date --date @"$endTime" +"%d.%m.%Y - %H:%M:%S")
duration=$((endTime-startTime))
durationSec=$((duration % 60))
durationMin=$(((duration / 60) % 60))
durationHour=$((duration / 3600))
durationReadable=$(printf "%02d hours %02d minutes %02d seconds" $durationHour $durationMin $durationSec)
echo -e "\n###### End of Backup: ${endDateReadable} (${durationReadable}) ######\n"
echo -e "backup disk usage:\n"
df -h ${backupDiscMount}
mail -s "Nextcloud backup finished" ${LOGGING_MAIL} -aFrom:${MAIL_SENDER} < "${logFile}"