Files
BorgBackupWrapper/_borg_backup_set_prefix_cleanup.sh
Clemens Schwaighofer d9346c84a7 Borg Backup wrapper Version 4.0 update
* file backup borg folder has now -file name inside. Old folder must be
  manuall renamed
* All modules have the module id name as prefix in the backup set,
  _borg_backup_set_prefix_cleanup.sh needs to be run before to clean up
  all names or prune will not correctly delete old entries

New -T for one time target backup with custom prefix to have backups
outside the automated prune. -D option to delete this set

Add borg 1.2 support for compact which is called after delete and prune
to actually clean up the space.

-b borg executable and BORG_EXECUTEABLE override setting if borg is not
in path or another borg executable should be used
2022-03-28 11:27:35 +09:00

106 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# this will fix backup sets name
# must have a target call for
# file
# gitea
# mysql
# pgsql
# zabbix-settings-
export BORG_BASE_DIR="borg/";
DRYRUN=0;
TARGET_USER="";
TARGET_HOST="";
TARGET_PORT="";
TARGET_BORG_PATH="";
TARGET_FOLDER="";
BASE_FOLDER="/usr/local/scripts/borg/";
# those are the valid modules
MODULE_LIST="file gitea mysql pgsql zabbix"
# basic options
# -c for config file override
# -n for dry run test
while getopts ":c:nd" opt; do
case "${opt}" in
c|config)
BASE_FOLDER=${OPTARG};
;;
d|debug)
DEBUG=1;
;;
n|dryrun)
DRYRUN=1;
;;
:)
echo "Option -$OPTARG requires an argument."
;;
\?)
echo -e "\n Option does not exist: ${OPTARG}\n";
usage;
exit 1;
;;
esac;
done;
if [ ! -d "${BASE_FOLDER}" ]; then
echo "Base folder not found: ${BASE_FOLDER}";
exit 1;
fi;
SETTINGS_FILE="borg.backup.settings";
. "${BASE_FOLDER}${SETTINGS_FILE}";
ORIG_BACKUPFILE=${BACKUP_FILE};
for MODULE in ${MODULE_LIST}; do
echo "************* MODULE: ${MODULE}";
BACKUP_FILE=${ORIG_BACKUPFILE};
# if [ -f "${BASE_FOLDER}${SETTINGS_FILE_SUB}" ]; then
# . "${BASE_FOLDER}${SETTINGS_FILE_SUB}";
# fi;
# SETTINGS_FILE_SUB=$(echo "${SETTINGS_FILE}" | sed -e "s/\.settings/\.${MODULE,,}\.settings/");
if [ "${MODULE,,}" != "file" ]; then
BACKUP_FILE=${BACKUP_FILE/.borg/-${MODULE,,}.borg};
fi;
TARGET_FOLDER=${TARGET_FOLDER%/}
TARGET_FOLDER=${TARGET_FOLDER#/}
# and add slash front and back and escape the path
TARGET_FOLDER=$(printf "%q" "/${TARGET_FOLDER}/");
if [ ! -z "${TARGET_USER}" ] && [ ! -z "${TARGET_HOST}" ] && [ ! -z "${TARGET_PORT}" ]; then
TARGET_SERVER="ssh://${TARGET_USER}@${TARGET_HOST}:${TARGET_PORT}/";
# host/port
elif [ ! -z "${TARGET_HOST}" ] && [ ! -z "${TARGET_PORT}" ]; then
TARGET_SERVER="ssh://${TARGET_HOST}:${TARGET_PORT}/";
# user/host
elif [ ! -z "${TARGET_USER}" ] && [ ! -z "${TARGET_HOST}" ]; then
TARGET_SERVER="${TARGET_USER}@${TARGET_HOST}:";
# host
elif [ ! -z "${TARGET_HOST}" ]; then
TARGET_SERVER="${TARGET_HOST}:";
fi;
# we dont allow special characters, so we don't need to special escape it
REPOSITORY="${TARGET_SERVER}${TARGET_FOLDER}${BACKUP_FILE}";
echo "==== REPOSITORY: ${REPOSITORY}";
borg list --format '{archive}{NL}' ${REPOSITORY}|grep -v "${MODULE},"|
while read i; do
# for gitea, zabbix we do not ADD we RENAME
if [ "${MODILE}" = "gitea" ]; then
target_name=$(echo $i | sed -e 's/gitea-/gitea,/');
elif [ "${MODILE}" = "zabbix" ]; then
target_name=$(echo $i | sed -e 's/zabbix-settings-/zabbix,settings-/');
else
target_name="${MODULE},${i}";
fi;
echo "- Rename from: ${i} to: ${target_name}";
if [ ${DEBUG} -eq 1 ]; then
echo "borg rename -p -v ${REPOSITORY}::${i} ${target_name}";
fi;
if [ ${DRYRUN} -eq 0 ]; then
borg rename -p -v ${REPOSITORY}::${i} ${target_name};
fi;
done;
done;
# __END__