This file contains repository-specific instructions for AI coding agents working on GL.iNet Cross-Model Backup, Migration and Recovery.
Repository:
zippyy/GL.iNet-CrossModel-BackupRestoreUtility
Primary development branch:
main
Unless the task explicitly says otherwise, treat the router-native implementation on main as authoritative. The docker branch is a separate experimental product line and must not be mixed into the router runtime or IPK.
This project provides a native OpenWrt/LuCI backup, migration, cloning, and disaster-recovery utility for GL.iNet firmware 4.x and generic OpenWrt.
A single IPK can operate in two roles:
-
Local / direct mode
- Back up the router on which the package is installed.
- Inspect archives.
- Validate archives.
- Restore archives.
- Review/install compatible packages.
-
Controller mode
- Manage other routers over SSH.
- Remote routers do not require the IPK.
- The controller streams the same shell runtime to the remote router.
- Temporary remote files must be cleaned afterward.
Do not create separate implementations for local and remote behavior unless technically unavoidable.
Shared behavior should remain in the shared runtime wherever possible.
When modifying this repository, optimize for these priorities in this order:
- Do not brick routers.
- Do not destroy remote management access.
- Do not leak credentials or device identity.
- Do not weaken archive validation.
- Do not weaken rollback behavior.
- Preserve cross-model portability rules.
- Preserve compatibility with constrained OpenWrt systems.
- Keep local and agentless-remote behavior consistent.
- Maintain backward compatibility where reasonably possible.
- Improve usability, diagnostics, and maintainability.
A backup utility is safety-critical infrastructure.
Prefer refusing an unsafe restore over attempting to be clever.
The OpenWrt package lives primarily under:
openwrt/luci-app-glinet-crossmodel-backup/
Important files include:
openwrt/luci-app-glinet-crossmodel-backup/
├── Makefile
└── root/
├── etc/
│ ├── config/
│ │ └── glinet_crossmodel
│ └── init.d/
│ └── gcm-glui-integration
├── lib/
│ └── upgrade/
│ └── keep.d/
│ └── glinet-crossmodel
├── usr/
│ ├── bin/
│ │ └── glinet-crossmodel
│ ├── lib/
│ │ ├── glinet-crossmodel/
│ │ │ └── core.sh
│ │ └── lua/luci/
│ │ ├── controller/
│ │ │ └── glinet_crossmodel.lua
│ │ └── view/glinet_crossmodel/
│ │ └── index.htm
│ └── libexec/
│ ├── glinet-crossmodel-remote
│ ├── glinet-crossmodel-backup
│ ├── glinet-crossmodel-validate
│ └── gcm-glui-integrate
└── www/
└── js/
└── gcm-glui-hook.js
Other important areas:
tests/
scripts/
docs/
.github/workflows/
README.md
STATUS.md
Before making significant changes, inspect the relevant implementation rather than assuming behavior from filenames or documentation alone.
core.sh contains the majority of backup, validation, compatibility, restore, sanitization, adaptation, package, and rollback logic.
It is especially important because it serves two environments:
- sourced locally by the installed CLI;
- concatenated with the CLI and streamed over SSH for agentless remote operation.
Therefore:
core.sh must remain BusyBox ash / POSIX-shell compatible.
Do not introduce dependencies on:
- Bash
- Python
- Node.js
- Perl
- GNU-only utilities
- package-local files unavailable on remote targets
unless the feature explicitly detects their availability and has a safe fallback.
Assume a remote endpoint may contain only normal OpenWrt/BusyBox tooling.
This is the user-facing CLI.
Major commands include:
glinet-crossmodel facts
glinet-crossmodel create
glinet-crossmodel inspect
glinet-crossmodel validate
glinet-crossmodel restore
glinet-crossmodel packages
glinet-crossmodel versionKeep command-line interfaces stable unless the requested feature requires a change.
Prefer adding optional flags over breaking existing invocation forms.
This is the agentless SSH coordinator.
It handles:
- SSH connection setup
- password/key/agent authentication
- known_hosts
- runtime streaming
- SCP transfers
- SHA-256 transfer verification
- remote temporary files
- remote cleanup
- remote create
- remote validate
- remote restore
- remote package review
Remote endpoints should not need the IPK installed.
Do not accidentally introduce a dependency on files existing under /usr/lib/glinet-crossmodel/ on the remote router.
The main LuCI backend is:
root/usr/lib/lua/luci/controller/glinet_crossmodel.lua
It handles:
- API routing
- CSRF validation
- saved router inventory
- local operations
- remote operations
- archive upload/download
- profile storage
- storage limits
- retention
- SSH credentials
- backend command invocation
Do not move security-sensitive validation solely into JavaScript.
The server side must remain authoritative.
This is a critical rule.
Several CLI commands produce machine-readable output consumed by the LuCI controller.
Examples include:
facts
inspect
validate
packages
Do not casually add:
echo "Doing something..."to code paths used by these commands.
A harmless-looking debug line can turn valid JSON into:
Starting validation...
{"valid":true}
and break the application.
- Machine-readable stdout must remain machine-readable.
- Diagnostic output should use stderr, syslog, or the project's logging subsystem.
- Human-readable create/restore status may continue using the established output conventions.
- When changing logging, explicitly test JSON-producing commands with a JSON parser.
Treat stdout compatibility as part of the public API.
Current archives use format v2 with the dedicated prefix:
glinet-crossmodel/
Typical contents:
glinet-crossmodel/
├── manifest.json
├── backup-info.txt
├── checksums.sha256
├── packages.json
├── portable/
├── uci/
├── extra/
├── artifacts/
└── source/
Do not weaken archive validation.
The implementation must continue protecting against:
- absolute paths
..traversal- unsafe member names
- unexpected top-level members
- duplicate member names
- symlinks
- hard links
- device files
- FIFOs
- sockets
- malformed archives
- excessive archive members
- incomplete checksum coverage
- duplicate checksum entries
- SHA-256 mismatches
- unsupported manifest versions
Validation should occur before extraction/application whenever possible.
Never replace safe extraction logic with a bare:
tar -xzf "$archive" -C /or equivalent.
There are four major strategies.
Purpose:
Cross-model migration.
Portable profiles describe configuration semantically instead of copying source hardware topology.
Do not directly restore source-specific values such as:
radio0,radio1, etc.- Ethernet device assignments
- DSA/switch topology
- source physical interfaces
- factory MAC addresses
- host keys
- cloud identity
- raw flash state
Wi-Fi should be mapped using target capabilities such as:
- band
- role
- available radios
- target logical networks
Portable restore should adapt to the target.
Purpose:
Replicate configuration to another router of the same model.
Clone must retain its same-model requirement.
Device-bound identity must continue to be sanitized or preserved from the target as appropriate.
Do not allow convenience changes to accidentally clone:
- MAC identity
- host SSH keys
- TLS private identity
- GL cloud identity
- node/device identity
- private VPN identity
unless explicitly intended by the design.
Purpose:
Same-model remote deployment without disconnecting the administrator.
This mode must prioritize continued remote management.
Preserve target-local management mechanisms including, where applicable:
- active SSH management path
- authorized keys
- WAN management
- GoodCloud
- rtty
- Tailscale
- ZeroTier
- other detected management state
When an active SSH connection would be disrupted, source connectivity configuration may need to be staged rather than activated immediately.
Do not weaken these protections.
Purpose:
Disaster recovery of the exact physical router.
Device Snapshot must remain bound to the target device fingerprint.
A same-model match alone is not sufficient.
A fingerprint mismatch should fail unless the user explicitly invokes the dangerous override.
Never infer or silently enable that override.
Restore operations must follow the safety sequence.
Broadly:
archive
↓
inspect
↓
validate structure
↓
validate manifest
↓
verify checksums
↓
validate strategy compatibility
↓
create pre-restore snapshot
↓
apply configuration
↓
verify / complete
↓
activate only when appropriate
Before mutating the target, a pre-restore rollback snapshot is mandatory.
Do not make rollback snapshots optional merely to simplify an implementation.
If snapshot creation fails, restore should normally fail before applying changes.
Rollback behavior is a core feature.
Changes involving restore must consider:
- which files will be modified;
- which files will be created;
- which UCI packages will change;
- what must be restored if an intermediate step fails;
- how rollback integrity is verified.
A restore failure after partial application must not simply return an error and leave the target in an unknown state when rollback is possible.
When modifying restore code, test at least one deliberately injected mid-restore failure when practical.
Remote restores are different from local restores.
Do not reload network, wireless, or firewall state prematurely during a remote operation if doing so can terminate the SSH connection before the controller receives the result.
The implementation deliberately supports deferred activation.
Preserve this behavior.
A successful remote restore should be able to report success before connectivity-affecting reload/reboot actions occur.
Do not casually copy source identity to another router.
Potential device-bound data includes:
- MAC addresses
- SSH host keys
- TLS private keys
- private WireGuard keys
- preshared keys
- certificates
- cloud identifiers
- GL DDNS/device registration
- GoodCloud identity
- rtty identity
- ZeroTier identity
- Tailscale state
- serial-bound data
- hardware identifiers
- vendor provisioning state
When uncertain whether a value represents device identity, preserve the target value or exclude the value until the behavior is understood.
Never print, log, commit, persist, or expose:
- passwords
- SSH private-key contents
- WireGuard private keys
- preshared keys
- API tokens
- authentication tokens
- CSRF tokens
- session IDs
- TLS private keys
- sensitive credential files
Passwords for remote routers are intentionally handled using transient mode-0600 files.
They must not be:
- stored in router inventory;
- passed on the command line;
- logged;
- returned in API responses.
Saved router definitions may contain authentication metadata and private-key paths but never password values.
Do not weaken SSH host verification for convenience.
The controller intentionally maintains:
/root/.ssh/known_hosts
Current behavior uses deliberate first-contact enrollment followed by mismatch rejection.
Saved router inventory may also contain a verified fingerprint.
Do not replace this with:
StrictHostKeyChecking=no
Do not silently delete and recreate known_hosts entries when a key changes.
A changed host key is a security event and should require deliberate user action.
Agentless operations create temporary endpoint files.
Every success and failure path must consider cleanup.
Use traps where appropriate.
Temporary files should be:
- unpredictable or operation-ID scoped;
- stored in
/tmpwhere appropriate; - removed after operations;
- removed after interrupted operations where reasonably possible.
Do not leave archives, credential material, scripts lists, or other operation artifacts behind without a reason.
Package restoration is intentionally conservative.
Important rules:
- package installation is optional;
- feed failure must not invalidate an otherwise successful configuration restore;
- packages require explicit selection where applicable;
- kernel modules must not be automatically restored;
- architecture compatibility must be checked;
- already-installed packages should not be unnecessarily reinstalled.
Never implement "restore every package from the old router" blindly.
Especially do not automatically install:
kmod-*
kernel-specific packages
hardware-specific packages
from another firmware/kernel environment.
Custom paths are security-sensitive.
Continue validating paths before reading or writing them.
Reject unsafe forms including traversal.
Custom binaries require compatibility checks.
CPU architecture equality does not guarantee runtime compatibility.
Do not remove ELF validation merely because two routers report the same architecture.
The utility supports both:
- GL.iNet firmware 4.x
- generic OpenWrt
The platform adapter distinguishes behavior.
Where GL.iNet exposes a supported local interface, prefer documented/discovered capabilities.
Do not invent or assume undocumented GL RPC APIs.
Capability probe first.
Fallback safely to standard UCI where supported.
A feature must not unnecessarily make generic OpenWrt unusable.
Assume the target router may have:
- limited RAM;
- limited
/tmp; - slow flash;
- BusyBox tools;
- older OpenWrt userspace;
- limited package availability.
Avoid:
- large dependencies;
- unnecessary background daemons;
- unbounded logs;
- repeated archive extraction;
- unnecessary hashing;
- huge in-memory objects;
- writing verbose temporary data to persistent flash;
- excessive process spawning in hot loops.
Favor straightforward shell and Lua over adding heavyweight runtimes.
Files intended for router execution must remain compatible with their declared shell.
For core.sh and streamed CLI logic:
Do not use Bash-only syntax.
Avoid constructs such as:
[[ ... ]]
arrays=(...)
${array[@]}
<(process substitution)
declare
local -nunless the file explicitly runs under Bash, which core router code should not.
Prefer:
case
test / [
while
for
sed
awk
uci
jsonfilter
ubuswhen appropriate.
Quote variable expansions unless intentional splitting is required.
Be especially careful with:
- filenames
- user-provided strings
- SSH arguments
- command construction
- temporary paths
Server-side validation is mandatory.
JavaScript validation is only a convenience.
For state-changing LuCI API operations:
- require POST;
- require valid CSRF;
- validate all input;
- sanitize identifiers and paths;
- return useful HTTP status codes;
- do not return secrets.
Use existing helpers and conventions before adding parallel abstractions.
Do not build shell commands from unsanitized request values.
The LuCI UI should be functional first.
Do not redesign the entire interface while fixing an unrelated backend issue.
Preserve support for:
- local router mode;
- remote router mode;
- saved router inventory;
- backup strategy selection;
- category selection;
- validation;
- package review;
- restore warnings;
- dangerous overrides;
- profile management.
Destructive or dangerous operations should be explicit.
Do not hide important safety warnings simply to make the workflow shorter.
Logging should be useful but must not break command output.
Prefer structured messages containing information such as:
operation
component
stage
scope
strategy
result
reason
Never log secret values.
Good:
INFO component=ssh host=192.168.8.1 auth=password stage=connect
Bad:
DEBUG password=hunter2
For UCI changes, log the option name if useful but redact sensitive values.
Examples of sensitive option names include:
password
passwd
secret
token
authkey
private_key
privatekey
preshared_key
tls_auth
tls_crypt
When adding logging, remember the machine-readable stdout rule.
Do not hide valuable failures with unconditional patterns such as:
some-important-command >/dev/null 2>&1 || trueunless failure is deliberately non-fatal and understood.
If a failure is intentionally tolerated:
- document why;
- preserve enough diagnostic information;
- avoid changing the final result incorrectly.
Errors should identify the stage that failed.
Prefer:
Could not calculate SHA-256 on the remote router.
over:
Operation failed.
Do not expose secrets just to provide better errors.
Do not perform broad unrelated refactors while implementing a focused task.
In particular, avoid simultaneously rewriting:
- archive format;
- restore engine;
- LuCI UI;
- SSH layer;
- logging;
- package handling;
unless the requested task actually requires those changes.
Small diffs are easier to reason about in a router recovery tool.
When architectural refactoring is required, preserve behavior first and improve structure second.
Do not hand-edit generated build output when the source file exists elsewhere.
The IPK build should package the checked-in runtime source.
Changes should be made to authoritative source files and then rebuilt.
Do not commit arbitrary test archives, credentials, router backups, private keys, or large generated artifacts.
Before considering a meaningful code change complete, run the repository test suite where possible:
sh tests/run.shThen build the OpenWrt package:
bash scripts/build-openwrt-ipk.shInspect the resulting package/archive as appropriate.
Do not claim tests passed unless they were actually executed.
If the environment prevents a test from running, state that clearly.
For modified shell code, perform syntax validation.
Examples:
sh -n path/to/fileRun ShellCheck when available and relevant.
Remember that ShellCheck success does not prove BusyBox runtime compatibility.
For modified Lua files, perform syntax validation using an available Lua compiler/interpreter where practical.
Do not assume desktop Lua and target LuCI versions expose identical APIs.
Changes involving LuCI behavior may still require router testing.
If modifying:
- logging;
- CLI dispatch;
- facts;
- inspect;
- validation;
- package review;
- command invocation;
explicitly verify that machine-readable output remains valid.
At minimum check applicable commands such as:
glinet-crossmodel facts
glinet-crossmodel inspect ...
glinet-crossmodel validate ...
glinet-crossmodel packages ...Parse the result.
Do not only visually inspect it.
Changes involving archives, extraction, paths, manifests, or checksums should cover hostile cases such as:
../ traversal
absolute paths
duplicate members
symlinks
hard links
bad checksums
missing checksums
unexpected top-level paths
malformed manifest
unsupported version
A security regression in archive handling is a blocking issue.
Changes to restoration should exercise:
- success path;
- validation failure;
- partial apply failure;
- rollback path;
- rollback verification;
- category selection;
- strategy-specific behavior.
For connectivity-related changes also consider deferred remote activation.
Changes affecting remote mode should consider:
- password authentication;
- key authentication;
- SSH agent authentication;
- connection timeout;
- authentication failure;
- changed host key;
- SCP upload;
- SCP download;
- SHA-256 verification;
- interrupted operations;
- remote cleanup;
- IPv4;
- IPv6 where applicable.
Do not mark remote behavior validated based solely on local mode tests.
Unit/integration tests intentionally cannot prove every router-specific behavior.
Do not state that hardware/firmware support is verified unless it was actually tested.
Important real-router validation areas include:
- GL.iNet 4.x
ubusUCI behavior; - MediaTek radio mapping;
- Qualcomm radio mapping;
- Wi-Fi 6E;
- Wi-Fi 7;
- MLO;
- GL DDNS;
- WireGuard;
- OpenVPN;
- AmneziaWG;
- persistent-file discovery;
- Dropbear/SCP differences;
- Remote-Safe management path preservation;
- rollback after deliberately injected failure;
- GL Admin Panel integration.
Distinguish:
implemented
from:
tested in automation
from:
validated on real hardware
Update documentation when changing:
- command syntax;
- archive format;
- safety behavior;
- restore strategy behavior;
- configuration options;
- LuCI workflow;
- package requirements;
- supported functionality.
Keep README examples aligned with actual behavior.
Do not document a capability as verified merely because code for it exists.
Do not bump versions merely because files changed.
If the task requires a release/version bump, inspect:
openwrt/luci-app-glinet-crossmodel-backup/Makefile
and the existing release workflow before changing version or package release numbers.
Keep:
- tool version;
- OpenWrt package version;
- package release;
- GitHub release/tag behavior
consistent.
When preparing a commit:
- keep changes focused;
- do not include credentials;
- do not include router backups;
- do not include unrelated formatting churn;
- do not modify generated files unnecessarily;
- describe safety-impacting changes explicitly.
Useful commit prefixes include:
fix:
feat:
security:
logging:
docs:
test:
build:
release:
Use the repository's recent history as the stronger guide when conventions differ.
For any non-trivial task:
- Confirm the target branch.
- Read the relevant implementation.
- Trace the complete call path.
- Identify local-mode implications.
- Identify remote-mode implications.
- Identify archive-format implications.
- Identify identity/security implications.
- Identify rollback implications.
- Identify JSON/stdout implications.
- Inspect existing tests covering the area.
Do not implement based solely on README descriptions.
Review the final diff and answer these questions:
- Could this brick a router?
- Could this cause loss of remote access?
- Could this weaken validation?
- Could this bypass rollback?
- Could this copy source device identity?
- Could this expose a secret?
- Could this break remote agentless mode?
- Could this break BusyBox compatibility?
- Could this contaminate JSON stdout?
- Could this leave remote temporary files behind?
- Could this install incompatible packages?
- Could this behave differently between local and remote mode unintentionally?
If the answer to any is "possibly", investigate before declaring the task complete.
When finishing a substantial task, report:
Briefly describe the implemented behavior.
List the meaningful files modified.
State whether the change affects:
- restore behavior;
- rollback;
- identity;
- SSH;
- archive validation;
- connectivity.
List the exact tests actually run.
Example:
PASS sh tests/run.sh
PASS sh -n core.sh
PASS IPK build
NOT RUN real-router smoke test — no router available
Clearly identify anything requiring physical GL.iNet/OpenWrt hardware.
Do not hide untested areas.
The following rules should be treated as hard constraints unless a task explicitly requires redesigning them:
DO NOT:
- disable archive safety checks;
- bypass checksums;
- silently bypass model/fingerprint restrictions;
- silently enable dangerous restore overrides;
- disable SSH host-key verification;
- persist router passwords;
- put passwords on argv;
- log credentials;
- clone private device identity unintentionally;
- automatically restore kernel modules;
- reload connectivity during remote restore before a safe response point;
- make
core.shdepend on the IPK being installed remotely; - introduce Bash requirements into the streamed runtime;
- contaminate JSON stdout;
- remove mandatory pre-restore rollback creation;
- claim real-hardware validation that did not occur.
When safety and convenience conflict, choose safety.
Agents working in this repository should be proactive.
If you discover a small defect directly related to the requested change, fix it when doing so is low risk and testable.
If you discover a larger unrelated defect:
- document it;
- explain its impact;
- leave it for a separate change unless it blocks the current work.
Do not ask for permission for every ordinary implementation detail.
Inspect the code, make the best technically sound decision consistent with this document, implement it, and test it.
The goal is a backup and recovery tool that can be trusted on real GL.iNet and OpenWrt routers—not merely code that looks correct in a desktop development environment.