Version 3.0 Add support for remote locations
This commit is contained in:
parent
d00af41ec4
commit
a621142148
18
tubs.config
18
tubs.config
@ -1,8 +1,24 @@
|
||||
# Date Formating
|
||||
DATE_FORMAT="+%Y-%m-%d_%H-%M"
|
||||
|
||||
# Backup Directorys
|
||||
BACKUP_SRC_ARRAY=("/home/janic/workspace/TUBS/TEST/SRC1" "/home/janic/workspace/TUBS/TEST/SRC2" "/home/janic/workspace/TUBS/TEST/SRC3" "/home/janic/workspace/TUBS/TEST/SRC4")
|
||||
BACKUP_DST="/home/janic/workspace/TUBS/TEST/DST"
|
||||
|
||||
# Prefixes
|
||||
BACKUP_PREFIX="backup_"
|
||||
RESTORE_PRFIX="restore_"
|
||||
|
||||
# RSYNC Arguments
|
||||
RSYNC_ARGS="-azP"
|
||||
|
||||
# Scripts to execute before & after
|
||||
PRE_BACKUP_SCRIPT=""
|
||||
POST_BACKUP_SCRIPT=""
|
||||
POST_BACKUP_SCRIPT=""
|
||||
|
||||
# SSH Remote Destination
|
||||
SSH_REMOTE_DST="YES"
|
||||
SSH_REMOTE_HOST_DST=""
|
||||
SSH_REMOTE_USER_DST=""
|
||||
SSH_REMOTE_PORT_DST=""
|
||||
SSH_REMOTE_KEY_DST=""
|
||||
|
124
tubs.sh
124
tubs.sh
@ -1,11 +1,11 @@
|
||||
#!/bin/bash
|
||||
#============================================================================
|
||||
#title : TUBS
|
||||
#description : The Ultimate Backup Script (Linux) Version 2
|
||||
#description : The Ultimate Backup Script (Linux) Version 3
|
||||
#author : Janic Voser Xelon
|
||||
#date : 2022-04-08
|
||||
#updated : 2022-04-09
|
||||
#version : 2.0
|
||||
#updated : 2022-07-04
|
||||
#version : 3.0
|
||||
#usage : chmod +x tubs.sh && ./tubs.sh
|
||||
#notes :
|
||||
#bash_version :
|
||||
@ -49,21 +49,42 @@ function load_config {
|
||||
}
|
||||
## Create destination Structure
|
||||
function create_dst {
|
||||
mkdir -p "$BACKUP_DST_FULL" || critical_error "Creating Directory BACKUP_DST_FULL"
|
||||
mkdir -p "$BACKUP_DST_INC" || critical_error "Creating Directory BACKUP_DST_INC"
|
||||
if [ "$SSH_REMOTE_DST" == "YES" ]
|
||||
then
|
||||
ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" mkdir -p "$BACKUP_DST_FULL" || critical_error "Creating Remote Directory BACKUP_DST_FULL"
|
||||
ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" mkdir -p "$BACKUP_DST_INC" || critical_error "Creating Remote Directory BACKUP_DST_INC"
|
||||
else
|
||||
mkdir -p "$BACKUP_DST_FULL" || critical_error "Creating Directory BACKUP_DST_FULL"
|
||||
mkdir -p "$BACKUP_DST_INC" || critical_error "Creating Directory BACKUP_DST_INC"
|
||||
fi
|
||||
}
|
||||
## Check destination
|
||||
function check_dst {
|
||||
if [ -e "$BACKUP_DST" ]
|
||||
if [ "$SSH_REMOTE_DST" == "YES" ]
|
||||
then
|
||||
blue "Backup destinantion existing"
|
||||
magenta "This script is currently not able to check if remote Destinations exist"
|
||||
if ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" [ -e "$BACKUP_DST" ]
|
||||
then
|
||||
blue "Remote Backup destinantion existing"
|
||||
else
|
||||
red "Remote Backup destination not existing"
|
||||
fi
|
||||
ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" mkdir -p "$BACKUP_DST_INC_PATH" || critical_error "Error while creating remote incremental backup-directory ($BACKUP_DST_INC_PATH)"
|
||||
ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" mkdir -p "$BACKUP_DST_FULL_LOGS" || critical_error "Error while creating remote log directory ($BACKUP_DST_FULL_LOGS)"
|
||||
else
|
||||
red "Backup destination not existing"
|
||||
if [ -e "$BACKUP_DST" ]
|
||||
then
|
||||
blue "Backup destinantion existing"
|
||||
else
|
||||
red "Backup destination not existing"
|
||||
fi
|
||||
mkdir -p "$BACKUP_DST_INC_PATH" || critical_error "Error while creating incremental backup-directory ($BACKUP_DST_INC_PATH)"
|
||||
mkdir -p "$BACKUP_DST_FULL_LOGS" || critical_error "Error while creating log directory ($BACKUP_DST_FULL_LOGS)"
|
||||
fi
|
||||
mkdir -p "$BACKUP_DST_INC_PATH" || critical_error "Error while creating incremental backup-directory ($BACKUP_DST_INC_PATH)"
|
||||
mkdir -p "$BACKUP_DST_FULL_LOGS" || critical_error "Error while creating log directory ($BACKUP_DST_FULL_LOGS)"
|
||||
|
||||
}
|
||||
## Check sources
|
||||
|
||||
## Check sources local
|
||||
function check_src {
|
||||
for src_dir in "${BACKUP_SRC_ARRAY[@]}"
|
||||
do
|
||||
@ -80,6 +101,16 @@ function check_src {
|
||||
}
|
||||
## Check Incremental Date
|
||||
function check_inc {
|
||||
if [ "$SSH_REMOTE_DST" == "YES" ]
|
||||
then
|
||||
check_inc_remote
|
||||
else
|
||||
check_inc_local
|
||||
fi
|
||||
BACKUP_DST_INC_PATH="$BACKUP_DST_INC/$BACKUP_PREFIX$LAST_BACKUP"
|
||||
}
|
||||
## Check Incremental Date
|
||||
function check_inc_local {
|
||||
if ! [ -e "$BACKUP_LAST_FILE" ]
|
||||
then
|
||||
magenta "Missing last successfull backup file, just using the day before"
|
||||
@ -92,7 +123,21 @@ function check_inc {
|
||||
LAST_BACKUP="$(date --date="yesterday" "$DATE_FORMAT")"
|
||||
fi
|
||||
fi
|
||||
BACKUP_DST_INC_PATH="$BACKUP_DST_INC/$BACKUP_PREFIX$LAST_BACKUP"
|
||||
}
|
||||
## Check Incremental Date
|
||||
function check_inc_remote {
|
||||
if ! ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" [ -e "$BACKUP_LAST_FILE" ]
|
||||
then
|
||||
magenta "Missing last successfull backup file, just using the day before"
|
||||
LAST_BACKUP="$(date --date="yesterday" "$DATE_FORMAT")"
|
||||
else
|
||||
LAST_BACKUP="$(ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" cat "$BACKUP_LAST_FILE")"
|
||||
if ! [[ $LAST_BACKUP =~ ^[0-9]{4}(-[0-9]{2}){2}_[0-9]{2}-[0-9]{2} ]]
|
||||
then
|
||||
magenta "Content of last successfull backup file was not valid"
|
||||
LAST_BACKUP="$(date --date="yesterday" "$DATE_FORMAT")"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
## Check Prerequirements
|
||||
function check_prereq {
|
||||
@ -101,6 +146,15 @@ function check_prereq {
|
||||
}
|
||||
## Last successful backup
|
||||
function last_backup {
|
||||
if [ "$SSH_REMOTE_DST" == "YES" ]
|
||||
then
|
||||
last_backup_remote
|
||||
else
|
||||
last_backup_local
|
||||
fi
|
||||
}
|
||||
### Last successful backup
|
||||
function last_backup_local {
|
||||
if touch "$BACKUP_LAST_FILE" &> /dev/null
|
||||
then
|
||||
echo "$BACKUP_DATE" > "$BACKUP_LAST_FILE"
|
||||
@ -108,8 +162,26 @@ function last_backup {
|
||||
critical_error "Can not write Last successful backup file"
|
||||
fi
|
||||
}
|
||||
### Last successful backup
|
||||
function last_backup_remote {
|
||||
if ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" touch "$BACKUP_LAST_FILE" &> /dev/null
|
||||
then
|
||||
echo "$BACKUP_DATE" | ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" -T "cat > $BACKUP_LAST_FILE"
|
||||
else
|
||||
critical_error "Can not write Last successful backup file"
|
||||
fi
|
||||
}
|
||||
## Backup all
|
||||
function backup_all {
|
||||
if [ "$SSH_REMOTE_DST" == "YES" ]
|
||||
then
|
||||
backup_all_remote
|
||||
else
|
||||
backup_all_local
|
||||
fi
|
||||
}
|
||||
## Backup all local
|
||||
function backup_all_local {
|
||||
blue "Starting Backup Processes"
|
||||
for src_dir in "${BACKUP_SRC_ARRAY[@]}"
|
||||
do
|
||||
@ -121,7 +193,7 @@ function backup_all {
|
||||
################################################################################
|
||||
|
||||
EOF
|
||||
rsync "$RSYNC_ARGS" --backup --delete --backup-dir="$BACKUP_DST_INC_PATH/" "$src_dir" "$BACKUP_DST_FULL/$src_dir_name" >> "$BACKUP_DST_FULL_LOGS/$src_dir_name.log" &
|
||||
rsync "$RSYNC_ARGS" --backup --delete --backup-dir="$BACKUP_DST_INC_PATH/" "$src_dir" "$BACKUP_DST_FULL" >> "$BACKUP_DST_FULL_LOGS/$src_dir_name.log" &
|
||||
done
|
||||
green "All backup processes started successfull"
|
||||
blue "Waiting for processes to finish"
|
||||
@ -133,6 +205,32 @@ EOF
|
||||
done
|
||||
green "All Processes finished"
|
||||
}
|
||||
## Backup all remote
|
||||
function backup_all_remote {
|
||||
blue "Starting Backup Processes"
|
||||
for src_dir in "${BACKUP_SRC_ARRAY[@]}"
|
||||
do
|
||||
src_dir_name="$(echo "$src_dir" | rev | cut -d / -f 1 | rev)"
|
||||
ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" -T "cat << EOF >> \"$BACKUP_DST_FULL_LOGS/$src_dir_name.log\"
|
||||
|
||||
################################################################################
|
||||
#############################TUBS $BACKUP_DATE##############################
|
||||
################################################################################
|
||||
|
||||
EOF
|
||||
"
|
||||
rsync "$RSYNC_ARGS" -e "ssh -p $SSH_REMOTE_PORT_DST -i $SSH_REMOTE_KEY_DST" --backup --delete --backup-dir="$BACKUP_DST_INC_PATH/" "$src_dir" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST:$BACKUP_DST_FULL" | ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" -T "cat >> \"$BACKUP_DST_FULL_LOGS/$src_dir_name.log\"" &
|
||||
done
|
||||
green "All backup processes started successfull"
|
||||
blue "Waiting for processes to finish"
|
||||
# wait for all pids
|
||||
jobs=$(ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" jobs -p)
|
||||
for process in $jobs
|
||||
do
|
||||
ssh -i "$SSH_REMOTE_KEY_DST" -p "$SSH_REMOTE_PORT_DST" "$SSH_REMOTE_USER_DST@$SSH_REMOTE_HOST_DST" wait "$process" || critical_error "Process: $process failed"
|
||||
done
|
||||
green "All Processes finished"
|
||||
}
|
||||
## Pre Backup Script
|
||||
function pre_backup_script {
|
||||
if [ -e "$PRE_BACKUP_SCRIPT" ]
|
||||
|
Loading…
Reference in New Issue
Block a user