fixes and additions

This commit is contained in:
olli 2022-12-06 17:00:17 +01:00
parent b1122df33a
commit 6e309e9bae

View File

@ -4,7 +4,7 @@ function g_backup {
#### Config defaults
# dir(s) to backup (: separated)
BACKUPDIRS=/""
BACKUPDIRS=""
# Excludefile für rsync
BACKUPEXCLUDES=""
# Backup-Server (Backup-Destination)
@ -40,16 +40,52 @@ function g_backup {
usage="
$0 BACKUPDIR1[:BACKUPDIR2:BACKUPDIR3] BACKUPPATH [BACKUPEXCLUDES BACKUPSRV BACKUPSRVPORT BACKUPSRVUSER BACKUPSRVTESTCMD NODIRSTRUCT]"
[ -z "$BACKUPDIRS" ] && g_echo_error_exit "No Directory to Backup (BACKUPDIR) given $usage"
[ -z "$BACKUPSRVPATH" ] && g_echo_error_exit "No path to put the Backup (BACKUPPATH) given $usage"
[ -z "$BACKUPDIRS" ] && g_echo_error "No Directory to Backup (BACKUPDIR) given $usage" && return 1
[ -z "$BACKUPSRVPATH" ] && g_echo_error "No path to put the Backup (BACKUPPATH) given $usage" && return 1
if [ "$BACKUPEXCLUDES" == "default" ]
then
BACKUPEXCLUDES="$g_tmp/default-excludes"
echo '.local/share/keyrings/*temp*
.local/share/gvfs-metadata
.local/share/Trash
.unison*
unison.log
.unison.sh.log
.local/share/Steam/
dwhelper
.cache
vpn
.config/Nextcloud/
.VirtualBox/
.g_tmp
DMS/
tmp
.zoom/
.steam/
pagefile.sys
.config/Signal/
.var/app/com.microsoft.Teams
GTC/
.xsession-errors*
part.dd
.var/app/org.mozilla.firefox/*/cache/*
.local/share/Steam
Nextcloud/
/proc/
/sys/
/dev/
/run/
/mnt/' >$g_tmp/default-excludes
fi
if [ -n "$BACKUPSRV" ]
then
[ -z "$BACKUPSRVPORT" ] && g_echo_error_exit "No SSH-Port given $usage"
[ -z "$BACKUPSRVPORT" ] && g_echo_error "No SSH-Port given $usage" && return 1
sshopts="g_retrycmd ssh -n -p $BACKUPSRVPORT -o BatchMode=yes -o StrictHostKeyChecking=accept-new $BACKUPSRVUSER@$BACKUPSRV"
fi
rsyncopts="-aXAh --delete --ignore-missing-args --inplace"
rsyncopts="-aXAh --delete --ignore-missing-args --inplace --partial"
[ -n "$BACKUPEXCLUDES" ] && rsyncopts="$rsyncopts --delete-excluded --exclude-from=$BACKUPEXCLUDES"
[ -n "$BACKUPSRV" ] && rsyncopts="$rsyncopts -e 'ssh -p $BACKUPSRVPORT'"
@ -62,14 +98,14 @@ function g_backup {
for CMD in $CMDS
do
IFS="$ORIGIFS"
g_runcmd $sshopts $CMD || exit $?
g_runcmd $sshopts $CMD || return $?
done
IFS=':'
for BACKUPDIR in $BACKUPDIRS
do
IFS="$ORIGIFS"
rsyncoptsrun=$rsyncopts
rsyncoptsrun="$rsyncopts"
# prepare paths
if [ -z "$NODIRSTRUCT" ]
@ -78,31 +114,26 @@ function g_backup {
BACKUPSRVPATH_WITH_HOST="$BACKUPSRVPATH/$(hostname)"
BACKUPSRVPATH_WITH_DATE="$BACKUPSRVPATH_WITH_HOST/backup-$(date +%Y-%m-%d)/$BACKUPDIR"
BACKUPSRVPATH_WITH_DATE_INCOMPLETE="$BACKUPSRVPATH_WITH_DATE-incomplete"
g_runcmd $sshopts "mkdir -p $BACKUPSRVPATH_WITH_DATE_INCOMPLETE" || exit $?
BACKUPSRVPATH_LINK_DEST="$($sshopts ls -d $BACKUPSRVPATH_WITH_HOST/backup-*/$BACKUPDIR 2>/dev/null | egrep -v incomplete$ | tail -n1)"
g_runcmd $sshopts mkdir -p "$BACKUPSRVPATH_WITH_DATE_INCOMPLETE" || continue
BACKUPSRVPATH_LINK_DEST="$($sshopts ls -d "$BACKUPSRVPATH_WITH_HOST/backup-*/$BACKUPDIR" 2>/dev/null | egrep -v incomplete$ | tail -n1)"
# linkdest
[ -n "$BACKUPSRVPATH_LINK_DEST" ] && rsyncoptsrun="$rsyncoptsrun --link-dest=$BACKUPSRVPATH_LINK_DEST"
else
BACKUPSRVPATH_WITH_DATE_INCOMPLETE="/$BACKUPSRVPATH"
BACKUPSRVPATH_WITH_DATE_INCOMPLETE="$BACKUPSRVPATH"
fi
# Backup
[ -n "$BACKUPSRV" ] && sshrsync="$BACKUPSRVUSER@$BACKUPSRV:"
set -x
g_runcmd g_retrycmd rsync $rsyncoptsrun "$BACKUPDIR"/ "${sshrsync}$BACKUPSRVPATH_WITH_DATE_INCOMPLETE"/ || exit $?
# echo "g_retrycmd rsync $rsyncoptsrun $BACKUPDIR/ ${sshrsync}$BACKUPSRVPATH_WITH_DATE_INCOMPLETE/" >$g_tmp/bcmd
# g_echo "Running Backup $BACKUPDIR with
#$(cat $g_tmp/bcmd)"
# g_runcmd bash $g_tmp/bcmd || exit $?
g_runcmd g_retrycmd rsync $rsyncoptsrun "$BACKUPDIR"/ "${sshrsync}$BACKUPSRVPATH_WITH_DATE_INCOMPLETE"/ || continue
# done if no direstructure
[ -n "$NODIRSTRUCT" ] && continue
# if backups from this day already exists move it to $BACKUPSRVPATH_WITH_DATE-$$-old
$sshopts test -e $BACKUPSRVPATH_WITH_DATE && $sshopts mv $BACKUPSRVPATH_WITH_DATE $BACKUPSRVPATH_WITH_DATE-$$-old
g_runcmd $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 $?
g_runcmd $sshopts mv "$BACKUPSRVPATH_WITH_DATE_INCOMPLETE" "$BACKUPSRVPATH_WITH_DATE" || continue
yearmonth="$(date +%Y-%m-)"
@ -111,9 +142,9 @@ function g_backup {
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"
g_runcmd $sshopts mkdir -p "$BACKUPSRVPATH_MONTHLY" || continue
g_runcmd $sshopts mv -T "$BACKUPSRVPATH_WITH_DATE" "$BACKUPSRVPATH_MONTHLY" || continue
g_runcmd $sshopts ln -s "$BACKUPSRVPATH_MONTHLY" "$BACKUPSRVPATH_WITH_DATE" || continue
fi
# Cleanup old backups
@ -121,7 +152,7 @@ function g_backup {
$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
do
g_echo_warn "Removing Backup $g_toremove"
$sshopts rm -r "$g_toremove"
g_runcmd $sshopts rm -r "$g_toremove"
done
done