„gaboshlib/g_backup.bashfunc“ ändern

This commit is contained in:
olli 2022-12-06 15:07:49 +01:00
parent 7275cf3899
commit b1122df33a

View File

@ -46,12 +46,12 @@ function g_backup {
if [ -n "$BACKUPSRV" ] if [ -n "$BACKUPSRV" ]
then then
[ -z "$BACKUPSRVPORT" ] && g_echo_error_exit "No SSH-Port given $usage" [ -z "$BACKUPSRVPORT" ] && g_echo_error_exit "No SSH-Port given $usage"
sshopts="g_retrycmd ssh -p $BACKUPSRVPORT -o BatchMode=yes -o StrictHostKeyChecking=accept-new $BACKUPSRVUSER@$BACKUPSRV" sshopts="g_retrycmd ssh -n -p $BACKUPSRVPORT -o BatchMode=yes -o StrictHostKeyChecking=accept-new $BACKUPSRVUSER@$BACKUPSRV"
fi fi
rsyncopts="-aXAh --delete" rsyncopts="-aXAh --delete --ignore-missing-args --inplace"
[ -n "$BACKUPEXCLUDES" ] && rsyncopts="$rsyncopts --delete-excluded --exclude-from=$BACKUPEXCLUDES" [ -n "$BACKUPEXCLUDES" ] && rsyncopts="$rsyncopts --delete-excluded --exclude-from=$BACKUPEXCLUDES"
[ -n "$BACKUPSRV" ] && rsyncopts="$rsyncopts -e \\'ssh -p $BACKUPSRVPORT\\'" [ -n "$BACKUPSRV" ] && rsyncopts="$rsyncopts -e 'ssh -p $BACKUPSRVPORT'"
ORIGIFS="$IFS" ORIGIFS="$IFS"
IFS=':' IFS=':'
@ -79,7 +79,7 @@ function g_backup {
BACKUPSRVPATH_WITH_DATE="$BACKUPSRVPATH_WITH_HOST/backup-$(date +%Y-%m-%d)/$BACKUPDIR" BACKUPSRVPATH_WITH_DATE="$BACKUPSRVPATH_WITH_HOST/backup-$(date +%Y-%m-%d)/$BACKUPDIR"
BACKUPSRVPATH_WITH_DATE_INCOMPLETE="$BACKUPSRVPATH_WITH_DATE-incomplete" BACKUPSRVPATH_WITH_DATE_INCOMPLETE="$BACKUPSRVPATH_WITH_DATE-incomplete"
g_runcmd $sshopts "mkdir -p $BACKUPSRVPATH_WITH_DATE_INCOMPLETE" || exit $? g_runcmd $sshopts "mkdir -p $BACKUPSRVPATH_WITH_DATE_INCOMPLETE" || exit $?
BACKUPSRVPATH_LINK_DEST="$($sshopts ls -d $BACKUPSRVPATH_WITH_HOST/backup-*/$BACKUPDIR 2>/dev/null | grep -v \\' Try \\' | tail -n1)" BACKUPSRVPATH_LINK_DEST="$($sshopts ls -d $BACKUPSRVPATH_WITH_HOST/backup-*/$BACKUPDIR 2>/dev/null | egrep -v incomplete$ | tail -n1)"
# linkdest # linkdest
[ -n "$BACKUPSRVPATH_LINK_DEST" ] && rsyncoptsrun="$rsyncoptsrun --link-dest=$BACKUPSRVPATH_LINK_DEST" [ -n "$BACKUPSRVPATH_LINK_DEST" ] && rsyncoptsrun="$rsyncoptsrun --link-dest=$BACKUPSRVPATH_LINK_DEST"
else else
@ -88,24 +88,41 @@ function g_backup {
# Backup # Backup
[ -n "$BACKUPSRV" ] && sshrsync="$BACKUPSRVUSER@$BACKUPSRV:" [ -n "$BACKUPSRV" ] && sshrsync="$BACKUPSRVUSER@$BACKUPSRV:"
echo "g_retrycmd rsync $rsyncoptsrun $BACKUPDIR/ ${sshrsync}$BACKUPSRVPATH_WITH_DATE_INCOMPLETE/" >$g_tmp/bcmd set -x
g_echo "Running Backup $BACKUPDIR with g_runcmd g_retrycmd rsync $rsyncoptsrun "$BACKUPDIR"/ "${sshrsync}$BACKUPSRVPATH_WITH_DATE_INCOMPLETE"/ || exit $?
$(cat $g_tmp/bcmd)" # echo "g_retrycmd rsync $rsyncoptsrun $BACKUPDIR/ ${sshrsync}$BACKUPSRVPATH_WITH_DATE_INCOMPLETE/" >$g_tmp/bcmd
g_runcmd bash $g_tmp/bcmd || exit $? # g_echo "Running Backup $BACKUPDIR with
#$(cat $g_tmp/bcmd)"
# g_runcmd bash $g_tmp/bcmd || exit $?
# done if no direstructure # done if no direstructure
[ -n "$NODIRSTRUCT" ] && continue [ -n "$NODIRSTRUCT" ] && continue
g_runcmd $sshopts test -e $BACKUPSRVPATH_WITH_DATE \&\& $sshopts mv $BACKUPSRVPATH_WITH_DATE $BACKUPSRVPATH_WITH_DATE-$$-old # if backups from this day already exists move it to $BACKUPSRVPATH_WITH_DATE-$$-old
g_runcmd $sshopts mv $BACKUPSRVPATH_WITH_DATE_INCOMPLETE $BACKUPSRVPATH_WITH_DATE || exit $? $sshopts test -e $BACKUPSRVPATH_WITH_DATE && $sshopts mv $BACKUPSRVPATH_WITH_DATE $BACKUPSRVPATH_WITH_DATE-$$-old
# move from -incomplete to default dir
$sshopts mv $BACKUPSRVPATH_WITH_DATE_INCOMPLETE $BACKUPSRVPATH_WITH_DATE || exit $?
yearmonth="$(date +%Y-%m-)"
# Create monthly dir if not exists
BACKUPSRVPATH_MONTHLY="$BACKUPSRVPATH_WITH_HOST/backup-${yearmonth}monthly/$BACKUPDIR"
if ! $sshopts test -e "$BACKUPSRVPATH_MONTHLY"
then
g_echo "Creating monthly-Backup $BACKUPSRVPATH_MONTHLY"
$sshopts mkdir -p "$BACKUPSRVPATH_MONTHLY"
$sshopts mv -T "$BACKUPSRVPATH_WITH_DATE" "$BACKUPSRVPATH_MONTHLY"
$sshopts ln -s "$BACKUPSRVPATH_MONTHLY" "$BACKUPSRVPATH_WITH_DATE"
fi
# Cleanup old backups # Cleanup old backups
yearmonth="$(date +%Y-%m-)"
yearmonthbefore="$(date +%Y-%m- --date='-1 month')" yearmonthbefore="$(date +%Y-%m- --date='-1 month')"
echo "$sshopts find $BACKUPSRVPATH_WITH_HOST -mindepth 1 -maxdepth 1 -type d -name \"backup-*\" | egrep -v \"backup-$yearmonth|backup-$yearmonthbefore|backup-20[0-9][0-9]-[0-9][0-9]-01\" | xargs -r rm -r" >$g_tmp/bcmd $sshopts find $BACKUPSRVPATH_WITH_HOST -mindepth 1 -maxdepth 1 -type d -name "backup-*" | egrep -v "backup-$yearmonth|backup-$yearmonthbefore|backup-20[0-9][0-9]-[0-9][0-9]-monthly" | while read g_toremove
g_echo "Cleaning up Backupdir $BACKUPDIR with '$(cat $g_tmp/bcmd)'" do
g_runcmd bash $g_tmp/bcmd || exit $? g_echo_warn "Removing Backup $g_toremove"
$sshopts rm -r "$g_toremove"
done
done done
g_echo "Backup-Skript beendet" g_echo "Backup-Skript beendet"