2
0
Fork 0
rootfs/usr/local/bin/sh.zio.backup

245 lines
6.4 KiB
Bash
Executable File

#!/usr/bin/env bash
if [[ "$(realpath "$(dirname "$(realpath -s "$0")")/../../../")" == "/" ]]; then
. /usr/local/libexec/zio/helpers/bash.sh
else
. "$(dirname "$(realpath -s "$0")")/../libexec/zio/helpers/bash.sh"
fi
me_filename="$(basename "$(realpath -s "$0")")"
backup_scripts_dir="$(get_config_dir "sh.zio.backup")/scripts"
cache_dir="/var/cache/sh.zio.backup/restic"
secrets_dir="$(get_config_dir "sh.zio.backup")/secrets"
host="$(hostname -s)"
restic_path=""
restic_repo_file="$secrets_dir/restic-repo"
restic_repo_passwd_file="$secrets_dir/restic-repo-passwd"
function download_restic() {
restic_version="$1"
restic_download_url="https://github.com/restic/restic/releases/download/v${restic_version}/restic_${restic_version}_linux_amd64.bz2"
restic_path="/tmp/restic-$restic_version"
restic_archive_path="${restic_path}_$(date +%s).${restic_download_url##*.}"
if [[ ! -f "$restic_path" ]]; then
say info "Downloading Restic ($restic_version)..."
curl -L -s -o "$restic_archive_path" "$restic_download_url"
bzip2 -dc "$restic_archive_path" > "$restic_path"
rm -f "$restic_archive_path"
chmod +x "$restic_path"
if [[ ! "$(echo "$("$restic_path" version)")" == "restic $restic_version"* ]]; then
die "Unexpected output from '$restic_path version'"
rm -f "$restic_path"
fi
fi
}
function invoke_restic() {
command="$1"
args="${@:2}"
case "$(cat "$restic_repo_file")" in
"b2"*)
b2_account_id_file="$secrets_dir/b2-account-id"
b2_account_key_file="$secrets_dir/b2-account-key"
test_file "$b2_account_id_file"
test_file "$b2_account_key_file"
export B2_ACCOUNT_ID="$(cat "$b2_account_id_file")"
export B2_ACCOUNT_KEY="$(cat "$b2_account_key_file")"
;;
*)
die "Repository unsupported ("$(cat "$restic_repo_file")")"
;;
esac
if [[ -z $command ]]; then
say warning "No command specified. Not running"
elif [[ $command == "self-update" ]]; then
say warning "Command '$command' not supported. Not running"
else
"$restic_path" \
--cache-dir "$cache_dir" \
--password-file "$restic_repo_passwd_file" \
--repo "$(cat $restic_repo_file)" \
$command $args
fi
}
function invoke_script() {
backup_script="$1"
backup_script_filename="$(basename "$backup_script")"
backup_script_name="${backup_script_filename%.*}"
backup_script_name_length="${#backup_script_name}"
say primary "-[$backup_script_name]$(repeat "-" $((80-3-$backup_script_name_length)))"
chmod +x "$backup_script"
export -f backup_dir
export -f die
export -f forget_backup
export -f get_config_dir
export -f get_real_path
export -f invoke_restic
export -f podman_exec
export -f prune_backup
export -f say
export -f start_service
export -f stop_service
export -f test_file
export backup_scripts_dir
export cache_dir
export host
export me_filename
export restic_repo_file
export restic_repo_passwd_file
export restic_path
export secrets_dir
"$backup_scripts_dir/$backup_script_filename"
}
function backup_dir() {
path="$1"
args="${@:2}"
if [[ ! -d "$path" ]]; then
say warning "'$path' does not exist. Not backing up"
else
say info "Backing up: $path$(cat $restic_repo_file)"
invoke_restic \
backup \
--iexclude "__MACOSX" \
--iexclude ".cache" \
--iexclude ".DS_Store" \
--iexclude "cache" \
--iexclude "CachedData" \
--iexclude "CachedExtensionVSIXs" \
--iexclude "Code Cache" \
--iexclude "GPUCache" \
--iexclude "GrSharedCache" \
--iexclude "ShaderCache" \
--iexclude "system-cache" \
--iexclude "thumbs.db" \
--iexclude "tmp" \
--exclude "containers/storage/overlay" \
--exclude "containers/storage/overlay-containers" \
--exclude "containers/storage/overlay-images" \
--exclude "containers/storage/overlay-layers" \
--exclude-if-present ".nobackup" \
--host "$host" \
--tag "$me_filename" \
--tag "$(basename "$0")" \
$args "$path"
fi
}
function forget_backup() {
timeframe="$1"
if [[ -z $timeframe ]]; then
timeframe="0y0m7d0h"
fi
say info "Forgetting: $timeframe ($host)"
invoke_restic \
forget \
--keep-within "$timeframe" \
--host "$host"
}
function prune_backup() {
say info "Pruning"
invoke_restic \
prune \
--dry-run
}
function start_service() {
service="$1"
say info "Starting service: $service"
systemctl start $service
}
function stop_service() {
service="$1"
say info "Stopping service: $service"
systemctl stop $service
}
if [[ "$@" == "help" ]]; then
echo "$me_filename
Usage:
$me_filename
Run all backup scripts available in $backup_scripts_dir
$me_filename <script>
Run a specific script from a given path
$me_filename <command> [arguments]
Execute 'restic' with arbitrary commands and optional arguments.
This command is bootstrapped with some arguments — which are not
overridable — as follows:
* --cache-dir
* --password-file
* --repo
$me_filename help
Output this usage text. Use '--help' or 'help <command>' to output
restic's help text.
"
exit 0
fi
test_root
test_prog "bzip2"
test_prog "curl"
test_prog "grep"
test_prog "hostname"
mkdir -p "$backup_scripts_dir"
mkdir -p "$cache_dir"
mkdir -p "$secrets_dir"
chmod -R 711 "$secrets_dir"
download_restic 0.16.0
test_file "$restic_repo_file"
test_file "$restic_repo_passwd_file"
if [[ -z "$1" ]]; then
say info "Running backup scripts..."
if ! [[ "$(ls -A $backup_scripts_dir)" ]]; then
die "No scripts found in '$backup_scripts_dir'"
fi
for backup_script in $backup_scripts_dir/*; do
invoke_script $backup_script
done
say primary "$(repeat "-" 80)"
elif [[ -f "$1" ]]; then
invoke_script "$1"
else
command="$1"
arguments="${@:2}"
say info "Running: $restic_path $command $arguments"
invoke_restic $command $arguments
fi