Add Options 'ONLY_BACKUP' (do not handle existing archives) and 'NO_NEW_BACKUP' (do not backup any host, only handle existing archives).

This commit is contained in:
2020-05-23 15:33:39 +02:00
parent 41e67196d3
commit e6e88c7e7d
7 changed files with 367 additions and 180 deletions

View File

@ -150,173 +150,185 @@ if [ "$found" = "true" -o "$pgsql_backup" = "true" -o "$mysql_backup" = "true"
if $ARCHIVE ;then
echolog "\nGoing to handle archives.. ( `$date +%H`:`$date +%M` h )"
_done=false
if $ONLY_BACKUP ; then
if [ ! -d $backup_archiv_dir ]; then
mkdir -p $backup_archiv_dir;
fi
echolog "\n\tOnly saving backups was requested - (ONLY_BACKUP=true)."
## - 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.."
else
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
echolog "\nGoing to handle archives.. ( `$date +%H`:`$date +%M` h )"
_done=false
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
fi
_done=true
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
if [ ! -d $backup_archiv_dir ]; then
mkdir -p $backup_archiv_dir;
fi
_done=true
## - 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 found, no backup older then $days days found."
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 $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 $mysql_backup ;then
if $NO_NEW_BACKUP ; then
echolog "\n\tOnly handle existing backups was requested - NOT WRITING any backups (NO_NEW_BACKUP=true)."
else
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
if $disksetting_backup ; then
echolog "\nGoing to backup disk settings.. ( `$date +%H`:`$date +%M` h )"
$script_dir/disksettings_backup.sh
fi
## - !!
if $restart_apache ;then
$ssh ${ssh_user}@$srcHost "sudo /etc/init.d/apache2 restart"
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
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
if $mysql_backup ;then
echolog "\nGoing to backup directories.. ( `$date +%H`:`$date +%M` h )"
_done=false
if [[ ${#mysql_credential_args_arr[@]} -gt 0 ]] ; then
for _val in "${mysql_credential_args_arr[@]}" ; do
for dir in $dir_backup ; do
echolog "\n\tDirectory $dir"
backup_dir=$dir $script_dir/dir_backup.sh
_done=true
done
IFS=':' read -a _val_arr <<< "${_val}"
if ! $_done ; then
echo -e "\t[ Warning ]: No directory for backup selected !!"
fi
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
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
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
fi
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
fi # if $NO_NEW_BACKUP
end_h=`$date +%H`
end_m=`$date +%M`