diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..c283b93 --- /dev/null +++ b/backup.sh @@ -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" diff --git a/maj.sh b/maj.sh deleted file mode 100755 index 0dfbb08..0000000 --- a/maj.sh +++ /dev/null @@ -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 diff --git a/synapse/backup.sh b/synapse/backup.sh deleted file mode 100755 index 051143d..0000000 --- a/synapse/backup.sh +++ /dev/null @@ -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" diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..71ad48f --- /dev/null +++ b/update.sh @@ -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 diff --git a/vw/backup.sh b/vw/backup.sh deleted file mode 100755 index 4d9051a..0000000 --- a/vw/backup.sh +++ /dev/null @@ -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!"