492 lines
15 KiB
Plaintext
492 lines
15 KiB
Plaintext
|
|
## --------------------------- ##
|
|
## - - ##
|
|
## - !! DON'T CHANGE THIS !! - ##
|
|
## - - ##
|
|
## --------------------------- ##
|
|
|
|
destHost=localhost
|
|
|
|
## - where to store backuped data
|
|
## -
|
|
script_backup_dir=$backup_base_dir/$srcHost
|
|
backup_archiv_dir=$script_backup_dir/archive
|
|
backup_mirror_dir=$script_backup_dir/mirror
|
|
|
|
## - where to find backup scripts
|
|
## -
|
|
script_dir=$hosts_base_dir/scripts
|
|
|
|
## - All backuped directories will be written to that file, to
|
|
## - avoid multiple execution.
|
|
## -
|
|
dirs_backup_done="$script_backup_dir/backup_dirs.lst"
|
|
|
|
export destHost script_backup_dir backup_archiv_dir backup_mirror_dir script_dir dirs_backup_done
|
|
|
|
#
|
|
# ------------------ Ende Variable ------------------ #
|
|
# --------------------------------------------------- #
|
|
|
|
## - Works even if "ssh_hopping" is not set
|
|
## -
|
|
if [ "X$ssh_hopping" = "X" ] ; then
|
|
ssh_hopping=false
|
|
fi
|
|
## - Works even if "ssh_tunnel" is not set
|
|
## -
|
|
if [ "X$ssh_tunnel" = "X" ] ; then
|
|
ssh_tunnel=false
|
|
fi
|
|
|
|
if ! $ssh_hopping && ! $ssh_tunnel ; then
|
|
|
|
_via_ssh_tunnel=false
|
|
export _via_ssh_tunnel
|
|
|
|
progArgs="$rsync_progArgs -a -p --delete"
|
|
|
|
else
|
|
|
|
## - create ssh-tunnel localhost -> $hop_host -> $target_host
|
|
## -
|
|
progArgs="-a -p -e ssh --delete"
|
|
target_host=$srcHost
|
|
srcHost=localhost
|
|
|
|
_via_ssh_tunnel=true
|
|
export _via_ssh_tunnel
|
|
|
|
## - establish ssh tunnel
|
|
## -
|
|
if $ssh_hopping ; then
|
|
|
|
_ssh_keyfile_arg=""
|
|
if [ "X$ssh_keyfile" != "X" ]; then
|
|
_ssh_keyfile_arg=" -i $ssh_keyfile"
|
|
fi
|
|
|
|
## - delete existing ssh tunnels
|
|
## -
|
|
tunnel_pid_old=`ps x | grep -e "$local_port:[^:]*:$target_port" | grep -v grep | awk '{print$1}'`
|
|
|
|
if [ -n "$tunnel_pid_old" ]; then
|
|
kill $tunnel_pid_old
|
|
fi
|
|
|
|
$ssh -fN -p $hop_host_port $_ssh_keyfile_arg \
|
|
-l $ssh_user \
|
|
-L $local_port:$target_host:$target_port \
|
|
$ssh_hop_host 2> /dev/null
|
|
|
|
tunnel_pid=`ps x | grep -e "$local_port:$target_host:$target_port" | grep -v grep | awk '{print$1}'`
|
|
|
|
else
|
|
if [ "X$ssh_tunnel_target_port" = "X" ]; then
|
|
ssh_tunnel_target_port=22
|
|
fi
|
|
_ssh_keyfile_arg=""
|
|
if [ "X$ssh_tunnel_key_file" != "X" ]; then
|
|
_ssh_keyfile_arg="-i $ssh_tunnel_key_file"
|
|
fi
|
|
|
|
## - delete existing ssh tunnels
|
|
## -
|
|
tunnel_pid_old=`ps x | grep -e "$ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port" | grep -v grep | awk '{print$1}'`
|
|
|
|
if [ -n "$tunnel_pid_old" ]; then
|
|
kill $tunnel_pid_old
|
|
fi
|
|
|
|
$ssh -fN -p $ssh_tunnel_target_port $_ssh_keyfile_arg \
|
|
-l $ssh_user \
|
|
-L $ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port \
|
|
$target_host 2> /dev/null
|
|
|
|
tunnel_pid=`ps x | grep -e "$ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port" | grep -v grep | awk '{print$1}'`
|
|
fi
|
|
|
|
fi
|
|
|
|
logArgs="--log-format"
|
|
formats=""
|
|
|
|
|
|
backup_dirs=""
|
|
for dir in $dir_backup ; do
|
|
backup_dirs="$backup_dirs $dir"
|
|
done
|
|
|
|
found=true
|
|
if [ "X$backup_dirs" = "X" ]; then
|
|
found=false
|
|
fi
|
|
|
|
## - Be compartible with older host files, which are missing variables concerning
|
|
## - backup nextcloud accounts.
|
|
## -
|
|
[[ -z "$nextcloud_backup" ]] && nextcloud_backup=false
|
|
|
|
if [ "$found" = "true" -o "$pgsql_backup" = "true" -o "$mysql_backup" = "true" -o "$disksetting_backup" = "true" -o "$nextcloud_backup" = "true" ] ; then
|
|
|
|
begin_h=`date +%H`
|
|
begin_m=`date +%M`
|
|
backup_date=`date +"%d.%m.%Y"`
|
|
backup_begin_timestamp=`date +"%s"`
|
|
|
|
if $_via_ssh_tunnel ; then
|
|
echolog "\nBegin saving host \"$target_host\" : $backup_date ( ${begin_h}:${begin_m} h )\n"
|
|
else
|
|
echolog "\nBegin saving host \"$srcHost\" : $backup_date ( ${begin_h}:${begin_m} h )\n"
|
|
fi
|
|
|
|
if ! $found; then
|
|
echolog "[ Notice ]: No directories for backup given..\n"
|
|
fi
|
|
|
|
## - BACKUP ..
|
|
## -
|
|
|
|
## - we want run the scripts in a subshell, so we export needed environment
|
|
## -
|
|
export srcHost progArgs logArgs formats backup_dirs
|
|
|
|
|
|
if $ARCHIVE ;then
|
|
|
|
if $ONLY_BACKUP ; then
|
|
|
|
echolog "\n\tOnly saving backups was requested - (ONLY_BACKUP=true)."
|
|
|
|
else
|
|
|
|
echolog "\nGoing to handle archives.. ( `$date +%H`:`$date +%M` h )"
|
|
_done=false
|
|
|
|
if [ ! -d $backup_archiv_dir ]; then
|
|
mkdir -p $backup_archiv_dir;
|
|
fi
|
|
|
|
## - remove backups older then $days
|
|
## -
|
|
archive_dirs=`find $backup_archiv_dir -maxdepth 1 -mindepth 1 -type d -name "Backup_*"`
|
|
_today=`date --date "now" +"%Y-%m-%d"`
|
|
_timestamp_today=`date --date "$_today" "+%s"`
|
|
for _dir in $archive_dirs ; do
|
|
_bakup_day=`cat $_dir/BACKUP-DATE`
|
|
_backup_timestamp=`date --date "$_bakup_day" +"%s"`
|
|
_days=$(echo "scale=0;($_timestamp_today-$_backup_timestamp)/86400" | bc)
|
|
if [ $_days -gt $days ]; then
|
|
echolog "\n\tRemoving backup from $_bakup_day.. ( `$date +%H`:`$date +%M` h )"
|
|
|
|
## - begin timestamp
|
|
## -
|
|
b_timestamp=`$date +"%s"`
|
|
|
|
rm -rf $_dir
|
|
retval=$?
|
|
|
|
## - end timestamp
|
|
## -
|
|
e_timestamp=`$date +"%s"`
|
|
|
|
## - determin duration
|
|
## -
|
|
_time=`expr $e_timestamp - $b_timestamp`
|
|
t_h=`expr $_time / 60 / 60`
|
|
t_rest_h=`expr $_time - $t_h \\* 60 \\* 60`
|
|
t_m=`expr $t_rest_h / 60`
|
|
t_s=`expr $t_rest_h - $t_m \\* 60`
|
|
duration=""
|
|
if [ $t_h -gt 0 ]; then
|
|
duration="$t_h h : $t_m min : $t_s sec"
|
|
elif [ $t_m -gt 0 ];then
|
|
duration="$t_m min : $t_s sec"
|
|
else
|
|
duration="$t_s sec"
|
|
fi
|
|
|
|
if [ ! "$retval" = 0 ]; then
|
|
echo -e "\t[ Error ]: Removing backup from $_bakup_day failed"
|
|
else
|
|
echolog "\t--- Backup from $_bakup_day successfully removed.. [ $duration ]"
|
|
fi
|
|
_done=true
|
|
fi
|
|
done
|
|
|
|
## - Archive last backup..
|
|
## -
|
|
if [ -f $backup_mirror_dir/BACKUP-DATE ]; then
|
|
_last_backup_date=`head -n1 $backup_mirror_dir/BACKUP-DATE`
|
|
if [ ! -d $backup_archiv_dir/Backup_$_last_backup_date ] ; then
|
|
echolog "\n\tArchiving (last) backup from $_last_backup_date.. ( `$date +%H`:`$date +%M` h )"
|
|
|
|
## - begin timestamp
|
|
## -
|
|
b_timestamp=`$date +"%s"`
|
|
|
|
cp -ral $backup_mirror_dir $backup_archiv_dir/Backup_$_last_backup_date
|
|
|
|
## - end timestamp
|
|
## -
|
|
e_timestamp=`$date +"%s"`
|
|
|
|
## - determin duration
|
|
## -
|
|
_time=`expr $e_timestamp - $b_timestamp`
|
|
t_h=`expr $_time / 60 / 60`
|
|
t_rest_h=`expr $_time - $t_h \\* 60 \\* 60`
|
|
t_m=`expr $t_rest_h / 60`
|
|
t_s=`expr $t_rest_h - $t_m \\* 60`
|
|
duration=""
|
|
if [ $t_h -gt 0 ]; then
|
|
duration="$t_h h : $t_m min : $t_s sec"
|
|
elif [ $t_m -gt 0 ];then
|
|
duration="$t_m min : $t_s sec"
|
|
else
|
|
duration="$t_s sec"
|
|
fi
|
|
|
|
if [ ! "$?" = 0 ]; then
|
|
echo -e "\t[ Error ]: Archiving backup from $_last_backup_date failed"
|
|
else
|
|
echolog "\t--- Backup from $_last_backup_date successfully archived.. [ $duration ]"
|
|
fi
|
|
_done=true
|
|
fi
|
|
|
|
fi # if $ONLY_BACKUP
|
|
|
|
fi
|
|
|
|
if ! $_done ; then
|
|
echolog "\n\tNothing to do. - No last backup (not yet archived) found\n\t No backup older then $days days found."
|
|
fi
|
|
echolog ""
|
|
|
|
fi
|
|
|
|
if $NO_NEW_BACKUP ; then
|
|
echolog "\n\tOnly handle existing backups was requested - NOT WRITING any backups (NO_NEW_BACKUP=true)."
|
|
else
|
|
|
|
if $disksetting_backup ; then
|
|
echolog "\nGoing to backup disk settings.. ( `$date +%H`:`$date +%M` h )"
|
|
$script_dir/disksettings_backup.sh
|
|
fi
|
|
if $svn_backup;then
|
|
echolog "\nGoing to backup svn repositories.. ( `$date +%H`:`$date +%M` h )"
|
|
export svn_source_base_path svn_gzip
|
|
$script_dir/svn_backup.sh
|
|
fi
|
|
|
|
if [[ -n "$pre_backup_commands" ]]; then
|
|
|
|
echolog "\nGoing to execute pre backup commands.."
|
|
|
|
for _val in "${pre_backup_commands[@]}" ; do
|
|
|
|
err_Log=${LOCK_DIR}/svn.err.log
|
|
> $err_Log
|
|
|
|
IFS='#' read -a _val_arr <<< "${_val}"
|
|
|
|
echolog "\t${_val_arr[0]}: ${_val_arr[1]}"
|
|
$ssh ${ssh_user}@${srcHost} "$sudo -u ${_val_arr[0]} ${_val_arr[1]}" > /dev/null 2> $err_Log
|
|
if [[ "$?" -ne 0 ]]; then
|
|
echolog "\t[ERROR] Executing command failed.\n $(cat "$err_Log")"
|
|
fi
|
|
|
|
done
|
|
fi
|
|
|
|
if $mysql_backup ;then
|
|
|
|
if [[ ${#mysql_credential_args_arr[@]} -gt 0 ]] ; then
|
|
for _val in "${mysql_credential_args_arr[@]}" ; do
|
|
|
|
IFS=':' read -a _val_arr <<< "${_val}"
|
|
|
|
mysql_version="${_val_arr[0]}"
|
|
mysql_credential_args="${_val_arr[1]}"
|
|
|
|
echolog "\nGoing to backup mysql databases ${_val_arr[0]} .. ( `$date +%H`:`$date +%M` h )"
|
|
export mysql_version mysql_credential_args mysql_gzip
|
|
$script_dir/mysql_backup.sh
|
|
done
|
|
else
|
|
echolog "\nGoing to backup mysql databases.. ( `$date +%H`:`$date +%M` h )"
|
|
export mysql_user mysql_password mysql_credential_args mysql_gzip
|
|
$script_dir/mysql_backup.sh
|
|
fi
|
|
## - !!
|
|
if $restart_apache ;then
|
|
$ssh ${ssh_user}@$srcHost "sudo /etc/init.d/apache2 restart"
|
|
fi
|
|
fi
|
|
if $pgsql_backup;then
|
|
echolog "\nGoing to backup postgres databases.. ( `$date +%H`:`$date +%M` h )"
|
|
export pgsql_user pgsql_gzip
|
|
$script_dir/pgsql_backup.sh
|
|
fi
|
|
|
|
echolog "\nGoing to backup directories.. ( `$date +%H`:`$date +%M` h )"
|
|
_done=false
|
|
|
|
for dir in $dir_backup ; do
|
|
echolog "\n\tDirectory $dir"
|
|
backup_dir=$dir $script_dir/dir_backup.sh
|
|
_done=true
|
|
done
|
|
|
|
if ! $_done ; then
|
|
echo -e "\t[ Warning ]: No directory for backup selected !!"
|
|
fi
|
|
|
|
|
|
if $mount_netdir; then
|
|
echolog "\nGoing to backup network directories.. ( `$date +%H`:`$date +%M` h )"
|
|
for dir in $net_mounted_dir_backup ; do
|
|
echolog "\n\tBackup network directory $dir"
|
|
export mount_netdir
|
|
backup_dir=$dir $script_dir/net_mounted_dir_backup.sh
|
|
done
|
|
fi
|
|
|
|
if $nextcloud_backup ; then
|
|
|
|
declare -a nextcloud_account_arr
|
|
for _account in $nextcloud_accounts ; do
|
|
nextcloud_account_arr+=("$_account")
|
|
done
|
|
|
|
echolog "\nGoing to backup nextcloud accounts.. ( $(date +%H): $($date +%M) h)"
|
|
for _val in "${nextcloud_account_arr[@]}" ; do
|
|
IFS=';' read -a _val_arr <<< "${_val}"
|
|
if [[ "${#_val_arr[@]}" -eq 3 ]] ; then
|
|
_server_url="${_val_arr[2]}"
|
|
else
|
|
_server_url="$nextcloud_server_url"
|
|
fi
|
|
_server_name="${_server_url#http://}"
|
|
_server_name="${_server_name#https://}"
|
|
#echo ""
|
|
#echo "#_val_arr[@]: ${#_val_arr[@]}"
|
|
#echo "_val_arr[0]: ${_val_arr[0]}"
|
|
#echo "_val_arr[1]: ${_val_arr[1]}"
|
|
#echo "_val_arr[2]: ${_val_arr[2]}"
|
|
#echo "_val_arr[3]: ${_val_arr[3]}"
|
|
#echo ""
|
|
|
|
echolog "\n\tBackup nextcloud account '${_val_arr[0]}' from server '$_server_name'"
|
|
nc_server_url="$_server_url" \
|
|
nc_server_name="$_server_name" \
|
|
nc_user="${_val_arr[0]}" \
|
|
nc_password="${_val_arr[1]}" \
|
|
${script_dir}/nc_accounts_backup.sh
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
if [[ -n "$post_backup_commands" ]]; then
|
|
|
|
echolog "\nGoing to execute post backup commands.."
|
|
|
|
for _val in "${post_backup_commands[@]}" ; do
|
|
|
|
err_Log=${LOCK_DIR}/svn.err.log
|
|
> $err_Log
|
|
|
|
IFS='#' read -a _val_arr <<< "${_val}"
|
|
|
|
echolog "\t${_val_arr[0]}: ${_val_arr[1]}"
|
|
$ssh ${ssh_user}@${srcHost} "$sudo -u ${_val_arr[0]} ${_val_arr[1]}" > /dev/null 2> $err_Log
|
|
if [[ "$?" -ne 0 ]]; then
|
|
echolog "\t[ERROR] Executing command failed.\n $(cat "$err_Log")"
|
|
fi
|
|
|
|
done
|
|
fi
|
|
|
|
fi # if $NO_NEW_BACKUP
|
|
|
|
end_h=`$date +%H`
|
|
end_m=`$date +%M`
|
|
backup_date=`$date +"%d.%m.%Y"`
|
|
backup_end_timestamp=`date +"%s"`
|
|
|
|
backup_time=`expr $backup_end_timestamp - $backup_begin_timestamp`
|
|
backup_h=`expr $backup_time / 60 / 60`
|
|
backup_rest_h=`expr $backup_time - $backup_h \\* 60 \\* 60`
|
|
backup_m=`expr $backup_rest_h / 60`
|
|
backup_s=`expr $backup_rest_h - $backup_m \\* 60`
|
|
|
|
if [ $backup_m -lt 10 ] ;then
|
|
backup_m="0$backup_m"
|
|
fi
|
|
if [ $backup_s -lt 10 ] ;then
|
|
backup_s="0$backup_s"
|
|
fi
|
|
|
|
rm -f $dirs_backup_done
|
|
|
|
if $_via_ssh_tunnel ; then
|
|
echolog "\nEnd saving host \"$target_host\" : $backup_date ( ${end_h}:${end_m} h ) - duration: ${backup_h} h :${backup_m} min : ${backup_s} sec\n\n"
|
|
else
|
|
echolog "\nEnd saving host \"$srcHost\" : $backup_date ( ${end_h}:${end_m} h ) - duration: ${backup_h} h :${backup_m} min : ${backup_s} sec\n\n"
|
|
fi
|
|
|
|
## - write duration time (a little bit formatted) to logfile of durations
|
|
## -
|
|
|
|
## - determin duration
|
|
## -
|
|
duration=""
|
|
if [ $backup_h -gt 0 ]; then
|
|
duration="$backup_h h : $backup_m min : $t_s sec"
|
|
elif [ $backup_m -gt 0 ];then
|
|
duration="$backup_m min : $backup_s sec"
|
|
else
|
|
duration="$backup_s sec"
|
|
fi
|
|
|
|
if $_via_ssh_tunnel ; then
|
|
_host=$target_host
|
|
else
|
|
_host=$srcHost
|
|
fi
|
|
_tmp_string="${_host}${duration}"
|
|
_strlen=${#_tmp_string}
|
|
_count_blank=`expr $right_tabstop - $_strlen`
|
|
echo -n -e " $_host:" >> $logDuration
|
|
_str_blanks=""
|
|
while [ $_count_blank -gt 1 ]; do
|
|
_str_blanks="$_str_blanks "
|
|
_count_blank=`expr $_count_blank - 1`
|
|
done
|
|
echo -n "$_str_blanks" >> $logDuration
|
|
echo -n " " >> $logDuration
|
|
echo -n -e "$duration\n" >> $logDuration
|
|
|
|
## - Set backup date
|
|
## -
|
|
## - Notice!!
|
|
## - We ave to remove olf file and set a new one, because we are
|
|
## - working with hard links
|
|
## -
|
|
[ -f $backup_mirror_dir/BACKUP-DATE ] && rm -f $backup_mirror_dir/BACKUP-DATE
|
|
[ -d $backup_mirror_dir ] && \
|
|
echo `date --date "now" +"%Y-%m-%d"` > $backup_mirror_dir/BACKUP-DATE
|
|
|
|
else
|
|
echolog "\n[WARNING HOST \"$srcHost\"]: Nothing was marked for backup !!\n"
|
|
fi
|
|
|
|
if $_via_ssh_tunnel ; then
|
|
if [[ -n "$tunnel_pid" ]]; then
|
|
kill "$tunnel_pid"
|
|
fi
|
|
fi
|