Version 3.0 Add support for remote locations
This commit is contained in:
parent
d00af41ec4
commit
a621142148
16
tubs.config
16
tubs.config
@ -1,8 +1,24 @@
|
|||||||
|
# Date Formating
|
||||||
DATE_FORMAT="+%Y-%m-%d_%H-%M"
|
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_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"
|
BACKUP_DST="/home/janic/workspace/TUBS/TEST/DST"
|
||||||
|
|
||||||
|
# Prefixes
|
||||||
BACKUP_PREFIX="backup_"
|
BACKUP_PREFIX="backup_"
|
||||||
RESTORE_PRFIX="restore_"
|
RESTORE_PRFIX="restore_"
|
||||||
|
|
||||||
|
# RSYNC Arguments
|
||||||
RSYNC_ARGS="-azP"
|
RSYNC_ARGS="-azP"
|
||||||
|
|
||||||
|
# Scripts to execute before & after
|
||||||
PRE_BACKUP_SCRIPT=""
|
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=""
|
||||||
|
110
tubs.sh
110
tubs.sh
@ -1,11 +1,11 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
#============================================================================
|
#============================================================================
|
||||||
#title : TUBS
|
#title : TUBS
|
||||||
#description : The Ultimate Backup Script (Linux) Version 2
|
#description : The Ultimate Backup Script (Linux) Version 3
|
||||||
#author : Janic Voser Xelon
|
#author : Janic Voser Xelon
|
||||||
#date : 2022-04-08
|
#date : 2022-04-08
|
||||||
#updated : 2022-04-09
|
#updated : 2022-07-04
|
||||||
#version : 2.0
|
#version : 3.0
|
||||||
#usage : chmod +x tubs.sh && ./tubs.sh
|
#usage : chmod +x tubs.sh && ./tubs.sh
|
||||||
#notes :
|
#notes :
|
||||||
#bash_version :
|
#bash_version :
|
||||||
@ -49,11 +49,29 @@ function load_config {
|
|||||||
}
|
}
|
||||||
## Create destination Structure
|
## Create destination Structure
|
||||||
function create_dst {
|
function create_dst {
|
||||||
|
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_FULL" || critical_error "Creating Directory BACKUP_DST_FULL"
|
||||||
mkdir -p "$BACKUP_DST_INC" || critical_error "Creating Directory BACKUP_DST_INC"
|
mkdir -p "$BACKUP_DST_INC" || critical_error "Creating Directory BACKUP_DST_INC"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
## Check destination
|
## Check destination
|
||||||
function check_dst {
|
function check_dst {
|
||||||
|
if [ "$SSH_REMOTE_DST" == "YES" ]
|
||||||
|
then
|
||||||
|
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
|
||||||
if [ -e "$BACKUP_DST" ]
|
if [ -e "$BACKUP_DST" ]
|
||||||
then
|
then
|
||||||
blue "Backup destinantion existing"
|
blue "Backup destinantion existing"
|
||||||
@ -62,8 +80,11 @@ function check_dst {
|
|||||||
fi
|
fi
|
||||||
mkdir -p "$BACKUP_DST_INC_PATH" || critical_error "Error while creating incremental backup-directory ($BACKUP_DST_INC_PATH)"
|
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)"
|
mkdir -p "$BACKUP_DST_FULL_LOGS" || critical_error "Error while creating log directory ($BACKUP_DST_FULL_LOGS)"
|
||||||
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
## Check sources
|
|
||||||
|
## Check sources local
|
||||||
function check_src {
|
function check_src {
|
||||||
for src_dir in "${BACKUP_SRC_ARRAY[@]}"
|
for src_dir in "${BACKUP_SRC_ARRAY[@]}"
|
||||||
do
|
do
|
||||||
@ -80,6 +101,16 @@ function check_src {
|
|||||||
}
|
}
|
||||||
## Check Incremental Date
|
## Check Incremental Date
|
||||||
function check_inc {
|
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" ]
|
if ! [ -e "$BACKUP_LAST_FILE" ]
|
||||||
then
|
then
|
||||||
magenta "Missing last successfull backup file, just using the day before"
|
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")"
|
LAST_BACKUP="$(date --date="yesterday" "$DATE_FORMAT")"
|
||||||
fi
|
fi
|
||||||
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
|
## Check Prerequirements
|
||||||
function check_prereq {
|
function check_prereq {
|
||||||
@ -101,6 +146,15 @@ function check_prereq {
|
|||||||
}
|
}
|
||||||
## Last successful backup
|
## Last successful backup
|
||||||
function last_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
|
if touch "$BACKUP_LAST_FILE" &> /dev/null
|
||||||
then
|
then
|
||||||
echo "$BACKUP_DATE" > "$BACKUP_LAST_FILE"
|
echo "$BACKUP_DATE" > "$BACKUP_LAST_FILE"
|
||||||
@ -108,8 +162,26 @@ function last_backup {
|
|||||||
critical_error "Can not write Last successful backup file"
|
critical_error "Can not write Last successful backup file"
|
||||||
fi
|
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
|
## Backup all
|
||||||
function 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"
|
blue "Starting Backup Processes"
|
||||||
for src_dir in "${BACKUP_SRC_ARRAY[@]}"
|
for src_dir in "${BACKUP_SRC_ARRAY[@]}"
|
||||||
do
|
do
|
||||||
@ -121,7 +193,7 @@ function backup_all {
|
|||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
EOF
|
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
|
done
|
||||||
green "All backup processes started successfull"
|
green "All backup processes started successfull"
|
||||||
blue "Waiting for processes to finish"
|
blue "Waiting for processes to finish"
|
||||||
@ -133,6 +205,32 @@ EOF
|
|||||||
done
|
done
|
||||||
green "All Processes finished"
|
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
|
## Pre Backup Script
|
||||||
function pre_backup_script {
|
function pre_backup_script {
|
||||||
if [ -e "$PRE_BACKUP_SCRIPT" ]
|
if [ -e "$PRE_BACKUP_SCRIPT" ]
|
||||||
|
Loading…
Reference in New Issue
Block a user