diff --git a/restore-workspace.sh b/restore-workspace.sh new file mode 100755 index 0000000..fbc8e56 --- /dev/null +++ b/restore-workspace.sh @@ -0,0 +1,144 @@ +#!/usr/bin/env bash +# +# One-shot workspace restore: create the admin account, create + assign the +# workspace, then restore a backup into it (via ./backup-restore.sh). +# +# Usage: +# ./restore-workspace.sh [options] [-- ] +# +# Options: +# -e, --email Admin account email (default: admin@local) +# -p, --password Admin account password (default: generated, printed at the end) +# -f, --first Admin first name (default: Admin) +# -l, --last Admin last name (default: Platform) +# --skip-account Don't create the account (it already exists) +# --skip-workspace Don't create/assign the workspace (it already exists) +# --date Backup snapshot timestamp, passed to backup-restore.sh +# +# Examples: +# ./restore-workspace.sh ./backups/myws myws +# ./restore-workspace.sh ./backups/myws myws -e me@example.com -p secret +# ./restore-workspace.sh ./backups/myws myws --skip-account -- --merge +set -euo pipefail + +CONFIG_FILE="huly_v7.conf" +if [ ! -f "$CONFIG_FILE" ]; then + echo -e "\033[1;31mConfig not found: $CONFIG_FILE. Run ./setup.sh first.\033[0m" + exit 1 +fi +# shellcheck disable=SC1090 +source "$CONFIG_FILE" + +BACKUP_DIR="${1:-}" +WORKSPACE="${2:-}" +shift $(( $# >= 2 ? 2 : $# )) || true + +EMAIL="admin@local" +PASSWORD="" +FIRST="Admin" +LAST="Platform" +SKIP_ACCOUNT=false +SKIP_WORKSPACE=false +DATE="" +EXTRA_ARGS=() + +while [ $# -gt 0 ] && [ "${1:-}" != "--" ]; do + case "$1" in + -e|--email) EMAIL="$2"; shift 2 ;; + -p|--password) PASSWORD="$2"; shift 2 ;; + -f|--first) FIRST="$2"; shift 2 ;; + -l|--last) LAST="$2"; shift 2 ;; + --skip-account) SKIP_ACCOUNT=true; shift ;; + --skip-workspace) SKIP_WORKSPACE=true; shift ;; + --date) DATE="$2"; shift 2 ;; + *) + echo -e "\033[1;31mUnknown option: $1\033[0m" + exit 1 + ;; + esac +done +if [ "${1:-}" == "--" ]; then + shift + EXTRA_ARGS=("$@") +fi + +if [ -z "$BACKUP_DIR" ] || [ -z "$WORKSPACE" ]; then + echo "Usage: $0 [options] [-- ]" + echo "" + echo " Local directory with downloaded backup files" + echo " Workspace id/url to create and restore into" + echo " -e, --email Admin account email (default: admin@local)" + echo " -p, --password Admin password (default: generated and printed)" + echo " -f, --first Admin first name (default: Admin)" + echo " -l, --last Admin last name (default: Platform)" + echo " --skip-account Account already exists, don't create it" + echo " --skip-workspace Workspace already exists, don't create/assign it" + echo " --date Backup snapshot timestamp (default: latest)" + echo " -- Extra args passed to backup-restore.sh tool call (e.g. --merge)" + exit 1 +fi + +if [ ! -d "$BACKUP_DIR" ]; then + echo -e "\033[1;31mBackup directory not found: $BACKUP_DIR\033[0m" + exit 1 +fi + +# Generate a password if none was provided +GENERATED_PASSWORD=false +if [ -z "$PASSWORD" ] && [ "$SKIP_ACCOUNT" != true ]; then + PASSWORD="$(LC_ALL=C tr -dc 'A-Za-z0-9'