2
0
Fork 0
rootfs/usr/local/sbin/zio-backup

175 lines
3.7 KiB
Plaintext
Raw Normal View History

2022-02-23 06:02:21 +01:00
#!/bin/bash
env_dir=$1
base_dir="$(dirname "$(realpath -s "$0")")"
root_dir="$base_dir/../../.."
[[ -z $env_dir ]] && env_dir="/var/lib/zio-backup"
2022-02-23 06:02:21 +01:00
2022-02-23 06:28:53 +01:00
function restic_exec() {
command="$1"
args="${@:2}"
if [[ -z $command ]]; then
say_warn "No command specified. Not running."
else
restic \
--password-file "$passwd_file" \
--repo "$repo" \
2022-02-23 06:33:37 +01:00
$command $args
2022-02-23 06:28:53 +01:00
fi
}
2022-02-23 06:02:21 +01:00
function backup() {
path="$1"
2022-02-23 06:36:36 +01:00
args="${@:2}"
2022-02-23 06:02:21 +01:00
if [[ ! -d $path ]]; then
2022-02-23 06:02:21 +01:00
warn "'$path' does not exist. Not backing up."
else
2022-02-23 07:12:31 +01:00
for exclude in "${excludes[@]}"
do
args+=" --iexclude \"$exclude\""
done
2022-02-23 06:28:53 +01:00
restic_exec \
2022-02-23 06:03:38 +01:00
backup \
2022-02-23 06:28:53 +01:00
--iexclude "__MACOSX" \
2022-02-23 06:02:21 +01:00
--iexclude ".cache" \
2022-02-23 06:28:53 +01:00
--iexclude ".DS_Store" \
2022-02-23 06:02:21 +01:00
--iexclude "cache" \
--iexclude "CachedData" \
--iexclude "CachedExtensionVSIXs" \
--iexclude "Code Cache" \
--iexclude "GPUCache" \
--iexclude "GrSharedCache" \
--iexclude "ShaderCache" \
--iexclude "system-cache" \
2022-02-23 06:28:53 +01:00
--iexclude "thumbs.db" \
2022-02-23 06:02:21 +01:00
--iexclude "tmp" \
--exclude-if-present ".nobackup" \
2022-02-23 06:16:32 +01:00
--host "$host" \
2022-02-23 06:33:37 +01:00
$args "$path"
2022-02-23 06:02:21 +01:00
fi
}
2022-02-23 06:28:53 +01:00
function forget() {
2022-02-23 06:36:36 +01:00
path="$1"
2022-02-23 06:28:53 +01:00
restic_exec \
forget \
--host "$host" \
--keep-last 5 \
2022-02-23 06:52:04 +01:00
--path "$path"
2022-02-23 06:28:53 +01:00
}
2022-02-23 06:02:21 +01:00
function get_prop() {
file=$1
item=$2
echo $(grep -oP '(?<=^'"$item"'=).+' $file | tr -d '"')
}
function say() {
message=$@
echo -e "$@"
}
function say_die() {
message=$@
say "\033[1;31mError: $message\033[0m"
exit 255
}
function say_do() {
message=$@
say "\033[1;34m$message\033[0m"
}
function say_warn() {
message=$@
say "\033[1;33mWarning: $message\033[0m"
}
function test_path() {
prog=$@
if [[ ! -x "$(command -v $prog)" ]]; then
say_die "'$prog' is not installed (or not in \$PATH). Aborting..."
fi
}
host="de01"
repo="b2:zio-euc-servers"
2022-02-23 06:41:21 +01:00
2022-02-23 07:12:31 +01:00
excludes=()
2022-02-23 06:41:21 +01:00
paths=()
2022-02-23 06:14:53 +01:00
passwd_file=""
2022-02-23 06:02:21 +01:00
pre_script=""
post_script=""
services=()
2022-02-23 06:46:08 +01:00
if [[ $UID != 0 ]]; then
say_die "Unauthorized (are you root?)"
fi
2022-02-23 06:02:21 +01:00
if [[ ! -d $env_dir ]]; then
say_warn "Environment directory ($env_dir) not found."
else
2022-02-23 06:46:08 +01:00
chown root:root -R $env_dir
chmod 660 -R $env_dir
2022-02-23 06:02:21 +01:00
[[ -f "$env_dir/$host.pre" ]] && pre_script="$env_dir/$host.pre"
[[ -f "$env_dir/$host.post" ]] && post_script="$env_dir/$host.post"
if [[ -f "$env_dir/$host.var" ]]; then
2022-02-23 07:12:31 +01:00
[[ ! -z $(get_prop "$env_dir/$host.var" EXCLUDES) ]] && \
IFS=';' read -ra excludes <<< "$(get_prop "$env_dir/$host.var" EXCLUDES)"
2022-02-23 06:41:21 +01:00
[[ ! -z $(get_prop "$env_dir/$host.var" PATHS) ]] && \
2022-02-23 06:49:34 +01:00
IFS=';' read -ra paths <<< "$(get_prop "$env_dir/$host.var" PATHS)"
2022-02-23 06:02:21 +01:00
[[ ! -z $(get_prop "$env_dir/$host.var" SERVICES) ]] && \
IFS=';' read -ra services <<< "$(get_prop "$env_dir/$host.var" SERVICES)"
fi
2022-02-23 06:14:53 +01:00
if [[ -f "$env_dir/passwd" ]]; then
passwd_file="$env_dir/passwd"
else
say_warn "Password file ($passwd_file) not found."
fi
2022-02-23 06:02:21 +01:00
fi
2022-02-23 06:42:45 +01:00
[[ -z $paths ]] && say_die "No paths specified. Aborting..."
2022-02-23 06:42:18 +01:00
2022-02-23 06:02:21 +01:00
if [[ ! -z $pre_script ]]; then
say_do "Executing pre-backup script..."
. $pre_script
fi
say_do "Stopping services..."
for service in "${services[@]}"
do
2022-02-23 06:41:21 +01:00
systemctl stop $service
2022-02-23 06:02:21 +01:00
done
say_do "Backing up files..."
2022-02-23 06:41:21 +01:00
for path in "${paths[@]}"
do
backup $path
done
say_do "Starting services..."
for service in "${services[@]}"
do
systemctl start $service
done
2022-02-23 06:02:21 +01:00
if [[ ! -z $post_script ]]; then
say_do "Executing post-backup script..."
. $post_script
fi
2022-02-23 06:28:53 +01:00
say_do "Removing old backups..."
2022-02-23 06:41:21 +01:00
for path in "${paths[@]}"
do
forget $path
done