diff --git a/deploy-nginx.sh b/deploy-nginx.sh index c650f94..76f3ed8 100644 --- a/deploy-nginx.sh +++ b/deploy-nginx.sh @@ -12,10 +12,10 @@ SITES_ENABLED="$NGINX_ETC/sites-enabled" SNIPPETS="$NGINX_ETC/snippets" CONFD="$NGINX_ETC/conf.d" -# Where to deploy static files from ./http and ./robots.txt -WEB_ROOT="${WEB_ROOT:-/var/www/nginx-conf}" +# Static file deploy targets (match your current layout) +WEB_HTML="/var/www/html" +WEB_ERRORS="/var/www/errors" -# Which sites to ensure are enabled (symlinked) ENABLE_SITES=( "default" "nik4nao.home.arpa" @@ -23,7 +23,6 @@ ENABLE_SITES=( "prv-api.nik4nao.xyz" ) -# Backup location BACKUP_BASE="/var/backups/nginx-conf-deploy" # --------------------------- @@ -33,14 +32,11 @@ usage() { cat <<'EOF' Usage: sudo ./deploy-nginx.sh [--dry-run] - -Options: - --dry-run Show actions, do not write/reload. -Env: - WEB_ROOT=/some/path Override static deploy directory (default: /var/www/nginx-conf) EOF } +log() { echo "==> $*"; } + need_root() { if [[ "${EUID}" -ne 0 ]]; then echo "ERROR: run as root (use sudo)." >&2 @@ -48,8 +44,6 @@ need_root() { fi } -log() { echo "==> $*"; } - DRY_RUN=0 if [[ "${1:-}" == "--dry-run" ]]; then DRY_RUN=1 @@ -90,20 +84,6 @@ copy_file() { fi } -sync_dir() { - local src="$1" - local dst="$2" - if [[ ! -d "$src" ]]; then - echo "ERROR: missing source dir: $src" >&2 - exit 1 - fi - if (( DRY_RUN )); then - log "[dry-run] rsync -a --delete $src/ -> $dst/" - else - rsync -a --delete "${src}/" "${dst}/" - fi -} - make_symlink() { local target="$1" local linkpath="$2" @@ -114,6 +94,25 @@ make_symlink() { fi } +sync_selected_http_files() { + # Copy only the files you actually use, into the two target dirs + local src_dir="$REPO_DIR/http" + + # /var/www/html + for f in index.html index.nginx-debian.html nik4nao-xyz-landing.html; do + if [[ -f "$src_dir/$f" ]]; then + copy_file "$src_dir/$f" "$WEB_HTML/$f" + fi + done + + # /var/www/errors + for f in 404.html 50x.html; do + if [[ -f "$src_dir/$f" ]]; then + copy_file "$src_dir/$f" "$WEB_ERRORS/$f" + fi + done +} + backup_paths=() add_backup_path() { @@ -125,7 +124,8 @@ add_backup_path() { do_backup() { mkdir_p "$backup_dir" - # Backup only what we touch (and their symlinks) + + # nginx files we touch add_backup_path "$NGINX_ETC/nginx.conf" add_backup_path "$CONFD/upstreams.conf" add_backup_path "$SNIPPETS/proxy-common.conf" @@ -134,14 +134,21 @@ do_backup() { add_backup_path "$SITES_ENABLED/$s" done + # static files we touch (only the known ones) + for f in index.html index.nginx-debian.html nik4nao-xyz-landing.html robots.txt; do + add_backup_path "$WEB_HTML/$f" + done + for f in 404.html 50x.html; do + add_backup_path "$WEB_ERRORS/$f" + done + if (( DRY_RUN )); then - log "[dry-run] tar -czf $backup_tar -P ${backup_paths[*]}" + log "[dry-run] tar -czf $backup_tar -P ${backup_paths[*]:-}" return fi if ((${#backup_paths[@]} == 0)); then log "No existing paths found to backup (first deploy?)." - # still create empty backup dir for consistency return fi @@ -154,12 +161,12 @@ restore_backup() { log "[dry-run] restore from $backup_tar" return fi - if [[ ! -f "$backup_tar" ]]; then + if [[ -f "$backup_tar" ]]; then + log "Restoring from backup..." + tar -xzf "$backup_tar" -P + else log "No backup tar found to restore: $backup_tar" - return fi - log "Restoring from backup..." - tar -xzf "$backup_tar" -P } nginx_test() { @@ -182,18 +189,15 @@ nginx_reload() { # Deploy # --------------------------- log "Repo: $REPO_DIR" -log "Deploying to: $NGINX_ETC" -log "Static WEB_ROOT: $WEB_ROOT" do_backup -# Ensure target dirs exist -mkdir_p "$SITES_AVAIL" "$SITES_ENABLED" "$SNIPPETS" "$CONFD" "$WEB_ROOT" +mkdir_p "$SITES_AVAIL" "$SITES_ENABLED" "$SNIPPETS" "$CONFD" "$WEB_HTML" "$WEB_ERRORS" # Copy core config/snippets -copy_file "$REPO_DIR/nginx.conf" "$NGINX_ETC/nginx.conf" -copy_file "$REPO_DIR/upstreams.conf" "$CONFD/upstreams.conf" -copy_file "$REPO_DIR/proxy-common.conf" "$SNIPPETS/proxy-common.conf" +copy_file "$REPO_DIR/nginx.conf" "$NGINX_ETC/nginx.conf" +copy_file "$REPO_DIR/upstreams.conf" "$CONFD/upstreams.conf" +copy_file "$REPO_DIR/proxy-common.conf" "$SNIPPETS/proxy-common.conf" # Copy site configs (repo *.conf -> /etc/nginx/sites-available/) copy_file "$REPO_DIR/default.conf" "$SITES_AVAIL/default" @@ -201,15 +205,15 @@ copy_file "$REPO_DIR/nik4nao.home.arpa.conf" "$SITES_AVAIL/nik4nao.home.arpa" copy_file "$REPO_DIR/nik4nao.xyz.conf" "$SITES_AVAIL/nik4nao.xyz" copy_file "$REPO_DIR/prv-api.nik4nao.xyz.conf" "$SITES_AVAIL/prv-api.nik4nao.xyz" -# Enable sites (symlinks) +# Enable sites for s in "${ENABLE_SITES[@]}"; do make_symlink "$SITES_AVAIL/$s" "$SITES_ENABLED/$s" done -# Deploy static content -sync_dir "$REPO_DIR/http" "$WEB_ROOT" +# Static files +sync_selected_http_files if [[ -f "$REPO_DIR/robots.txt" ]]; then - copy_file "$REPO_DIR/robots.txt" "$WEB_ROOT/robots.txt" + copy_file "$REPO_DIR/robots.txt" "$WEB_HTML/robots.txt" fi # Test + reload (rollback on failure)