114 lines
2.4 KiB
Bash
Executable File
114 lines
2.4 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/zio-backup"
|
|
|
|
function backup() {
|
|
path="$1"
|
|
tag="$2"
|
|
args="${@:3}"
|
|
|
|
if [[ ! -d $path ]]; then
|
|
warn "'$path' does not exist. Not backing up."
|
|
else
|
|
tag="$host_$tag"
|
|
|
|
restic \
|
|
backup
|
|
--dry-run
|
|
--iexclude ".cache" \
|
|
--iexclude "cache" \
|
|
--iexclude "CachedData" \
|
|
--iexclude "CachedExtensionVSIXs" \
|
|
--iexclude "Code Cache" \
|
|
--iexclude "GPUCache" \
|
|
--iexclude "GrSharedCache" \
|
|
--iexclude "ShaderCache" \
|
|
--iexclude "system-cache" \
|
|
--iexclude "tmp" \
|
|
--exclude-if-present ".nobackup" \
|
|
--hostname "$host" \
|
|
--repo "$repo" \
|
|
--tag "$tag" \
|
|
--verbose \
|
|
`[[ -s $args ]] && echo "$args"` "$path"
|
|
fi
|
|
}
|
|
|
|
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"
|
|
pre_script=""
|
|
post_script=""
|
|
services=()
|
|
|
|
if [[ ! -d $env_dir ]]; then
|
|
say_warn "Environment directory ($env_dir) not found."
|
|
else
|
|
[[ -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" SERVICES) ]] && \
|
|
IFS=';' read -ra services <<< "$(get_prop "$env_dir/$host.var" SERVICES)"
|
|
fi
|
|
fi
|
|
|
|
if [[ ! -z $pre_script ]]; then
|
|
say_do "Executing pre-backup script..."
|
|
. $pre_script
|
|
fi
|
|
|
|
say_do "Stopping services..."
|
|
for service in "${services[@]}"
|
|
do
|
|
echo "systemctl stop $service"
|
|
done
|
|
|
|
say_do "Backing up files..."
|
|
backup /etc config
|
|
backup /srv/store store
|
|
backup /var variable
|
|
|
|
if [[ ! -z $post_script ]]; then
|
|
say_do "Executing post-backup script..."
|
|
. $post_script
|
|
fi
|