|
| 1 | +--- |
| 2 | +display_name: Archive |
| 3 | +description: Create automated and user-invocable scripts that archive and extract selected files/directories with optional compression (gzip or zstd). |
| 4 | +icon: ../../../../.icons/folder.svg |
| 5 | +verified: false |
| 6 | +tags: [backup, archive, tar, helper] |
| 7 | +--- |
| 8 | + |
| 9 | +# Archive |
| 10 | + |
| 11 | +This module installs small, robust scripts in your workspace to create and extract tar archives from a list of files and directories. It supports optional compression (gzip or zstd). The create command prints only the resulting archive path to stdout; operational logs go to stderr. An optional stop hook can also create an archive automatically when the workspace stops, and an optional start hook can wait for an archive on-disk and extract it on start. |
| 12 | + |
| 13 | +```tf |
| 14 | +module "archive" { |
| 15 | + count = data.coder_workspace.me.start_count |
| 16 | + source = "registry.coder.com/coder-labs/archive/coder" |
| 17 | + version = "0.0.1" |
| 18 | + agent_id = coder_agent.example.id |
| 19 | +
|
| 20 | + paths = ["./projects", "./code"] |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +## Features |
| 25 | + |
| 26 | +- Installs two commands into the workspace `$PATH`: `coder-archive-create` and `coder-archive-extract`. |
| 27 | +- Creates a single `.tar`, `.tar.gz`, or `.tar.zst` containing selected paths (depends on `tar`). |
| 28 | +- Optional compression: `gzip`, `zstd` (depends on `gzip` or `zstd`). |
| 29 | +- Stores defaults so commands can be run without arguments (supports overriding via CLI flags). |
| 30 | +- Logs and status messages go to stderr, the create command prints only the final archive path to stdout. |
| 31 | +- Optional: |
| 32 | + - `create_on_stop` to create an archive automatically when the workspace stops. |
| 33 | + - `extract_on_start` to wait for an archive to appear and extract it on start. |
| 34 | + |
| 35 | +> [!WARNING] |
| 36 | +> The `create_on_stop` feature uses the `coder_script` `run_on_stop` which may not work as expected on certain templates without additional provider configuration. The agent may be terminated before the script completes. See [coder/coder#6174](https://github.com/coder/coder/issues/6174) for provider-specific workarounds and [coder/coder#6175](https://github.com/coder/coder/issues/6175) for tracking a fix. |
| 37 | +
|
| 38 | +## Usage |
| 39 | + |
| 40 | +Basic example: |
| 41 | + |
| 42 | +```tf |
| 43 | +module "archive" { |
| 44 | + count = data.coder_workspace.me.start_count |
| 45 | + source = "registry.coder.com/coder-labs/archive/coder" |
| 46 | + version = "0.0.1" |
| 47 | + agent_id = coder_agent.example.id |
| 48 | +
|
| 49 | + # Paths to include in the archive (files or directories). |
| 50 | + directory = "~" |
| 51 | + paths = [ |
| 52 | + "./projects", |
| 53 | + "./code", |
| 54 | + ] |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +Customize compression and output: |
| 59 | + |
| 60 | +```tf |
| 61 | +module "archive" { |
| 62 | + count = data.coder_workspace.me.start_count |
| 63 | + source = "registry.coder.com/coder-labs/archive/coder" |
| 64 | + version = "0.0.1" |
| 65 | + agent_id = coder_agent.example.id |
| 66 | +
|
| 67 | + directory = "/" |
| 68 | + paths = ["/etc", "/home"] |
| 69 | + compression = "zstd" # "gzip" | "zstd" | "none" |
| 70 | + output_dir = "/tmp/backup" # defaults to /tmp |
| 71 | + archive_name = "my-backup" # base name (extension is inferred from compression) |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +Enable auto-archive on stop: |
| 76 | + |
| 77 | +```tf |
| 78 | +module "archive" { |
| 79 | + count = data.coder_workspace.me.start_count |
| 80 | + source = "registry.coder.com/coder-labs/archive/coder" |
| 81 | + version = "0.0.1" |
| 82 | + agent_id = coder_agent.example.id |
| 83 | +
|
| 84 | + # Creates /tmp/coder-archive.tar.gz of the users home directory (defaults). |
| 85 | + create_on_stop = true |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Extract on start: |
| 90 | + |
| 91 | +```tf |
| 92 | +module "archive" { |
| 93 | + count = data.coder_workspace.me.start_count |
| 94 | + source = "registry.coder.com/coder-labs/archive/coder" |
| 95 | + version = "0.0.1" |
| 96 | + agent_id = coder_agent.example.id |
| 97 | +
|
| 98 | + # Where to look for the archive file to extract: |
| 99 | + output_dir = "/tmp" |
| 100 | + archive_name = "my-archive" |
| 101 | + compression = "gzip" |
| 102 | +
|
| 103 | + # Waits up to 5 minutes for /tmp/my-archive.tar.gz to be present, note that |
| 104 | + # using a long timeout will delay every workspace start by this much until the |
| 105 | + # archive is present. |
| 106 | + extract_on_start = true |
| 107 | + extract_wait_timeout_seconds = 300 |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Command usage |
| 112 | + |
| 113 | +The installer writes the following files: |
| 114 | + |
| 115 | +- `$CODER_SCRIPT_DATA_DIR/archive-lib.sh` |
| 116 | +- `$CODER_SCRIPT_BIN_DIR/coder-archive-create` |
| 117 | +- `$CODER_SCRIPT_BIN_DIR/coder-archive-extract` |
| 118 | + |
| 119 | +Create usage: |
| 120 | + |
| 121 | +```console |
| 122 | +coder-archive-create [OPTIONS] [PATHS...] |
| 123 | + -c, --compression <gzip|zstd|none> Compression algorithm (default from module) |
| 124 | + -C, --directory <DIRECTORY> Change to directory for archiving (default from module) |
| 125 | + -f, --file <ARCHIVE> Output archive file (default from module) |
| 126 | + -h, --help Show help |
| 127 | +``` |
| 128 | + |
| 129 | +Extract usage: |
| 130 | + |
| 131 | +```console |
| 132 | +coder-archive-extract [OPTIONS] |
| 133 | + -c, --compression <gzip|zstd|none> Compression algorithm (default from module) |
| 134 | + -C, --directory <DIRECTORY> Extract into directory (default from module) |
| 135 | + -f, --file <ARCHIVE> Archive file to extract (default from module) |
| 136 | + -h, --help Show help |
| 137 | +``` |
| 138 | + |
| 139 | +Examples: |
| 140 | + |
| 141 | +- Use Terraform defaults: |
| 142 | + |
| 143 | + ``` |
| 144 | + coder-archive-create |
| 145 | + ``` |
| 146 | + |
| 147 | +- Override compression and output file at runtime: |
| 148 | + |
| 149 | + ``` |
| 150 | + coder-archive-create --compression zstd --file /tmp/backups/archive.tar.zst |
| 151 | + ``` |
| 152 | + |
| 153 | +- Add extra paths on the fly (in addition to the Terraform defaults): |
| 154 | + |
| 155 | + ``` |
| 156 | + coder-archive-create /etc/hosts |
| 157 | + ``` |
| 158 | + |
| 159 | +- Extract an archive into a directory: |
| 160 | + |
| 161 | + ``` |
| 162 | + coder-archive-extract --file /tmp/backups/archive.tar.gz --directory /tmp/restore |
| 163 | + ``` |
0 commit comments