WEBTOOLIST.
STATIC WEB PLATFORM
NO ACCOUNTS / NOTHING TO INSTALL

$ webtoolist --script deploy-with-rollback

Symlink Deploy with Rollback

Deploy a release directory and switch a symlink, keeping the previous release for rollback.

category DevOps shell Bash 4+ updated 2026-09-21
deploysymlinkrollbackrelease
Safety notes
  • Modifies a live symlink. Run the dry run first and keep the previous release intact.
  • This is a minimal pattern, not a full deployment system; add health checks before switching traffic.

Dependencies: rsync, ln, date

Usage

  • Pass the built release directory, the releases root, and the current symlink.
  • Add —apply to copy and switch; the default prints the plan.

Script

#!/usr/bin/env bash
set -euo pipefail

src="${1:-}"
releases="${2:-}"
current="${3:-}"
apply=0
[[ ${4:-} == --apply ]] && apply=1

[[ -d "$src" && -n "$releases" && -n "$current" ]] || {
  echo "usage: $0 RELEASE_DIR RELEASES_ROOT CURRENT_SYMLINK [--apply]" >&2
  exit 1
}

stamp="$(date +%Y%m%d-%H%M%S)"
target="$releases/$stamp"

if ((apply)); then
  mkdir -p "$target"
  rsync -a --delete "$src"/ "$target"/
  ln -sfn "$target" "$current"
  echo "deployed: $target -> $current"
  echo "previous release still available; roll back with:"
  echo "  ln -sfn \$(readlink -f \"$current\" | xargs dirname)/PREVIOUS \"$current\""
else
  echo "would copy: $src/ -> $target/"
  echo "would switch: $current -> $target"
  echo "(dry run) re-run with --apply"
fi

Example

./deploy-with-rollback ./dist /srv/app/releases /srv/app/current
./deploy-with-rollback ./dist /srv/app/releases /srv/app/current --apply

Notes

  • Keep the releases directory on the same filesystem as the symlink so the switch is atomic.
  • Real deployments should add a smoke test between rsync and the symlink switch.

Related scripts