2021-11-12 08:54:17 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
2020-12-31 14:05:56 +00:00
|
|
|
function init-file-repo() {
|
2021-11-12 08:54:17 +00:00
|
|
|
local command="restic -r ${RESTIC_REPOSITORY}/files -v init"
|
2020-12-31 14:05:56 +00:00
|
|
|
|
2021-11-12 08:54:17 +00:00
|
|
|
if [ -z ${CERTIFICATE_FILE} ];
|
|
|
|
then
|
2021-11-12 10:52:47 +00:00
|
|
|
${command}
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-12 10:52:47 +00:00
|
|
|
${command} --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 10:52:47 +00:00
|
|
|
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"
|
2020-12-31 14:05:56 +00:00
|
|
|
|
2021-11-12 08:54:17 +00:00
|
|
|
if [ -z ${CERTIFICATE_FILE} ];
|
|
|
|
then
|
2021-11-12 10:52:47 +00:00
|
|
|
${command_unlock}
|
|
|
|
${command_backup}
|
|
|
|
${command_forget}
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-12 10:52:47 +00:00
|
|
|
${command_unlock} --cacert ${CERTIFICATE_FILE}
|
|
|
|
${command_backup} --cacert ${CERTIFICATE_FILE}
|
|
|
|
${command_forget} --cacert ${CERTIFICATE_FILE}
|
2021-11-12 08:54:17 +00:00
|
|
|
fi
|
2020-12-31 14:05:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:34:59 +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 10:52:47 +00:00
|
|
|
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"
|
2021-11-12 08:54:17 +00:00
|
|
|
|
|
|
|
if [ -z ${CERTIFICATE_FILE} ];
|
|
|
|
then
|
2021-11-12 10:52:47 +00:00
|
|
|
${command_unlock}
|
|
|
|
${command_backup}
|
|
|
|
${command_forget}
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-12 10:52:47 +00:00
|
|
|
${command_unlock} --cacert ${CERTIFICATE_FILE}
|
|
|
|
${command_backup} --cacert ${CERTIFICATE_FILE}
|
|
|
|
${command_forget} --cacert ${CERTIFICATE_FILE}
|
2021-11-12 08:54:17 +00:00
|
|
|
fi
|
|
|
|
|
2021-03-01 10:34:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 14:05:56 +00:00
|
|
|
function restore-directory() {
|
|
|
|
local directory="$1"; shift
|
|
|
|
|
2021-11-12 10:52:47 +00:00
|
|
|
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}"
|
2020-12-31 14:05:56 +00:00
|
|
|
|
2021-11-12 08:54:17 +00:00
|
|
|
if [ -z ${CERTIFICATE_FILE} ];
|
|
|
|
then
|
2021-11-12 10:52:47 +00:00
|
|
|
${command_unlock}
|
2021-11-12 08:54:17 +00:00
|
|
|
rm -rf ${directory}*
|
2021-11-12 10:52:47 +00:00
|
|
|
${command_restore}
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-12 10:52:47 +00:00
|
|
|
${command_unlock} --cacert ${CERTIFICATE_FILE}
|
2021-11-12 08:54:17 +00:00
|
|
|
rm -rf ${directory}*
|
2021-11-12 10:52:47 +00:00
|
|
|
${command_restore} --cacert ${CERTIFICATE_FILE}
|
2021-11-12 08:54:17 +00:00
|
|
|
fi
|
|
|
|
|
2020-12-31 14:05:56 +00:00
|
|
|
}
|