61 lines
1.3 KiB
Bash
61 lines
1.3 KiB
Bash
|
#!/usr/bin/env sh
|
||
|
|
||
|
set -e
|
||
|
|
||
|
set -a
|
||
|
. "/opt/services/backup/.env"
|
||
|
. "/opt/services/backup/.secret.env"
|
||
|
set +a
|
||
|
|
||
|
hc_url="https://hc-ping.com/$HC_UID"
|
||
|
|
||
|
curl -fsSL --retry 3 "$hc_url/start" >/dev/null
|
||
|
|
||
|
BACKUP_LOCATION="/tmp/backup-misc"
|
||
|
|
||
|
rm -rf "$BACKUP_LOCATION"
|
||
|
mkdir -p "$BACKUP_LOCATION"
|
||
|
cd "$BACKUP_LOCATION" || exit
|
||
|
|
||
|
_hc_fail () {
|
||
|
curl -fsSL --retry 3 "$hc_url/fail"
|
||
|
exit 1
|
||
|
}
|
||
|
|
||
|
_backup_prepare_postgres () {
|
||
|
mkdir -p "$BACKUP_LOCATION/postgres"
|
||
|
cd "$BACKUP_LOCATION/postgres" || exit
|
||
|
|
||
|
postgres_tables=$(sudo -u postgres psql -Atc "SELECT datname FROM pg_database WHERE datistemplate = false;")
|
||
|
|
||
|
for i in $postgres_tables
|
||
|
do
|
||
|
echo "dumping $i"
|
||
|
sudo -u postgres pg_dump "$i" | gzip >"pg_dump_$i.gz"
|
||
|
echo "done with $i"
|
||
|
echo ""
|
||
|
done
|
||
|
|
||
|
echo "dumping all"
|
||
|
sudo -u postgres pg_dumpall | gzip >"pg_dumpall.gz"
|
||
|
}
|
||
|
|
||
|
_backup_prepare_mailcow () {
|
||
|
export MAILCOW_BACKUP_LOCATION="$BACKUP_LOCATION/mailcow"
|
||
|
mkdir -p "$MAILCOW_BACKUP_LOCATION"
|
||
|
/opt/mailcow-dockerized/helper-scripts/backup_and_restore.sh \
|
||
|
backup all --delete-days 3
|
||
|
}
|
||
|
|
||
|
echo ""
|
||
|
echo "preparing postgres"
|
||
|
time _backup_prepare_postgres >/tmp/backup-postgres.log || _hc_fail
|
||
|
echo ""
|
||
|
echo "preparing mailcow"
|
||
|
time _backup_prepare_mailcow >/tmp/backup-mailcow.log || _hc_fail
|
||
|
|
||
|
cd /opt/services/backup/
|
||
|
docker compose run --rm backup || _hc_fail
|
||
|
|
||
|
curl -fsSL --retry 3 "$hc_url"
|