#!/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;