From 2096b0434a2d8f439db392ec7888926418f99c83 Mon Sep 17 00:00:00 2001 From: swg Date: Wed, 29 Nov 2023 18:35:36 +0100 Subject: [PATCH] stub of cloud restore script and example of a cloud backup configuration. --- test/scripts/cloud_backup.conf | 27 ++++++++ test/scripts/cloud_restore.sh | 117 +++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 test/scripts/cloud_backup.conf create mode 100755 test/scripts/cloud_restore.sh diff --git a/test/scripts/cloud_backup.conf b/test/scripts/cloud_backup.conf new file mode 100644 index 0000000..bc88943 --- /dev/null +++ b/test/scripts/cloud_backup.conf @@ -0,0 +1,27 @@ +#!/bin/bash + +# Path to nextclouds data folder +CONFIG_NC_DATA="/srv/nextcloud-data" +# Path to the web root of the nextcloud +CONFIG_NC_BASE="/var/www/nextcloud" +# Folder for the log file - relative to nextcloud data folder! +CONFIG_NC_LOGPATH="admin/files/System/" +# Where to find the file with folders to exclude from backup +CONFIG_EXCLUDE=/home/cloudbackup/.config/cloud_backup.exclude +# Host of the database +CONFIG_NC_DB_SERVER=localhost +# Name of the nextcloud database +CONFIG_NC_DB_NAME=nextclouddb +# User for the nextcloud datbase +CONFIG_NC_DB_USER=nextclouduser +# Folders to backup with rsync +BACKUP_SOURCES=(/srv/nextcloud-data /var/www/nextcloud) + +# Target server for backup +TOSSH= +# port to use for ssh/rsync +SSHPORT=10110 +# User for the backup server +SSHUSER="cloudbackup" +# target folder on the backup server +BACKUP_TARGET="/srv/dev-disk-by-label-mirny/Backup/nubecula-minor/nextcloud" diff --git a/test/scripts/cloud_restore.sh b/test/scripts/cloud_restore.sh new file mode 100755 index 0000000..d38ec74 --- /dev/null +++ b/test/scripts/cloud_restore.sh @@ -0,0 +1,117 @@ +#!/bin/bash +## Restores the cloud configuration, data and database from backup done with cloud_backup. + +# Log levels for messages +LLMute=0 +LLMdtry=1 # mandatory: messages that are always written to the log (except mute) +LLError=2 +LLWarning=3 +LLInfo=4 +LLDebug=5 +## Actual log level of the logger used +Log_Level=$LLDebug + +## base name of the logging script. +Log_Name="${0##*/}"; + +## Write log of the script. +## $1 Loglevel to log the message with. +## $2 message to log. +function logger() { + local _logging_level=$1; + local _log_message=$2; + if [[ $Log_Level -lt $_logging_level ]]; then + return; + fi + local _now=$($DATE +%Y%m%d_%H:%M:%S); + if [[ $_logging_level -eq $LLInfo ]]; then + _type=" Info:"; + elif [[ $_logging_level -eq $LLError ]]; then + _type=" ERROR!"; + elif [[ $_logging_level -eq $LLWarning ]]; then + _type=" Warning:"; + elif [[ $_logging_level -eq $LLDebug ]]; then + _type=" DEBUG:"; + else + _type=":"; + fi + printf "$Log_Name ${_now}${_type} ${_log_message}\n" >> $Log_File; + if [[ $Log_Level -eq $LLDebug ]]; then + printf "${_now}${_type} ${_log_message}\n"; + fi +} + +## Cleanup and exit with exit value given. +function clean_up() { + local _exit_val=$1; + logger $LLInfo "Cleaning up on exit $_exit_val."; + + + logger $LLMdtry "Exiting on $_exit_val."; + exit $_exit_val; +} + +## Returns the binaries full path if existent. +function which_is() { + declare -n _ret_val=$1; # declare RETVAL a reference of $1 + local _executable=$2; + local _with_full_path=$(which ${_executable} 2>/dev/null); + if [ -x "$_with_full_path" ]; then + _ret_val="$_with_full_path"; + return 0; + else + _ret_val="$_executable not found!"; + return 1; + fi +} + +function create_folders() { + logger $LLDebug "CONFIG_NC_DATA: mkdir -p $CONFIG_NC_DATA"; + if [[ ! -n "$CONFIG_NC_DATA" ]]; then clean_up 1; fi + mkdir -p "$CONFIG_NC_DATA"; # Path to nextclouds data folder + + logger $LLDebug "CONFIG_NC_BASE: mkdir -p $CONFIG_NC_BASE"; + if [[ ! -n "$CONFIG_NC_BASE" ]]; then clean_up 1; fi + mkdir -p "$CONFIG_NC_BASE"; # Path to the web root of the nextcloud + + for _source in "$BACKUP_SOURCES"; do + logger $LLDebug "BACKUP_SOURCES: mkdir -p $_source"; + mkdir -p "$_source"; + done; + + logger $LLDebug "BACKUP_TARGET: mkdir -p $BACKUP_TARGET"; + if [[ ! -n "$BACKUP_TARGET" ]]; then clean_up 1; fi + mkdir -p "$BACKUP_TARGET"; # target folder on the backup server +} + + +CONFIG_NC_DB_SERVER=localhost # Host of the database +# Name of the nextcloud database +CONFIG_NC_DB_NAME=nextclouddb +# User for the nextcloud datbase +CONFIG_NC_DB_USER=nextclouduser +# Target server for backup +TOSSH= +# port to use for ssh/rsync +SSHPORT=10110 +# User for the backup server +SSHUSER="cloudbackup" + +which_is DATE 'date'; +if [ $? -ne 0 ]; then echo $DATE; exit 1; fi + +## Logfile in nextcloud to have a visible result. +Run_Date=$($DATE +%Y_%m_%d) +Log_File="/tmp/${Run_Date}_cloudrestore.log"; + +if [[ ! -f "./cloud_backup.conf" ]]; then + clean_up 1; +fi + +source ./cloud_backup.conf +create_folders; +if [[ $? -ne 0 ]]; then + clean_up 1; +fi + +clean_up 0;