updated backup & update scripts

This commit is contained in:
Viyurz 2023-12-09 10:28:33 +01:00
parent 6728469f78
commit 27fa06a960
5 changed files with 182 additions and 72 deletions

97
backup.sh Executable file
View file

@ -0,0 +1,97 @@
#!/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"

31
maj.sh
View file

@ -1,31 +0,0 @@
#!/bin/bash
services=(etebase nginx-www searxng synapse syncthing vw)
if [[ ! $(echo "${services[*]} all" | grep -P "\b$1\b" ) ]]; then
echo "Wrong argument."
exit 1
fi
rel_path="$(dirname "$0")"
case "$1" in
vw|all)
sudo "$rel_path/vw/backup.sh" || exit 1
;;
esac
if [[ "$1" != "all" ]]; then
docker-compose -f "$rel_path/$1/docker-compose.yaml" pull
docker-compose -f "$rel_path/$1/docker-compose.yaml" up -d
else
for service in "${services[@]}"; do
docker-compose -f "$rel_path/$service/docker-compose.yaml" pull
docker-compose -f "$rel_path/$service/docker-compose.yaml" up -d
done
fi

View file

@ -1,24 +0,0 @@
#!/bin/bash
if [[ $UID -ne 0 ]]; then
echo "Must be run as root."
exit 1
fi
media_dir="/mnt/synapsedata/media_store"
backup_date="$(date '+%Y%m%d-%Hh%Mm')"
bak_folder="/mnt/backups/synapsebackup"
borg_repo="$bak_folder/borg"
mkdir -p "$bak_folder"
# PSQL
echo "Creating backup of PosgreSQL databases to $bak_folder/dump_$backup_date.sql"
docker exec -t synapse_postgres pg_dumpall -c -U synapse > "$bak_folder/dump_$backup_date.sql"
echo "Done, total size of all backups: $(du -hc $bak_folder/dump_* | grep total | grep -oP '^[\d\w]+')."
# Media
[[ -d "$borg_repo" ]] || borg init --encryption=none "$borg_repo"
borg create --stats "$borg_repo::media-{now:%Y-%m-%d_%H:%M:%S}" "$media_dir"
borg prune --stats --keep-daily=7 --keep-weekly=4 --keep-monthly=3 "$borg_repo"
borg compact "$borg_repo"

85
update.sh Executable file
View file

@ -0,0 +1,85 @@
#!/bin/bash
services=(coturn etebase nginx-www searxng synapse syncthing vw)
rel_path="$(dirname "$0")"
function compose_up {
docker-compose -f "$rel_path/$1/docker-compose.yaml" pull
docker-compose -f "$rel_path/$1/docker-compose.yaml" up -d
}
function coturn_update {
sudo docker-compose -f "$rel_path/coturn/docker-compose.yaml" pull
sudo docker-compose -f "$rel_path/coturn/docker-compose.yaml" up -d
}
function etebase_update {
if ! docker pull victorrds/etebase:alpine | grep -q 'Image is up to date' ; then
echo "Update available for etebase."
sudo "$rel_path/backup.sh" etebase --norestart
compose_up etebase
else
echo "No update available for etebase."
fi
}
function nginx-www_update {
compose_up nginx-www
}
function searxng_update {
compose_up searxng
}
function synapse_update {
has_update=0
if ! docker pull postgres:alpine | grep -q 'Image is up to date' || ! docker pull matrixdotorg/synapse:latest | grep -q 'Image is up to date'; then
has_update=1
fi
if [[ $has_update -eq 1 ]]; then
echo "Update available for synapse and/or postgres."
sudo "$rel_path/backup.sh" synapse --norestart
compose_up synapse
else
echo "No update available for synapse and postgres."
fi
}
function syncthing_update {
compose_up syncthing
}
function vw_update {
sudo "$rel_path/backup.sh" vw
compose_up vw
}
if [[ -z "$1" || ! $(echo "${services[*]} all" | grep -P "\b$1\b" ) ]]; then
echo "Wrong argument."
echo "Options: ${services[*]}"
exit 1
fi
if [[ "$1" != "all" ]]; then
echo "Running $1 update."
${1}_update
else
for service in "${services[@]}"; do
echo "Running $service update."
${service}_update
done
fi

View file

@ -1,17 +0,0 @@
#!/bin/bash
if [[ $UID -ne 0 ]]; then
echo "Must be run as root."
exit 1
fi
backup_date="$(date '+%Y%m%d-%Hh%Mm')"
vw_data='/mnt/vwdata'
bak_folder="/mnt/backups/vwbackup"
mkdir -p "$bak_folder"
# Backup directement vers la storage box marche pas
sqlite3 "$vw_data/db.sqlite3" ".backup '$vw_data/db-$backup_date.sqlite3'" && echo "Vaultwarden backup created successfully!"
mv "$vw_data/db-$backup_date.sqlite3" "$bak_folder" && echo "Successfully moved backup to $bak_folder!"