#!/usr/bin/env bash # # Fetches a fresh backup from the server to a local folder (e.g. the NAS) – nothing is changed on the server # except creating the new database dump. # # scripts/backup-pull.sh # # Copies: the newest database dump (backup:database) and the uploaded files (photos, contracts) from shared/storage/app. # Uses the same settings as scripts/deploy.sh (scripts/deploy..env). set -euo pipefail ENVIRONMENT="${1:?Umgebung angeben, z. B. production}" TARGET="${2:?Zielordner angeben, z. B. /volume1/backup/fewo}" ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" # shellcheck source=/dev/null source "$ROOT/scripts/deploy.$ENVIRONMENT.env" DEPLOY_SSH_KEY="${DEPLOY_SSH_KEY/#\~/$HOME}" SSH=(ssh -p "${DEPLOY_PORT:-22}" -o BatchMode=yes ${DEPLOY_SSH_KEY:+-i "$DEPLOY_SSH_KEY"} "${DEPLOY_USER:+$DEPLOY_USER@}$DEPLOY_HOST") STAMP="$(date +%Y%m%d-%H%M%S)" mkdir -p "$TARGET/$STAMP" echo "▶ Datenbank sichern" "${SSH[@]}" "cd '$DEPLOY_PATH/current' && '${DEPLOY_PHP:-/opt/RZphp84/bin/php-cli}' artisan backup:database --no-interaction" LATEST="$("${SSH[@]}" "ls -1 '$DEPLOY_PATH/shared/storage/app/private/backups/' | sort | tail -n 1")" "${SSH[@]}" "cat '$DEPLOY_PATH/shared/storage/app/private/backups/$LATEST'" > "$TARGET/$STAMP/$LATEST" echo "▶ Dateien sichern (Fotos, Verträge)" "${SSH[@]}" "tar -czf - -C '$DEPLOY_PATH/shared/storage/app' --exclude='private/backups' ." > "$TARGET/$STAMP/files.tar.gz" echo "✓ Gesichert nach $TARGET/$STAMP ($(du -sh "$TARGET/$STAMP" | cut -f1))"