Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,35 @@ See the [Shopware CLI dump docs](https://developer.shopware.com/docs/products/to

### Live hosts

A live consumer (`SHOPWARE_DEPLOY_ENV=live`, also `SYNC_ENV=live`, a checkout
named `live`, or hostname `live`) is refused by default.
A live consumer (`SHOPWARE_DEPLOY_ENV=live`, a checkout named `live`, or
hostname `live`) is refused by default.

| Command | Gate |
| --- | --- |
| `db import` | `--allow-live` or `SYNC_ALLOW_LIVE_RESTORE=1` |
| `sync apply` / `sync pull` | `SYNC_ALLOW_LIVE_RESTORE=1` only |
| `backup recover` | `--i-understand-this-restores-this-host` (or `BACKUP_CONFIRM_RESTORE=1`), **and** `BACKUP_ALLOW_LIVE_RESTORE=1` when `SHOPWARE_DEPLOY_ENV=live` |
| `db import` | `--allow-live` or `SHOPWARE_ALLOW_LIVE_RESTORE=1` |
| `sync apply` / `sync pull` | `SHOPWARE_ALLOW_LIVE_RESTORE=1` only |
| `backup recover` | `--i-understand-this-restores-this-host` (or `BACKUP_CONFIRM_RESTORE=1`), **and** `SHOPWARE_ALLOW_LIVE_RESTORE=1` when `SHOPWARE_DEPLOY_ENV=live` |

`backup create` / `backup prune` are allowed on live (that is the cron path).
Opt-in URL rewrite after apply is never allowed on live. Passwords are never
printed.
URL rewrite after apply uses `APP_URL` and is skipped on live. Passwords are
never printed.

## Usage

Run from the shop checkout (directory with `.env`), or set `COMPOSE_DIR`.
`--dry-run` prints the plan and does not write, copy, import, or delete.

Every shopware verb uses one loader: process env, then `.env`, `.env.local` if
present, `.env.prod` if present. Later files win except identity / process-win
keys. Do not use `deploy/sync.env` or `deploy/backup.env`.

Identity: `SHOPWARE_SHOP_ID`, `SHOPWARE_DEPLOY_ENV`, optional
`SHOPWARE_DATA_BASE` (default `/var/lib/shopware/data`) or
`SHOPWARE_DATA_ROOT`. Laptop SSH: `SHOPWARE_SSH_HOST` / `USER` / `KEY` in
`.env.local` (host defaults to the `--from` alias). Remote live data:
`SHOPWARE_REMOTE_DATA_ROOT` or `{data_base}/{shop_id}/live`. Rewrite:
`APP_URL`. Live gate: `SHOPWARE_ALLOW_LIVE_RESTORE`.

### env init

Does not overwrite the whole `.env`. Does not invent MYSQL passwords or
Expand Down Expand Up @@ -206,9 +217,10 @@ After `sync local`: `shopware-cli project console cache:clear`.

### backup create / prune / recover

Needs `BACKUP_TARGET` (local path or SSH), `SHOPWARE_SHOP_ID`, and
`SHOPWARE_DEPLOY_ENV`. Create copies bind-mount trees and an operator-provided
dump (`BACKUP_DB_DUMP`) into a timestamped artifact — it does not dump. Prune
Needs `SHOPWARE_SHOP_ID` and `SHOPWARE_DEPLOY_ENV`. `BACKUP_TARGET` is a local
path or SSH URL (default `local` under the shop). SSH targets reuse
`SHOPWARE_SSH_*`. Create copies bind-mount trees and an operator-provided dump
(`BACKUP_DB_DUMP`) into a timestamped artifact — it does not dump. Prune
deletes stamp dirs older than `BACKUP_KEEP_DAYS` (default 14; `0` = keep
forever). Recover is disaster recovery onto **this host**, not live→staging
sync.
Expand All @@ -224,7 +236,7 @@ fyrst-cli shopware backup recover --artifact 20260912T020000Z \
--i-understand-this-restores-this-host --dry-run
fyrst-cli shopware backup recover --artifact 20260912T020000Z \
--i-understand-this-restores-this-host
BACKUP_ALLOW_LIVE_RESTORE=1 fyrst-cli shopware backup recover --artifact 20260912T020000Z \
SHOPWARE_ALLOW_LIVE_RESTORE=1 fyrst-cli shopware backup recover --artifact 20260912T020000Z \
--i-understand-this-restores-this-host
```

Expand Down
265 changes: 124 additions & 141 deletions docs/command-matrix.md

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ sync capture copies bind-mount trees for sync (not a retained backup).
sync apply loads a workdir onto this host; backup recover is off-host DR.

Live policy (refuse a live consumer unless):
db import --allow-live or SYNC_ALLOW_LIVE_RESTORE=1
sync apply SYNC_ALLOW_LIVE_RESTORE=1 only
sync pull SYNC_ALLOW_LIVE_RESTORE=1 only
backup recover confirm flag + BACKUP_ALLOW_LIVE_RESTORE=1
db import --allow-live or SHOPWARE_ALLOW_LIVE_RESTORE=1
sync apply SHOPWARE_ALLOW_LIVE_RESTORE=1 only
sync pull SHOPWARE_ALLOW_LIVE_RESTORE=1 only
backup recover confirm flag + SHOPWARE_ALLOW_LIVE_RESTORE=1
(--i-understand-this-restores-this-host or BACKUP_CONFIRM_RESTORE=1)

Dump = shopware-cli project dump only (fyrst-cli does not dump).
Expand Down Expand Up @@ -118,7 +118,7 @@ BACKUP_TARGET; it is allowed on live. \
(BACKUP_KEEP_DAYS, default 14, 0 = keep forever). \
`backup recover` fetches an artifact (`--artifact` / `--stamp`, alias `--from`) and applies it onto this host. It is not \
live→staging sync. Confirmation is required (`--i-understand-this-restores-this-host` \
or BACKUP_CONFIRM_RESTORE=1). Live needs BACKUP_ALLOW_LIVE_RESTORE=1.",
or BACKUP_CONFIRM_RESTORE=1). Live needs SHOPWARE_ALLOW_LIVE_RESTORE=1.",
after_help = SHOPWARE_COMMAND_TREE
)]
pub struct ShopwareArgs {
Expand Down Expand Up @@ -254,7 +254,7 @@ pub struct DbImportArgs {
#[arg(long)]
pub dry_run: bool,

/// Allow importing onto a live host (or set SYNC_ALLOW_LIVE_RESTORE=1)
/// Allow importing onto a live host (or set SHOPWARE_ALLOW_LIVE_RESTORE=1)
#[arg(long = "allow-live")]
pub allow_live: bool,
}
Expand Down
24 changes: 5 additions & 19 deletions src/shopware/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,20 @@ pub fn post_restore_hints(
) {
if rewrite_requested {
println!(
"==> Sales-channel domains: opt-in rewrite was requested (see SYNC_REWRITE_*). Payment/shipping webhooks may still need manual review."
"==> Sales-channel domains: rewrite used APP_URL. Payment/shipping webhooks may still need manual review."
);
} else {
println!(
"==> Sales-channel domains were not rewritten (default). Set SYNC_REWRITE_APP_URL=https://staging.example.com (or SYNC_REWRITE_URL_MAP) on a non-live consumer to rewrite sales_channel_domain after restore."
"==> Sales-channel domains were not rewritten. Set APP_URL on a non-live consumer to rewrite sales_channel_domain after restore."
);
if let Some(target) = env.get("SYNC_APP_URL").or_else(|| env.get("APP_URL")) {
if let Some(target) = env.get("APP_URL") {
println!(
"==> This shop APP_URL / SYNC_APP_URL={target} — destination storefront URL if you rewrite manually."
"==> This shop APP_URL={target} — destination storefront URL if you rewrite manually."
);
}
}
if dry_run {
println!(
"==> DRY-RUN would try cache:clear (non-fatal) and optional SYNC_POST_RESTORE_CMD"
);
println!("==> DRY-RUN would try cache:clear (non-fatal)");
return;
}
if env.get("IMAGE").is_some() && !files.is_empty() {
Expand Down Expand Up @@ -202,16 +200,4 @@ pub fn post_restore_hints(
println!("==> cache:clear skipped or failed — not fatal");
}
}
if let Some(cmd) = env.get("SYNC_POST_RESTORE_CMD") {
println!("==> Running SYNC_POST_RESTORE_CMD (non-fatal)");
let ok = Command::new("bash")
.args(["-lc", cmd])
.current_dir(compose_dir)
.status()
.map(|s| s.success())
.unwrap_or(false);
if !ok {
println!("==> SYNC_POST_RESTORE_CMD failed — not fatal");
}
}
}
28 changes: 12 additions & 16 deletions src/shopware/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,29 @@ pub fn run_with_env(
) -> Result<(), Error> {
let compose_dir = resolve_compose_dir(process_env, cwd)?;
let compose_dir = fs::canonicalize(&compose_dir).unwrap_or(compose_dir);
let mut env = ShopEnv::load(compose_dir.clone(), process_env)?;
env.load_backup_overlay(process_env)?;
let env = ShopEnv::load(compose_dir.clone(), process_env)?;

let shop_id = require_shop_id(&env)?;
let deploy_env = require_deploy_env(&env)?;
let selection = normalize_data(args.data.as_deref(), false, false)?;
let (project_name, _derived) = derive_project_name(&env)?;
let (data_root, data_root_derived) = resolve_backup_data_root(&env, &shop_id, &deploy_env);

let raw_target = env.get("BACKUP_TARGET").unwrap_or("").to_string();
let ssh_port = env.get("BACKUP_SSH_PORT").unwrap_or("22");
let mut target = parse_backup_target(&raw_target, ssh_port)?;
let raw_target = env
.get("BACKUP_TARGET")
.unwrap_or(super::env::DEFAULT_BACKUP_TARGET)
.to_string();
let mut target = parse_backup_target(&raw_target, "22")?;
if let BackupTarget::Local { path } = &target {
target = BackupTarget::Local {
path: resolve_local_target_path(path, &compose_dir),
};
}

let keep_days = prune::parse_keep_days(env.get("BACKUP_KEEP_DAYS"))?;
let ssh_key = env.get("BACKUP_SSH_KEY").map(PathBuf::from);
let ssh_key = env.get("SHOPWARE_SSH_KEY").map(PathBuf::from);
let db_dump = env.get("BACKUP_DB_DUMP").map(PathBuf::from);
let archive_image = env
.get("SYNC_ARCHIVE_IMAGE")
.unwrap_or(DEFAULT_ARCHIVE_IMAGE)
.to_string();
let archive_image = DEFAULT_ARCHIVE_IMAGE.to_string();
let now_unix = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
Expand Down Expand Up @@ -486,7 +484,7 @@ fn probe_ssh(ctx: &BackupCtx) -> Result<(), Error> {
.map_err(|e| Error::fail(format!("could not exec ssh: {e}")))?;
if !status.success() {
return Err(Error::fail(format!(
"SSH to {dest} failed (BatchMode). Check BACKUP_TARGET / BACKUP_SSH_KEY."
"SSH to {dest} failed (BatchMode). Check BACKUP_TARGET / SHOPWARE_SSH_KEY."
)));
}
Ok(())
Expand Down Expand Up @@ -610,17 +608,15 @@ mod tests {
}

#[test]
fn missing_backup_target_fails_clearly() {
fn default_backup_target_is_local() {
let shop = TempShop::new("no-tgt");
shop.write_shop("live");
let err = run_with_env(
run_with_env(
&args(Some("media"), true),
&process(shop.path(), &[]),
shop.path(),
)
.unwrap_err();
assert!(err.to_string().contains("BACKUP_TARGET"), "{err}");
assert!(!err.to_string().contains("not implemented"), "{err}");
.unwrap();
}

#[test]
Expand Down
44 changes: 23 additions & 21 deletions src/shopware/backup_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
//!
//! Not `sync apply` (live→staging clone). `--artifact` (aliases `--stamp`,
//! `--from`) and confirmation are required. Live needs
//! `BACKUP_ALLOW_LIVE_RESTORE=1`; inner apply then sets
//! `SYNC_ALLOW_LIVE_RESTORE=1`. DB import and bind-mount apply reuse the sync
//! apply module. fyrst-cli does not dump.
//! `SHOPWARE_ALLOW_LIVE_RESTORE`; inner apply then sets the same flag so
//! hostname/checkout named live still passes the apply guard. DB import and
//! bind-mount apply reuse the sync apply module. fyrst-cli does not dump.

use super::data::normalize_data;
use super::env::{
env_truthy, require_deploy_env, require_shop_id, resolve_compose_dir, resolve_data_root,
ShopEnv,
allow_live_restore, env_truthy, require_deploy_env, require_shop_id, resolve_compose_dir,
resolve_data_root, ShopEnv,
};
use super::error::Error;
use super::restore;
Expand All @@ -28,7 +28,7 @@ pub fn run(args: BackupRecoverArgs) -> Result<(), Error> {
let cwd = std::env::current_dir().map_err(|e| Error::fail(format!("cannot read cwd: {e}")))?;
let compose_dir = resolve_compose_dir(&process_env, &cwd)?;
let compose_dir = fs::canonicalize(&compose_dir).unwrap_or(compose_dir);
let mut env = ShopEnv::load_backup(compose_dir, &process_env)?;
let mut env = ShopEnv::load(compose_dir, &process_env)?;
restore_from_args(&args, &mut env, &cwd)
}

Expand Down Expand Up @@ -60,15 +60,15 @@ pub(crate) fn restore_from_args(
));
}

if deploy_env_is_live(env) && !env_truthy(env.get("BACKUP_ALLOW_LIVE_RESTORE")) {
if deploy_env_is_live(env) && !allow_live_restore(env) {
return Err(Error::fail(
"Refusing restore onto SHOPWARE_DEPLOY_ENV=live without BACKUP_ALLOW_LIVE_RESTORE=1 (disaster recovery only; quarterly drill on staging does not need this).",
"Refusing restore onto SHOPWARE_DEPLOY_ENV=live without SHOPWARE_ALLOW_LIVE_RESTORE=1 (disaster recovery only; quarterly drill on staging does not need this).",
));
}

// Overlay always sets this for inner `sync apply` so the existing live
// guard allows DR (hostname/checkout named live on a staging drill too).
env.set("SYNC_ALLOW_LIVE_RESTORE", "1");
// Inner `sync apply` uses the same live gate (hostname/checkout named live
// on a staging drill too).
env.set("SHOPWARE_ALLOW_LIVE_RESTORE", "1");

let dry_run = args.common.dry_run;
let selection = normalize_data(args.common.data.as_deref(), false, false)?;
Expand All @@ -92,7 +92,7 @@ pub(crate) fn restore_from_args(
println!("==> {n}");
}
println!(
"==> Restoring {} into {} (SYNC_ALLOW_LIVE_RESTORE for live DR)",
"==> Restoring {} into {} (SHOPWARE_ALLOW_LIVE_RESTORE for live DR)",
artifact.display(),
data_root,
);
Expand Down Expand Up @@ -147,8 +147,11 @@ fn fetch_artifact(

let shop_id = require_shop_id(env)?;
let deploy_env = require_deploy_env(env)?;
let default_port = env.get("BACKUP_SSH_PORT").unwrap_or("22");
let mut target = parse_backup_target(env.get("BACKUP_TARGET").unwrap_or(""), default_port)?;
let mut target = parse_backup_target(
env.get("BACKUP_TARGET")
.unwrap_or(super::env::DEFAULT_BACKUP_TARGET),
"22",
)?;
if let BackupTarget::Local { path } = &target {
target = BackupTarget::Local {
path: resolve_local_target_path(path, &env.compose_dir),
Expand Down Expand Up @@ -217,14 +220,14 @@ fn rsync_from_ssh(
"rsync is required to fetch backups from an SSH BACKUP_TARGET",
));
}
if let Some(key) = env.get("BACKUP_SSH_KEY") {
if let Some(key) = env.get("SHOPWARE_SSH_KEY") {
if !Path::new(key).is_file() {
return Err(Error::fail(format!("BACKUP_SSH_KEY not found: {key}")));
return Err(Error::fail(format!("SHOPWARE_SSH_KEY not found: {key}")));
}
}
fs::create_dir_all(dest)
.map_err(|e| Error::fail(format!("cannot create {}: {e}", dest.display())))?;
let ssh_e_opt = ssh_e(target, env.get("BACKUP_SSH_KEY"))?;
let ssh_e_opt = ssh_e(target, env.get("SHOPWARE_SSH_KEY"))?;
let ssh_target = target
.ssh_destination()
.ok_or_else(|| Error::fail("internal error: ssh_target on a local BACKUP_TARGET"))?;
Expand All @@ -239,7 +242,7 @@ fn rsync_from_ssh(
.map_err(|e| Error::fail(format!("failed to exec rsync: {e}")))?;
if !status.success() {
return Err(Error::fail(format!(
"rsync from {ssh_target} failed (BatchMode). Check BACKUP_TARGET / BACKUP_SSH_KEY."
"rsync from {ssh_target} failed (BatchMode). Check BACKUP_TARGET / SHOPWARE_SSH_KEY."
)));
}
Ok(())
Expand Down Expand Up @@ -312,7 +315,7 @@ mod tests {
"COMPOSE_DIR".into(),
shop.path().to_string_lossy().into_owned(),
);
ShopEnv::load_backup(shop.path().to_path_buf(), &process).unwrap()
ShopEnv::load(shop.path().to_path_buf(), &process).unwrap()
}

#[test]
Expand Down Expand Up @@ -350,15 +353,14 @@ mod tests {
let shop = TempShop::new("live");
shop.write_min("live");
let mut env = env_for(&shop);
env.set("SYNC_ALLOW_LIVE_RESTORE", "1");
let err = restore_from_args(
&args(Some("20260912T020000Z"), true, true, Some("db")),
&mut env,
shop.path(),
)
.unwrap_err();
assert!(
err.to_string().contains("BACKUP_ALLOW_LIVE_RESTORE"),
err.to_string().contains("SHOPWARE_ALLOW_LIVE_RESTORE"),
"{err}"
);
assert!(err.to_string().contains("live"), "{err}");
Expand Down
Loading
Loading