vps/backup.sh

98 lines
1.9 KiB
Bash
Raw Normal View History

2023-12-09 10:28:33 +01:00
#!/bin/bash
if [[ $UID -ne 0 ]]; then
echo "Must be run as root."
exit 1
fi
rel_path="$(dirname "$0")"
# Rootless socket
export DOCKER_HOST=unix:///run/user/1000/docker.sock
# Restart after backup yes/no
if [[ "$2" = "--norestart" ]]; then
restart=0
else
restart=1
fi
etebase_datadir="/mnt/etebasedata"
synapse_datadir="/mnt/synapsedata"
synapsepg_datadir="/mnt/synapsepgdata"
vw_datadir="/mnt/vwdata"
borg_repo="/mnt/backups/borg"
declare -A targets=(
[vw]="$vw_datadir/db-backup.sqlite3"
[synapse]="$synapse_datadir $synapsepg_datadir/dump.sql"
[etebase]="$etebase_datadir"
)
function borg_create {
borg create --stats "$borg_repo::$1-{now:%Y-%m-%d-%H-%M-%S}" ${targets[$1]}
borg prune --stats --glob-archives="$1-*" --keep-daily=7 --keep-weekly=4 --keep-monthly=3 "$borg_repo"
}
function compose_start {
docker-compose -f "$rel_path/$1/docker-compose.yaml" start
}
function compose_stop {
docker-compose -f "$rel_path/$1/docker-compose.yaml" stop
}
function etebase_backup {
compose_stop etebase
borg_create etebase
[[ $restart -eq 1 ]] && compose_start etebase
}
function synapse_backup {
docker exec -t synapse_postgres pg_dumpall -c -U synapse > "$synapsepg_datadir/dump.sql"
compose_stop synapse
borg_create synapse
[[ $restart -eq 1 ]] && compose_start synapse
}
function vw_backup {
sqlite3 "$vw_datadir/db.sqlite3" ".backup '$vw_datadir/db-backup.sqlite3'"
borg_create vw
}
if [[ -z "$1" || ! $(echo "${!targets[*]} all" | grep -P "\b$1\b" ) ]]; then
echo "Wrong argument."
echo "Options: ${!targets[*]}"
exit 1
fi
if ! [[ -d "$borg_repo" ]]; then
echo "Creating borg repository at $borg_repo."
borg init --encryption=none "$borg_repo"
fi
if [[ "$1" != "all" ]]; then
echo "Running $1 backup."
${1}_backup
else
for service in "${!targets[@]}"; do
echo "Running $service backup."
${service}_backup
done
fi
echo "Compacting borg repository."
borg compact --progress "$borg_repo"