diff --git a/CHANGELOG.md b/CHANGELOG.md index a1fe213..b2b09bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,17 @@ that only restate the release note above them. ### Fixed +- **`roll restore` and `roll pull` no longer litter the project with `*.backup.` files.** + Before overwriting `.env.roll`, `app/etc/env.php`, `auth.json`, `composer.json` or + `composer.lock` with the archived copy, the restore saved the old file beside it, so every pull + whose files differed from the build server's left another copy in the project root and `app/etc` + (1.3 MB per `composer.lock`, credentials in `env.php` and `auth.json`), usually hidden by the + project's `.gitignore`. The restore now overwrites without a copy: tracked files come back with + `git checkout`, and after a pull the archived `env.php` and `auth.json` are the ones wanted. +- **A failed decryption no longer empties `env.php` or `auth.json`.** Encrypted configuration was + decrypted straight into the target, which the shell truncates before gpg runs, so a wrong + passphrase or a damaged archive left an empty file. It now decrypts into a temporary file and + writes the target only on success. - **RedisInsight's preset connection reaches the store.** It pointed at `-redis`, a container hostname only the Redis fragment sets, so it did not resolve when Dragonfly or Valkey ran the service; it now uses the service name `redis`. diff --git a/docs/backup-restore.md b/docs/backup-restore.md index da89b05..9aec065 100644 --- a/docs/backup-restore.md +++ b/docs/backup-restore.md @@ -204,6 +204,16 @@ The system automatically backs up relevant configuration files: - `config/database.yml` - Rails database configuration - Other framework-specific configuration files +### Files a Restore Replaces + +`roll restore` (and `roll pull`, which runs it) writes the archived copies over the project's own +files without keeping the old ones. Tracked files such as `.env.roll`, `composer.json` and +`composer.lock` come back with `git checkout -- `; for untracked ones such as +`app/etc/env.php` and `auth.json`, copy anything you want to keep before restoring. + +Earlier versions saved every overwritten file as `.backup.` next to it. Those leftovers +can be deleted; `find . -maxdepth 3 -name '*.backup.[0-9]*' -not -path './vendor/*'` lists them. + ## Metadata and Integrity Each backup includes comprehensive metadata: diff --git a/utils/backup.sh b/utils/backup.sh index 1a7a056..63846fb 100644 --- a/utils/backup.sh +++ b/utils/backup.sh @@ -571,6 +571,27 @@ function performLegacyMigration() { fi } +## decryptConfigInto +## Decrypt into a private temporary file next to the target and write the target only once gpg has +## succeeded. Redirecting gpg straight into the target truncated it before decryption started, so a +## wrong passphrase or a damaged archive left an empty env.php or auth.json, and the restore keeps no +## copy of the original. `cat >` rather than `mv` keeps the target's own owner and mode. +function decryptConfigInto() { + local source_file="$1" target_path="$2" tmp_file="" + + tmp_file="$(mktemp "${target_path}.restore.XXXXXX")" || return 1 + + if echo "$RESTORE_DECRYPT" | gpg --batch --yes --quiet --passphrase-fd 0 --decrypt "$source_file" > "$tmp_file" \ + && cat "$tmp_file" > "$target_path" + then + rm -f "$tmp_file" + return 0 + fi + + rm -f "$tmp_file" + return 1 +} + function restoreConfigurations() { local backup_path="$1" local step="$2" @@ -621,7 +642,7 @@ function restoreConfigurations() { if [[ $is_encrypted == true ]]; then # Decrypt the file directly to target location if [[ -n "$RESTORE_DECRYPT" ]]; then - if echo "$RESTORE_DECRYPT" | gpg --batch --yes --quiet --passphrase-fd 0 --decrypt "$source_file" > "$target_path"; then + if decryptConfigInto "$source_file" "$target_path"; then logMessage INFO "Decrypted and restored $file" else logMessage ERROR "Failed to decrypt $file" @@ -667,22 +688,15 @@ function restoreConfigurations() { # Create target directory if needed mkdir -p "$(dirname "$target_path")" - # Backup existing file if it exists - if [[ -f "$target_path" ]]; then - if [[ $is_encrypted == true ]]; then - # For encrypted files, we can't easily compare so always backup - cp "$target_path" "$target_path.backup.$(date +%s)" - logMessage INFO "Backed up existing $relative_path" - elif ! cmp -s "$config_file" "$target_path"; then - cp "$target_path" "$target_path.backup.$(date +%s)" - logMessage INFO "Backed up existing $relative_path" - fi - fi - + # The existing file is overwritten without keeping a copy. Restores used to save it as + # `.backup.` beside the original, so every `roll pull` whose archived + # .env.roll, env.php, auth.json or composer.lock differed left more of them in the project + # root and app/etc (credentials included). Nobody used them: the tracked files come back + # with `git checkout`, and for env.php and auth.json the archived copy is the one wanted. if [[ $is_encrypted == true ]]; then # Decrypt the file if [[ -n "$RESTORE_DECRYPT" ]]; then - if echo "$RESTORE_DECRYPT" | gpg --batch --yes --quiet --passphrase-fd 0 --decrypt "$config_file" > "$target_path"; then + if decryptConfigInto "$config_file" "$target_path"; then logMessage INFO "Decrypted and restored $relative_path" else logMessage ERROR "Failed to decrypt $relative_path"