168 lines
3.5 KiB
Bash
Executable File
168 lines
3.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
env_dir=$1
|
|
base_dir="$(dirname "$(realpath -s "$0")")"
|
|
root_dir="$base_dir/../../.."
|
|
|
|
[[ -z $env_dir ]] && env_dir="/var/lib/zio-backup"
|
|
|
|
function restic_exec() {
|
|
command="$1"
|
|
args="${@:2}"
|
|
|
|
if [[ -z $command ]]; then
|
|
say_warn "No command specified. Not running."
|
|
else
|
|
restic \
|
|
--cleanup-cache \
|
|
--password-file "$passwd_file" \
|
|
--repo "$repo" \
|
|
$command $args
|
|
fi
|
|
}
|
|
|
|
function backup() {
|
|
path="$1"
|
|
args="${@:2}"
|
|
|
|
if [[ ! -d $path ]]; then
|
|
warn "'$path' does not exist. Not backing up."
|
|
else
|
|
restic_exec \
|
|
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-if-present ".nobackup" \
|
|
--host "$host" \
|
|
$args "$path"
|
|
fi
|
|
}
|
|
|
|
function forget() {
|
|
path="$1"
|
|
|
|
restic_exec \
|
|
forget \
|
|
--host "$host" \
|
|
--keep-last 5 \
|
|
--path "$tag"
|
|
}
|
|
|
|
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"
|
|
|
|
paths=()
|
|
passwd_file=""
|
|
pre_script=""
|
|
post_script=""
|
|
services=()
|
|
|
|
if [[ $UID != 0 ]]; then
|
|
say_die "Unauthorized (are you root?)"
|
|
fi
|
|
|
|
if [[ ! -d $env_dir ]]; then
|
|
say_warn "Environment directory ($env_dir) not found."
|
|
else
|
|
chown root:root -R $env_dir
|
|
chmod 660 -R $env_dir
|
|
|
|
[[ -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
|
|
[[ ! -z $(get_prop "$env_dir/$host.var" PATHS) ]] && \
|
|
IFS=';' read -ra services <<< "$(get_prop "$env_dir/$host.var" PATHS)"
|
|
[[ ! -z $(get_prop "$env_dir/$host.var" SERVICES) ]] && \
|
|
IFS=';' read -ra services <<< "$(get_prop "$env_dir/$host.var" SERVICES)"
|
|
fi
|
|
|
|
if [[ -f "$env_dir/passwd" ]]; then
|
|
passwd_file="$env_dir/passwd"
|
|
else
|
|
say_warn "Password file ($passwd_file) not found."
|
|
fi
|
|
fi
|
|
|
|
[[ -z $paths ]] && say_die "No paths specified. Aborting..."
|
|
|
|
if [[ ! -z $pre_script ]]; then
|
|
say_do "Executing pre-backup script..."
|
|
. $pre_script
|
|
fi
|
|
|
|
say_do "Stopping services..."
|
|
for service in "${services[@]}"
|
|
do
|
|
systemctl stop $service
|
|
done
|
|
|
|
say_do "Backing up files..."
|
|
for path in "${paths[@]}"
|
|
do
|
|
backup $path
|
|
done
|
|
|
|
say_do "Starting services..."
|
|
for service in "${services[@]}"
|
|
do
|
|
systemctl start $service
|
|
done
|
|
|
|
if [[ ! -z $post_script ]]; then
|
|
say_do "Executing post-backup script..."
|
|
. $post_script
|
|
fi
|
|
|
|
say_do "Removing old backups..."
|
|
for path in "${paths[@]}"
|
|
do
|
|
forget $path
|
|
done
|