dda-backup/infrastructure/docker/image/resources/file-functions.sh

70 lines
3.1 KiB
Bash
Raw Normal View History

2023-03-10 14:01:32 +00:00
backup_file_path='files'
2021-11-12 08:54:17 +00:00
2020-12-31 14:05:56 +00:00
function init-file-repo() {
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
2023-03-10 14:01:32 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_file_path} -v init
2021-11-12 08:54:17 +00:00
else
2023-03-10 14:01:32 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_file_path} -v init --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
fi
}
2020-12-31 14:05:56 +00:00
2021-11-12 08:54:17 +00:00
# First arg is the directory, second is optional for the path to a certificate file
2020-12-31 14:05:56 +00:00
function backup-directory() {
local directory="$1"; shift
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} unlock --cleanup-cache
cd ${directory} && restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} backup .
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} forget --group-by '' --keep-last 1 --keep-daily ${RESTIC_DAYS_TO_KEEP} --keep-monthly ${RESTIC_MONTHS_TO_KEEP} --prune
2021-11-12 08:54:17 +00:00
else
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} unlock --cleanup-cache --cacert ${CERTIFICATE_FILE}
cd ${directory} && restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} backup . --cacert ${CERTIFICATE_FILE}
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} forget --group-by '' --keep-last 1 --keep-daily ${RESTIC_DAYS_TO_KEEP} --keep-monthly ${RESTIC_MONTHS_TO_KEEP} --prune --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
fi
2020-12-31 14:05:56 +00:00
}
# First arg is the directory, the remaining args are the sub-directories (relative to the first directory) to backup.
function backup-fs-from-directory() {
local directory="$1"; shift
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} unlock --cleanup-cache
cd ${directory} && restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} backup $@
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} forget --group-by '' --keep-last 1 --keep-daily ${RESTIC_DAYS_TO_KEEP} --keep-monthly ${RESTIC_MONTHS_TO_KEEP} --prune
2021-11-12 08:54:17 +00:00
else
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} unlock --cleanup-cache --cacert ${CERTIFICATE_FILE}
cd ${directory} && restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} backup $@ --cacert ${CERTIFICATE_FILE}
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} forget --group-by '' --keep-last 1 --keep-daily ${RESTIC_DAYS_TO_KEEP} --keep-monthly ${RESTIC_MONTHS_TO_KEEP} --prune --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
fi
}
2020-12-31 14:05:56 +00:00
function restore-directory() {
local directory="$1"; shift
2023-03-10 14:19:09 +00:00
local snapshot_id="${1:-latest}"; shift
2020-12-31 14:05:56 +00:00
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} unlock --cleanup-cache
2021-11-12 08:54:17 +00:00
rm -rf ${directory}*
2023-03-10 14:19:09 +00:00
restic -v -r $RESTIC_REPOSITORY/${backup_file_path} restore ${snapshot_id} --target ${directory}
2021-11-12 08:54:17 +00:00
else
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_file_path} unlock --cleanup-cache --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
rm -rf ${directory}*
2023-03-10 14:19:09 +00:00
restic -v -r $RESTIC_REPOSITORY/${backup_file_path} restore ${snapshot_id} --target ${directory} --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
fi
2023-03-10 14:43:58 +00:00
}
function list-snapshot-files() {
if [ -z ${CERTIFICATE_FILE} ];
then
restic -r ${RESTIC_REPOSITORY}/${backup_file_path} snapshots
else
restic -r ${RESTIC_REPOSITORY}/${backup_file_path} snapshots --cacert ${CERTIFICATE_FILE}
fi
}