diff --git a/backup.py b/backup.py index 1eeb511..8234288 100644 --- a/backup.py +++ b/backup.py @@ -38,6 +38,10 @@ def createBackupScriptStructure(backupList, baseDomain=""): pathsToOptions = dict() basePaths = [] fullPaths = [] + + # commands for all size changed paths # + sizeChangeNotifyCommands = [] + for p in paths: cur = p @@ -69,6 +73,12 @@ def createBackupScriptStructure(backupList, baseDomain=""): fullPaths.append(cur) pathsToOptions.update({ "{}\t{}".format(hostname, cur) : options }) + # build commands to save new size after backup # + if options and "onlyifsizechanged" in options: + cmd = "ssh {} -t /opt/check_dir_size_for_backup.py --save-new-size {}" + cmd = cmd.format(hostname, cur) + sizeChangeNotifyCommands.append(cmd) + # keep order (important!) pathsAll = list(set(basePaths)) + [ p.rstrip("/") + "/***" for p in fullPaths ] @@ -82,11 +92,19 @@ def createBackupScriptStructure(backupList, baseDomain=""): pathsMinimal = list(filter(filterSizeChanged, pathsNoHighData)) rsyncScript = rsyncScriptTemplate.render(hostname=hostname, token=icingaToken, - hostname_base=hostnameBase) - + hostname_base=hostnameBase, + size_change_commands=sizeChangeNotifyCommands) + + # build all filter # rsyncFilterAll = rsyncFilterTemplate.render(paths=pathsAll) + + # build filter excluding high data # rsyncFilterNoHighData = rsyncFilterTemplate.render(paths=pathsNoHighData) + + # build filter excluding size changed no # rsyncFilterOnlyIfSizeChanged = rsyncFilterTemplate.render(paths=pathsOnlyIfSizeChanged) + + # build filter without high data and without non-size-changed dirs # rsyncFilterMinimal = rsyncFilterTemplate.render(paths=pathsMinimal) # async icinga config # @@ -123,7 +141,7 @@ def createBackupScriptStructure(backupList, baseDomain=""): wrapperName = "wrapper.sh" with open(os.path.join(backupPath, wrapperName), "w") as f: for n in scriptNames: - f.write("./{}".format(n)) + f.write("./{} $1".format(n)) f.write("\n") os.chmod(os.path.join(backupPath, wrapperName), 0o700) diff --git a/templates/rsync-backup.sh.j2 b/templates/rsync-backup.sh.j2 index 3dd6c93..48632eb 100644 --- a/templates/rsync-backup.sh.j2 +++ b/templates/rsync-backup.sh.j2 @@ -4,14 +4,14 @@ set -eu mkdir -p {{ hostname_base }} cd {{ hostname_base }} -TYPE="" -if [ -z "$1" ]; then +if [ $# -ne 0 ]; then TYPE=$1 fi -TEST=$(python -c "int('$TYPE' in ['size_changed', 'no_high_data', 'minimal'])^1") +TEST=$(python3 -c "print(int('$TYPE' in ['size_changed', 'no_high_data', 'minimal', ''])^1)") if [ $TEST -ne 0 ]; then - echo Bad Filter Option $1 + echo Bad Filter Option [$TYPE] + exit 1 fi dest=./ @@ -46,6 +46,14 @@ TOKEN="{{ token }}" if [ $RSYNC_SUCCESS -eq 0 ]; then curl -H "${CONTENT_TYPE}" -X POST "${ASYNC_ICINGA_ADDRESS}" -d \ "{\"service\": \"${SERVICE}\", \"token\": \"${TOKEN}\", \"status\": \"OK\", \"info\": \"\"}" + + # if size changed was copied save new size # + {% if not size_change_commands %} + echo No Sizes to notifiy + {% endif %} + {% for cmd in size_change_commands %} + {{ cmd }} + {% endfor %} else curl -H "${CONTENT_TYPE}" -X POST "${ASYNC_ICINGA_ADDRESS}" -d \ "{\"service\": \"${SERVICE}\", \"token\": \"${TOKEN}\", \"status\": \"CRITICAL\", \"info\": \"\"}"