dda-backup/infrastructure/docker/image/resources/file-functions.sh
2021-11-12 11:52:47 +01:00

72 lines
No EOL
2.2 KiB
Bash

#! /bin/bash
function init-file-repo() {
local command="restic -r ${RESTIC_REPOSITORY}/files -v init"
if [ -z ${CERTIFICATE_FILE} ];
then
${command}
else
${command} --cacert ${CERTIFICATE_FILE}
fi
}
# First arg is the directory, second is optional for the path to a certificate file
function backup-directory() {
local directory="$1"; shift
local command_unlock="restic -v -r ${RESTIC_REPOSITORY}/files unlock --cleanup-cache"
local command_backup="cd ${directory} && restic -v -r ${RESTIC_REPOSITORY}/files backup ."
local command_forget="restic -v -r ${RESTIC_REPOSITORY}/files forget --keep-last 1 --keep-within ${RESTIC_DAYS_TO_KEEP}d --prune"
if [ -z ${CERTIFICATE_FILE} ];
then
${command_unlock}
${command_backup}
${command_forget}
else
${command_unlock} --cacert ${CERTIFICATE_FILE}
${command_backup} --cacert ${CERTIFICATE_FILE}
${command_forget} --cacert ${CERTIFICATE_FILE}
fi
}
# 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
local command_unlock="restic -v -r ${RESTIC_REPOSITORY}/files unlock --cleanup-cache"
local command_backup="cd ${directory} && restic -v -r ${RESTIC_REPOSITORY}/files backup $@"
local command_forget="restic -v -r ${RESTIC_REPOSITORY}/files forget --keep-last 1 --keep-within ${RESTIC_DAYS_TO_KEEP}d --prune"
if [ -z ${CERTIFICATE_FILE} ];
then
${command_unlock}
${command_backup}
${command_forget}
else
${command_unlock} --cacert ${CERTIFICATE_FILE}
${command_backup} --cacert ${CERTIFICATE_FILE}
${command_forget} --cacert ${CERTIFICATE_FILE}
fi
}
function restore-directory() {
local directory="$1"; shift
local command_unlock="restic -v -r ${RESTIC_REPOSITORY}/files unlock --cleanup-cache"
local command_restore="restic -v -r $RESTIC_REPOSITORY/files restore latest --target ${directory}"
if [ -z ${CERTIFICATE_FILE} ];
then
${command_unlock}
rm -rf ${directory}*
${command_restore}
else
${command_unlock} --cacert ${CERTIFICATE_FILE}
rm -rf ${directory}*
${command_restore} --cacert ${CERTIFICATE_FILE}
fi
}