added install script for agent

changed backend to agent
This commit is contained in:
Philipp
2026-06-04 14:05:50 +02:00
parent 7f2785fb05
commit 0b059aec1d
669 changed files with 767 additions and 70582 deletions
+17
View File
@@ -0,0 +1,17 @@
AWS_ACCESS_KEY_ID="your_s3_key"
AWS_SECRET_ACCESS_KEY="your_s3_secret"
RESTIC_REPOSITORY="s3:https://s3.eu-central-1.amazonaws.com/bucket-name"
RESTIC_PASSWORD="restic_encryption_password"
ZFS_POOL_NAME="incus-pool"
RESTIC_KEEP_HOURLY=0
RESTIC_KEEP_DAILY=7
RESTIC_KEEP_WEEKLY=0
RESTIC_KEEP_MONTHLY=0
HOST="0.0.0.0"
PORT=3000
API_TOKEN="change-me-to-at-least-32-characters"
AGENT_DATABASE_PATH="./agent.sqlite"
HTTPS_ENABLED=false
TLS_CERT_FILE=""
TLS_KEY_FILE=""
ALLOWED_MANAGEMENT_IPS=""
+867
View File
@@ -0,0 +1,867 @@
{
"name": "incus-backup-ui-agent",
"version": "0.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "incus-backup-ui-agent",
"version": "0.1.0",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.21.2"
}
},
"node_modules/accepts": {
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
"integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==",
"license": "MIT",
"dependencies": {
"mime-types": "~2.1.34",
"negotiator": "0.6.3"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==",
"license": "MIT"
},
"node_modules/body-parser": {
"version": "1.20.5",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.5.tgz",
"integrity": "sha512-3grm+/2tUOvu2cjJkvsIxrv/wVpfXQW4PsQHYm7yk4vfpu7Ekl6nEsYBoJUL6qDwZUx8wUhQ8tR2qz+ad9c9OA==",
"license": "MIT",
"dependencies": {
"bytes": "~3.1.2",
"content-type": "~1.0.5",
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "~1.2.0",
"http-errors": "~2.0.1",
"iconv-lite": "~0.4.24",
"on-finished": "~2.4.1",
"qs": "~6.15.1",
"raw-body": "~2.5.3",
"type-is": "~1.6.18",
"unpipe": "~1.0.0"
},
"engines": {
"node": ">= 0.8",
"npm": "1.2.8000 || >= 1.4.16"
}
},
"node_modules/bytes": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
"integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/call-bind-apply-helpers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz",
"integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0",
"function-bind": "^1.1.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/call-bound": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz",
"integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==",
"license": "MIT",
"dependencies": {
"call-bind-apply-helpers": "^1.0.2",
"get-intrinsic": "^1.3.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/content-disposition": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
"integrity": "sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==",
"license": "MIT",
"dependencies": {
"safe-buffer": "5.2.1"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/content-type": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz",
"integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/cookie": {
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz",
"integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/cookie-signature": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.7.tgz",
"integrity": "sha512-NXdYc3dLr47pBkpUCHtKSwIOQXLVn8dZEuywboCOJY/osA0wFSLlSawr3KN8qXJEyX66FcONTH8EIlVuK0yyFA==",
"license": "MIT"
},
"node_modules/cors": {
"version": "2.8.6",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.6.tgz",
"integrity": "sha512-tJtZBBHA6vjIAaF6EnIaq6laBBP9aq/Y3ouVJjEfoHbRBcHBAHYcMh/w8LDrk2PvIMMq8gmopa5D4V8RmbrxGw==",
"license": "MIT",
"dependencies": {
"object-assign": "^4",
"vary": "^1"
},
"engines": {
"node": ">= 0.10"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/express"
}
},
"node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"license": "MIT",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
"integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/destroy": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
"integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==",
"license": "MIT",
"engines": {
"node": ">= 0.8",
"npm": "1.2.8000 || >= 1.4.16"
}
},
"node_modules/dotenv": {
"version": "16.6.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.6.1.tgz",
"integrity": "sha512-uBq4egWHTcTt33a72vpSG0z3HnPuIl6NqYcTrKEg2azoEyl2hpW0zqlxysq2pK9HlDIHyHyakeYaYnSAwd8bow==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/dunder-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
"integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==",
"license": "MIT",
"dependencies": {
"call-bind-apply-helpers": "^1.0.1",
"es-errors": "^1.3.0",
"gopd": "^1.2.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==",
"license": "MIT"
},
"node_modules/encodeurl": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz",
"integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/es-define-property": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
"integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-errors": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",
"integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-object-atoms": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz",
"integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==",
"license": "MIT"
},
"node_modules/etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/express": {
"version": "4.22.2",
"resolved": "https://registry.npmjs.org/express/-/express-4.22.2.tgz",
"integrity": "sha512-IuL+Elrou2ZvCFHs18/CIzy2Nzvo25nZ1/D2eIZlz7c+QUayAcYoiM2BthCjs+EBHVpjYjcuLDAiCWgeIX3X1Q==",
"license": "MIT",
"dependencies": {
"accepts": "~1.3.8",
"array-flatten": "1.1.1",
"body-parser": "~1.20.5",
"content-disposition": "~0.5.4",
"content-type": "~1.0.4",
"cookie": "~0.7.1",
"cookie-signature": "~1.0.6",
"debug": "2.6.9",
"depd": "2.0.0",
"encodeurl": "~2.0.0",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"finalhandler": "~1.3.1",
"fresh": "~0.5.2",
"http-errors": "~2.0.0",
"merge-descriptors": "1.0.3",
"methods": "~1.1.2",
"on-finished": "~2.4.1",
"parseurl": "~1.3.3",
"path-to-regexp": "~0.1.12",
"proxy-addr": "~2.0.7",
"qs": "~6.15.1",
"range-parser": "~1.2.1",
"safe-buffer": "5.2.1",
"send": "~0.19.0",
"serve-static": "~1.16.2",
"setprototypeof": "1.2.0",
"statuses": "~2.0.1",
"type-is": "~1.6.18",
"utils-merge": "1.0.1",
"vary": "~1.1.2"
},
"engines": {
"node": ">= 0.10.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/express"
}
},
"node_modules/finalhandler": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.2.tgz",
"integrity": "sha512-aA4RyPcd3badbdABGDuTXCMTtOneUCAYH/gxoYRTZlIJdF0YPWuGqiAsIrhNnnqdXGswYk6dGujem4w80UJFhg==",
"license": "MIT",
"dependencies": {
"debug": "2.6.9",
"encodeurl": "~2.0.0",
"escape-html": "~1.0.3",
"on-finished": "~2.4.1",
"parseurl": "~1.3.3",
"statuses": "~2.0.2",
"unpipe": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/forwarded": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
"integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
"integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/function-bind": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/get-intrinsic": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
"integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==",
"license": "MIT",
"dependencies": {
"call-bind-apply-helpers": "^1.0.2",
"es-define-property": "^1.0.1",
"es-errors": "^1.3.0",
"es-object-atoms": "^1.1.1",
"function-bind": "^1.1.2",
"get-proto": "^1.0.1",
"gopd": "^1.2.0",
"has-symbols": "^1.1.0",
"hasown": "^2.0.2",
"math-intrinsics": "^1.1.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/get-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz",
"integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==",
"license": "MIT",
"dependencies": {
"dunder-proto": "^1.0.1",
"es-object-atoms": "^1.0.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/gopd": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
"integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/has-symbols": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz",
"integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/hasown": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.3.tgz",
"integrity": "sha512-ej4AhfhfL2Q2zpMmLo7U1Uv9+PyhIZpgQLGT1F9miIGmiCJIoCgSmczFdrc97mWT4kVY72KA+WnnhJ5pghSvSg==",
"license": "MIT",
"dependencies": {
"function-bind": "^1.1.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/http-errors": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz",
"integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==",
"license": "MIT",
"dependencies": {
"depd": "~2.0.0",
"inherits": "~2.0.4",
"setprototypeof": "~1.2.0",
"statuses": "~2.0.2",
"toidentifier": "~1.0.1"
},
"engines": {
"node": ">= 0.8"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/express"
}
},
"node_modules/iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"license": "MIT",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"license": "ISC"
},
"node_modules/ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==",
"license": "MIT",
"engines": {
"node": ">= 0.10"
}
},
"node_modules/math-intrinsics": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
"integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/merge-descriptors": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.3.tgz",
"integrity": "sha512-gaNvAS7TZ897/rVaZ0nMtAyxNyi/pdbjbAwUpFQpN70GqnVfOiXpeUUMKRBmzXaSQ8DdTX4/0ms62r2K+hE6mQ==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/methods": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
"integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
"license": "MIT",
"bin": {
"mime": "cli.js"
},
"engines": {
"node": ">=4"
}
},
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"license": "MIT",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
"license": "MIT"
},
"node_modules/negotiator": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
"integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/object-inspect": {
"version": "1.13.4",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
"integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/on-finished": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz",
"integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==",
"license": "MIT",
"dependencies": {
"ee-first": "1.1.1"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
"integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/path-to-regexp": {
"version": "0.1.13",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.13.tgz",
"integrity": "sha512-A/AGNMFN3c8bOlvV9RreMdrv7jsmF9XIfDeCd87+I8RNg6s78BhJxMu69NEMHBSJFxKidViTEdruRwEk/WIKqA==",
"license": "MIT"
},
"node_modules/proxy-addr": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
"integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==",
"license": "MIT",
"dependencies": {
"forwarded": "0.2.0",
"ipaddr.js": "1.9.1"
},
"engines": {
"node": ">= 0.10"
}
},
"node_modules/qs": {
"version": "6.15.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.15.2.tgz",
"integrity": "sha512-Rzq0KEyX/w/tEybncDgdkZrJgVUsUMk3xjh3t5bv3S1HTAtg+uOYt72+ZfwiQwKdysThkTBdL/rTi6HDmX9Ddw==",
"license": "BSD-3-Clause",
"dependencies": {
"side-channel": "^1.1.0"
},
"engines": {
"node": ">=0.6"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
"integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/raw-body": {
"version": "2.5.3",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.3.tgz",
"integrity": "sha512-s4VSOf6yN0rvbRZGxs8Om5CWj6seneMwK3oDb4lWDH0UPhWcxwOWw5+qk24bxq87szX1ydrwylIOp2uG1ojUpA==",
"license": "MIT",
"dependencies": {
"bytes": "~3.1.2",
"http-errors": "~2.0.1",
"iconv-lite": "~0.4.24",
"unpipe": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
"license": "MIT"
},
"node_modules/send": {
"version": "0.19.2",
"resolved": "https://registry.npmjs.org/send/-/send-0.19.2.tgz",
"integrity": "sha512-VMbMxbDeehAxpOtWJXlcUS5E8iXh6QmN+BkRX1GARS3wRaXEEgzCcB10gTQazO42tpNIya8xIyNx8fll1OFPrg==",
"license": "MIT",
"dependencies": {
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "1.2.0",
"encodeurl": "~2.0.0",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"fresh": "~0.5.2",
"http-errors": "~2.0.1",
"mime": "1.6.0",
"ms": "2.1.3",
"on-finished": "~2.4.1",
"range-parser": "~1.2.1",
"statuses": "~2.0.2"
},
"engines": {
"node": ">= 0.8.0"
}
},
"node_modules/send/node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"license": "MIT"
},
"node_modules/serve-static": {
"version": "1.16.3",
"resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.16.3.tgz",
"integrity": "sha512-x0RTqQel6g5SY7Lg6ZreMmsOzncHFU7nhnRWkKgWuMTu5NN0DR5oruckMqRvacAN9d5w6ARnRBXl9xhDCgfMeA==",
"license": "MIT",
"dependencies": {
"encodeurl": "~2.0.0",
"escape-html": "~1.0.3",
"parseurl": "~1.3.3",
"send": "~0.19.1"
},
"engines": {
"node": ">= 0.8.0"
}
},
"node_modules/setprototypeof": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==",
"license": "ISC"
},
"node_modules/side-channel": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz",
"integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0",
"object-inspect": "^1.13.3",
"side-channel-list": "^1.0.0",
"side-channel-map": "^1.0.1",
"side-channel-weakmap": "^1.0.2"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/side-channel-list": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz",
"integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0",
"object-inspect": "^1.13.4"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/side-channel-map": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz",
"integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==",
"license": "MIT",
"dependencies": {
"call-bound": "^1.0.2",
"es-errors": "^1.3.0",
"get-intrinsic": "^1.2.5",
"object-inspect": "^1.13.3"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/side-channel-weakmap": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz",
"integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==",
"license": "MIT",
"dependencies": {
"call-bound": "^1.0.2",
"es-errors": "^1.3.0",
"get-intrinsic": "^1.2.5",
"object-inspect": "^1.13.3",
"side-channel-map": "^1.0.1"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/statuses": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
"integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/toidentifier": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz",
"integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==",
"license": "MIT",
"engines": {
"node": ">=0.6"
}
},
"node_modules/type-is": {
"version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
"integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==",
"license": "MIT",
"dependencies": {
"media-typer": "0.3.0",
"mime-types": "~2.1.24"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/utils-merge": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==",
"license": "MIT",
"engines": {
"node": ">= 0.4.0"
}
},
"node_modules/vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
}
}
}
+15
View File
@@ -0,0 +1,15 @@
{
"name": "incus-backup-ui-agent",
"version": "0.1.0",
"type": "module",
"private": true,
"scripts": {
"dev": "node --watch src/index.js",
"start": "node src/index.js"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.21.2"
}
}
+158
View File
@@ -0,0 +1,158 @@
import dotenv from 'dotenv';
import { readFile, rename, writeFile } from 'node:fs/promises';
import path from 'node:path';
dotenv.config();
const envPath = path.resolve(process.cwd(), '.env');
const minApiTokenLength = 32;
export const requiredEnv = [
'AWS_ACCESS_KEY_ID',
'AWS_SECRET_ACCESS_KEY',
'RESTIC_REPOSITORY',
'RESTIC_PASSWORD',
'ZFS_POOL_NAME',
];
export const config = {
port: Number(process.env.PORT || 3000),
host: process.env.HOST || '0.0.0.0',
apiToken: process.env.API_TOKEN || '',
httpsEnabled: process.env.HTTPS_ENABLED === 'true',
tlsCertFile: process.env.TLS_CERT_FILE || '',
tlsKeyFile: process.env.TLS_KEY_FILE || '',
agentDatabasePath: path.resolve(process.cwd(), process.env.AGENT_DATABASE_PATH || './agent.sqlite'),
allowedManagementIps: (process.env.ALLOWED_MANAGEMENT_IPS || '')
.split(',')
.map((value) => value.trim())
.filter(Boolean),
zfsPoolName: process.env.ZFS_POOL_NAME || '',
resticEnv: {
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID || '',
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY || '',
RESTIC_REPOSITORY: process.env.RESTIC_REPOSITORY || '',
RESTIC_PASSWORD: process.env.RESTIC_PASSWORD || '',
},
retention: {
keepHourly: Number(process.env.RESTIC_KEEP_HOURLY || 0),
keepDaily: Number(process.env.RESTIC_KEEP_DAILY || 7),
keepWeekly: Number(process.env.RESTIC_KEEP_WEEKLY || 0),
keepMonthly: Number(process.env.RESTIC_KEEP_MONTHLY || 0),
},
};
if (!config.apiToken || config.apiToken.length < minApiTokenLength) {
throw new Error(`API_TOKEN is required and must be at least ${minApiTokenLength} characters long.`);
}
if (config.httpsEnabled && (!config.tlsCertFile || !config.tlsKeyFile)) {
throw new Error('TLS_CERT_FILE and TLS_KEY_FILE are required when HTTPS_ENABLED=true.');
}
export const editableEnv = [
{ key: 'AWS_ACCESS_KEY_ID', label: 'AWS access key ID', required: true, secret: true },
{ key: 'AWS_SECRET_ACCESS_KEY', label: 'AWS secret access key', required: true, secret: true },
{ key: 'RESTIC_REPOSITORY', label: 'Restic repository', required: true, secret: false },
{ key: 'RESTIC_PASSWORD', label: 'Restic password', required: true, secret: true },
{ key: 'ZFS_POOL_NAME', label: 'ZFS pool name', required: true, secret: false },
{ key: 'RESTIC_KEEP_HOURLY', label: 'Keep hourly snapshots', required: false, secret: false },
{ key: 'RESTIC_KEEP_DAILY', label: 'Keep daily snapshots', required: false, secret: false },
{ key: 'RESTIC_KEEP_WEEKLY', label: 'Keep weekly snapshots', required: false, secret: false },
{ key: 'RESTIC_KEEP_MONTHLY', label: 'Keep monthly snapshots', required: false, secret: false },
{ key: 'PORT', label: 'API port', required: false, secret: false },
{ key: 'HOST', label: 'API bind host', required: false, secret: false },
{ key: 'API_TOKEN', label: 'API token', required: true, secret: true },
{ key: 'HTTPS_ENABLED', label: 'Enable HTTPS', required: false, secret: false },
{ key: 'TLS_CERT_FILE', label: 'TLS certificate file', required: false, secret: false },
{ key: 'TLS_KEY_FILE', label: 'TLS private key file', required: false, secret: false },
{ key: 'AGENT_DATABASE_PATH', label: 'Agent database path', required: false, secret: false },
{ key: 'ALLOWED_MANAGEMENT_IPS', label: 'Allowed management IPs', required: false, secret: false },
];
export function missingEnvVars() {
return requiredEnv.filter((key) => !process.env[key]);
}
export function resticProcessEnv() {
return {
...process.env,
...config.resticEnv,
};
}
export async function readEnvSettings() {
const fileValues = await readEnvFile();
return editableEnv.map((field) => ({
...field,
value: fileValues[field.key] ?? process.env[field.key] ?? '',
}));
}
export async function writeEnvSettings(values) {
const allowedKeys = new Set(editableEnv.map((field) => field.key));
const currentValues = await readEnvFile();
const nextValues = { ...currentValues };
for (const [key, value] of Object.entries(values || {})) {
if (!allowedKeys.has(key)) continue;
if (key === 'API_TOKEN' && String(value || '').length < minApiTokenLength) {
const error = new Error(`API_TOKEN must be at least ${minApiTokenLength} characters long.`);
error.status = 400;
throw error;
}
nextValues[key] = String(value ?? '');
}
const body = editableEnv
.map(({ key }) => `${key}=${quoteEnvValue(nextValues[key] || '')}`)
.join('\n');
const tempPath = `${envPath}.tmp`;
await writeFile(tempPath, `${body}\n`, { mode: 0o600 });
await rename(tempPath, envPath);
applyRuntimeEnv(nextValues);
return readEnvSettings();
}
async function readEnvFile() {
try {
const content = await readFile(envPath, 'utf8');
return dotenv.parse(content);
} catch (error) {
if (error.code === 'ENOENT') return {};
throw error;
}
}
function quoteEnvValue(value) {
const escaped = String(value).replace(/\\/g, '\\\\').replace(/"/g, '\\"');
return `"${escaped}"`;
}
function applyRuntimeEnv(values) {
for (const [key, value] of Object.entries(values)) {
process.env[key] = value;
}
config.port = Number(process.env.PORT || 3000);
config.host = process.env.HOST || '0.0.0.0';
config.apiToken = process.env.API_TOKEN || '';
config.httpsEnabled = process.env.HTTPS_ENABLED === 'true';
config.tlsCertFile = process.env.TLS_CERT_FILE || '';
config.tlsKeyFile = process.env.TLS_KEY_FILE || '';
config.agentDatabasePath = path.resolve(process.cwd(), process.env.AGENT_DATABASE_PATH || './agent.sqlite');
config.allowedManagementIps = (process.env.ALLOWED_MANAGEMENT_IPS || '')
.split(',')
.map((value) => value.trim())
.filter(Boolean);
config.zfsPoolName = process.env.ZFS_POOL_NAME || '';
config.resticEnv.AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || '';
config.resticEnv.AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || '';
config.resticEnv.RESTIC_REPOSITORY = process.env.RESTIC_REPOSITORY || '';
config.resticEnv.RESTIC_PASSWORD = process.env.RESTIC_PASSWORD || '';
config.retention.keepHourly = Number(process.env.RESTIC_KEEP_HOURLY || 0);
config.retention.keepDaily = Number(process.env.RESTIC_KEEP_DAILY || 7);
config.retention.keepWeekly = Number(process.env.RESTIC_KEEP_WEEKLY || 0);
config.retention.keepMonthly = Number(process.env.RESTIC_KEEP_MONTHLY || 0);
}
+161
View File
@@ -0,0 +1,161 @@
import { spawn } from 'node:child_process';
import { PassThrough, Transform } from 'node:stream';
import { resticProcessEnv } from './config.js';
export class CommandError extends Error {
constructor(message, result) {
super(message);
this.name = 'CommandError';
this.result = result;
}
}
export function spawnCommand(command, args = [], options = {}) {
const {
env = process.env,
input = null,
log = null,
ignoreExitCode = false,
cwd = process.cwd(),
} = options;
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd,
env,
shell: false,
stdio: ['pipe', 'pipe', 'pipe'],
});
let stdout = '';
let stderr = '';
child.stdout.on('data', (chunk) => {
const text = chunk.toString();
stdout += text;
log?.(text.trimEnd());
});
child.stderr.on('data', (chunk) => {
const text = chunk.toString();
stderr += text;
log?.(text.trimEnd());
});
child.on('error', (error) => {
reject(error);
});
child.on('close', (exitCode) => {
const result = { stdout, stderr, exitCode };
if (exitCode !== 0 && !ignoreExitCode) {
reject(new CommandError(stderr.trim() || `${command} exited with ${exitCode}`, result));
return;
}
resolve(result);
});
if (input) {
input.on('error', (error) => {
child.kill('SIGTERM');
reject(error);
});
input.pipe(child.stdin);
} else {
child.stdin.end();
}
});
}
export function runRestic(args, options = {}) {
return spawnCommand('restic', args, {
...options,
env: resticProcessEnv(),
});
}
export async function resticSnapshotFileSize(snapshotId, filename) {
const result = await runRestic(['ls', '--json', snapshotId]);
const wanted = `/${filename}`;
for (const line of result.stdout.split('\n')) {
if (!line.trim()) continue;
const entry = JSON.parse(line);
if (entry.type === 'file' && (entry.path === filename || entry.path === wanted || entry.path?.endsWith(wanted))) {
return Number(entry.size);
}
}
const error = new Error(`Could not verify Restic file size for ${filename} in snapshot ${snapshotId}.`);
error.status = 500;
throw error;
}
export function createProgressStream(totalBytes, onProgress) {
let currentBytes = 0;
return new Transform({
transform(chunk, _encoding, callback) {
currentBytes += chunk.length;
onProgress?.({
currentBytes,
totalBytes,
percent: totalBytes ? (currentBytes / totalBytes) * 100 : 0,
});
callback(null, chunk);
},
});
}
export function streamResticDumpToDd(snapshotId, filename, outputPath, log, progress = null) {
return new Promise((resolve, reject) => {
const restic = spawn('restic', ['dump', snapshotId, filename], {
env: resticProcessEnv(),
shell: false,
stdio: ['ignore', 'pipe', 'pipe'],
});
const dd = spawn('dd', [`of=${outputPath}`, 'bs=4M', 'conv=sparse', 'status=none'], {
env: process.env,
shell: false,
stdio: ['pipe', 'pipe', 'pipe'],
});
const errors = [];
let resticClosed = false;
let ddClosed = false;
const pipe = new PassThrough();
let restoreStream = restic.stdout.pipe(pipe);
if (progress) {
restoreStream = restoreStream.pipe(createProgressStream(progress.totalBytes, progress.onProgress));
}
restoreStream.pipe(dd.stdin);
restic.stderr.on('data', (chunk) => log?.(chunk.toString().trimEnd()));
dd.stderr.on('data', (chunk) => log?.(chunk.toString().trimEnd()));
restic.on('error', reject);
dd.on('error', reject);
restic.on('close', (code) => {
resticClosed = true;
if (code !== 0) {
errors.push(`restic dump exited with ${code}`);
dd.stdin.destroy();
}
maybeFinish();
});
dd.on('close', (code) => {
ddClosed = true;
if (code !== 0) {
errors.push(`dd exited with ${code}`);
}
maybeFinish();
});
function maybeFinish() {
if (!resticClosed || !ddClosed) return;
if (errors.length) {
reject(new Error(errors.join('; ')));
return;
}
resolve();
}
});
}
+74
View File
@@ -0,0 +1,74 @@
import cors from 'cors';
import express from 'express';
import http from 'node:http';
import https from 'node:https';
import { readFile } from 'node:fs/promises';
import { config } from './config.js';
import { backupRouter } from './routes/backup.js';
import { healthRouter } from './routes/health.js';
import { jobsRouter } from './routes/jobs.js';
import { restoreRouter } from './routes/restore.js';
import { schedulesRouter } from './routes/schedules.js';
import { settingsRouter } from './routes/settings.js';
import { snapshotsRouter } from './routes/snapshots.js';
import { vmsRouter } from './routes/vms.js';
import { startScheduler } from './scheduler.js';
const app = express();
app.use(cors());
app.use(express.json());
app.use((req, res, next) => {
if (config.allowedManagementIps.length && !config.allowedManagementIps.includes(normalizeIp(req.ip))) {
res.status(403).json({ error: 'Forbidden management source.' });
return;
}
const header = req.get('authorization') || '';
if (header === `Bearer ${config.apiToken}`) {
next();
return;
}
res.status(401).json({ error: 'Unauthorized.' });
});
function normalizeIp(value) {
return String(value || '').replace(/^::ffff:/, '');
}
app.use('/api/health', healthRouter);
app.use('/api/vms', vmsRouter);
app.use('/api/snapshots', snapshotsRouter);
app.use('/api/jobs', jobsRouter);
app.use('/api/backup', backupRouter);
app.use('/api/restore', restoreRouter);
app.use('/api/schedules', schedulesRouter);
app.use('/api/settings', settingsRouter);
app.use((error, _req, res, _next) => {
const status = error.status || 500;
res.status(status).json({ error: error.message || 'Internal server error.' });
});
startServer().catch((error) => {
console.error(`Failed to start Incus backup API: ${error.message}`);
process.exit(1);
});
startScheduler().catch((error) => {
console.error(`Failed to start scheduler: ${error.message}`);
});
async function startServer() {
const protocol = config.httpsEnabled ? 'https' : 'http';
const server = config.httpsEnabled
? https.createServer({
cert: await readFile(config.tlsCertFile),
key: await readFile(config.tlsKeyFile),
}, app)
: http.createServer(app);
server.listen(config.port, config.host, () => {
console.log(`Incus backup API listening on ${protocol}://${config.host}:${config.port}`);
});
}
+187
View File
@@ -0,0 +1,187 @@
import crypto from 'node:crypto';
import { mkdirSync } from 'node:fs';
import { DatabaseSync } from 'node:sqlite';
import path from 'node:path';
import { config } from './config.js';
const jobs = new Map();
const locks = new Map();
const maxJobs = 100;
mkdirSync(path.dirname(config.agentDatabasePath), { recursive: true, mode: 0o700 });
const db = new DatabaseSync(config.agentDatabasePath);
migrate();
loadPersistedJobs();
export function createJob(type, vmName) {
if (locks.has(vmName)) {
const activeJob = jobs.get(locks.get(vmName));
const error = new Error(`A ${activeJob?.type || 'job'} job is already active for ${vmName}.`);
error.status = 409;
throw error;
}
const id = `job_${crypto.randomBytes(8).toString('hex')}`;
const now = new Date().toISOString();
const job = {
id,
type,
vmName,
status: 'queued',
startedAt: now,
finishedAt: null,
currentStep: 'Queued',
progress: {
percent: 0,
detail: 'Queued',
currentBytes: 0,
totalBytes: null,
},
logs: [],
error: null,
};
jobs.set(id, job);
locks.set(vmName, id);
persistJob(job);
trimJobs();
return job;
}
export function getJob(id) {
return jobs.get(id) || null;
}
export function listJobs() {
return [...jobs.values()].sort((a, b) => b.startedAt.localeCompare(a.startedAt));
}
export function activeJobForVm(vmName) {
const id = locks.get(vmName);
return id ? getJob(id) : null;
}
export function latestJobForVm(vmName) {
return listJobs().find((job) => job.vmName === vmName) || null;
}
export function setJobRunning(job, step) {
job.status = 'running';
setJobStep(job, step);
}
export function setJobStep(job, step) {
job.currentStep = step;
job.progress = {
...job.progress,
detail: step,
};
appendJobLog(job, `==> ${step}`);
persistJob(job);
}
export function setJobProgress(job, progress = {}) {
const nextPercent = progress.percent ?? job.progress?.percent ?? 0;
job.progress = {
...job.progress,
...progress,
percent: Math.max(0, Math.min(100, Number(nextPercent) || 0)),
};
persistJob(job);
}
export function appendJobLog(job, line) {
if (!line) return;
job.logs.push(...String(line).split('\n').filter(Boolean));
if (job.logs.length > 1000) {
job.logs = job.logs.slice(-1000);
}
persistJob(job);
}
export function finishJob(job, status, error = null) {
job.status = status;
job.finishedAt = new Date().toISOString();
job.currentStep = status === 'success' ? 'Completed' : 'Failed';
job.progress = {
...job.progress,
percent: status === 'success' ? 100 : job.progress?.percent || 0,
detail: status === 'success' ? 'Completed' : 'Failed',
};
job.error = error ? String(error.message || error) : null;
locks.delete(job.vmName);
persistJob(job);
}
function trimJobs() {
const allJobs = listJobs();
for (const job of allJobs.slice(maxJobs)) {
if (job.status !== 'running' && job.status !== 'queued') {
jobs.delete(job.id);
db.prepare('DELETE FROM jobs WHERE id = ?').run(job.id);
}
}
}
function migrate() {
db.exec(`
PRAGMA journal_mode = WAL;
CREATE TABLE IF NOT EXISTS jobs (
id TEXT PRIMARY KEY,
vm_name TEXT NOT NULL,
type TEXT NOT NULL,
status TEXT NOT NULL,
started_at TEXT NOT NULL,
finished_at TEXT,
updated_at TEXT NOT NULL,
payload TEXT NOT NULL
);
`);
}
function loadPersistedJobs() {
const now = new Date().toISOString();
const rows = db.prepare('SELECT payload FROM jobs ORDER BY started_at DESC LIMIT ?').all(maxJobs);
for (const row of rows.reverse()) {
const job = JSON.parse(row.payload);
if (['queued', 'running'].includes(job.status)) {
job.status = 'failed';
job.finishedAt = now;
job.currentStep = 'Failed';
job.progress = {
...job.progress,
detail: 'Failed',
};
job.error = 'Agent restarted while this job was active.';
job.logs = [...(job.logs || []), 'Agent restarted while this job was active; job marked failed.'].slice(-1000);
}
jobs.set(job.id, job);
persistJob(job);
}
trimJobs();
}
function persistJob(job) {
db.prepare(`
INSERT INTO jobs (id, vm_name, type, status, started_at, finished_at, updated_at, payload)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
vm_name = excluded.vm_name,
type = excluded.type,
status = excluded.status,
started_at = excluded.started_at,
finished_at = excluded.finished_at,
updated_at = excluded.updated_at,
payload = excluded.payload
`).run(
job.id,
job.vmName,
job.type,
job.status,
job.startedAt,
job.finishedAt,
new Date().toISOString(),
JSON.stringify(job),
);
}
+358
View File
@@ -0,0 +1,358 @@
import { Router } from 'express';
import { spawn } from 'node:child_process';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { finished } from 'node:stream/promises';
import { setTimeout as delay } from 'node:timers/promises';
import { config } from '../config.js';
import { createProgressStream, resticSnapshotFileSize, spawnCommand, runRestic } from '../executor.js';
import { appendJobLog, createJob, finishJob, setJobProgress, setJobRunning, setJobStep } from '../jobs.js';
import { validateVmExists } from '../validators.js';
export const backupRouter = Router();
backupRouter.post('/:vmName', async (req, res, next) => {
try {
const vmName = req.params.vmName;
const job = await startBackupForVm(vmName);
res.status(202).json({ jobId: job.id, message: 'Backup job started.' });
} catch (error) {
next(error);
}
});
export async function startBackupForVm(vmName) {
const instance = await validateVmExists(vmName);
const job = createJob('backup', vmName);
job.instanceType = instance.type;
runBackupJob(job, instance).catch(() => {});
return job;
}
export async function runBackupJob(job, instance = null) {
const currentInstance = instance || await validateVmExists(job.vmName);
if (currentInstance.type === 'container') {
await runContainerBackupJob(job);
return;
}
const timestamp = new Date().toISOString().replace(/[-:.TZ]/g, '').slice(0, 14);
const snapshotName = `s3-backup-${timestamp}`;
const zvol = `${config.zfsPoolName}/virtual-machines/${job.vmName}.block`;
const snapshotDevice = `/dev/zvol/${zvol}@snapshot-${snapshotName}`;
try {
const totalBytes = await zfsVolumeSize(zvol);
setJobRunning(job, 'Creating Incus snapshot');
setJobProgress(job, { percent: 2 });
await spawnCommand('incus', ['snapshot', 'create', job.vmName, snapshotName], { log: (line) => appendJobLog(job, line) });
setJobStep(job, 'Making ZFS snapshot device visible');
setJobProgress(job, { percent: 5 });
await spawnCommand('zfs', ['set', 'snapdev=visible', zvol], { log: (line) => appendJobLog(job, line) });
setJobStep(job, 'Waiting for snapshot device');
setJobProgress(job, { percent: 8 });
await new Promise((resolve) => setTimeout(resolve, 2000));
setJobStep(job, 'Streaming block device to Restic');
const snapshotStream = await deviceReadStream(snapshotDevice);
let streamedBytes = 0;
const progressStream = createProgressStream(totalBytes, ({ currentBytes, totalBytes: bytesTotal, percent }) => {
streamedBytes = currentBytes;
setJobProgress(job, {
currentBytes,
totalBytes: bytesTotal,
percent: 10 + percent * 0.78,
detail: `Streaming ${formatBytes(currentBytes)} of ${formatBytes(bytesTotal)}`,
});
});
try {
const result = await spawnCommand('restic', ['backup', '--stdin', '--stdin-filename', `${job.vmName}.raw`, '--tag', job.vmName, '--tag', 'data', '--tag', 'virtual-machine'], {
env: { ...process.env, ...config.resticEnv },
input: snapshotStream.pipe(progressStream),
log: (line) => appendJobLog(job, line),
});
const snapshotId = parseResticSnapshotId(result);
await verifyResticFileSize(job, snapshotId, `${job.vmName}.raw`, streamedBytes || totalBytes);
} finally {
if (!snapshotStream.destroyed) {
snapshotStream.destroy();
}
await finished(snapshotStream, { cleanup: true }).catch(() => {});
}
setJobStep(job, 'Hiding ZFS snapshot device');
setJobProgress(job, { percent: 90 });
await spawnCommand('zfs', ['set', 'snapdev=hidden', zvol], { log: (line) => appendJobLog(job, line) });
await settleUdev(job);
setJobStep(job, 'Deleting temporary Incus snapshot');
setJobProgress(job, { percent: 93 });
await deleteIncusSnapshotWithRetry(job, snapshotName);
setJobStep(job, 'Backing up Incus metadata');
setJobProgress(job, { percent: 95 });
await backupInstanceMetadata(job, 'virtual-machine');
setJobStep(job, 'Applying Restic retention policy');
setJobProgress(job, { percent: 97 });
await runRestic(retentionArgs(job.vmName), {
log: (line) => appendJobLog(job, line),
});
finishJob(job, 'success');
} catch (error) {
appendJobLog(job, error.message);
await cleanupBackup(job, zvol, snapshotName);
finishJob(job, 'failed', error);
}
}
async function runContainerBackupJob(job) {
const timestamp = new Date().toISOString().replace(/[-:.TZ]/g, '').slice(0, 14);
const snapshotName = `s3-backup-${timestamp}`;
const dataset = `${config.zfsPoolName}/containers/${job.vmName}`;
const snapshot = `${dataset}@snapshot-${snapshotName}`;
try {
const totalBytes = await zfsDatasetUsed(dataset);
setJobRunning(job, 'Creating Incus snapshot');
setJobProgress(job, { percent: 2 });
await spawnCommand('incus', ['snapshot', 'create', job.vmName, snapshotName], { log: (line) => appendJobLog(job, line) });
setJobStep(job, 'Streaming ZFS snapshot to Restic');
const zfs = spawn('zfs', ['send', snapshot], { stdio: ['ignore', 'pipe', 'pipe'] });
const zfsClosed = waitForProcess(zfs, 'zfs send');
zfs.stderr.on('data', (chunk) => appendJobLog(job, chunk.toString().trimEnd()));
let streamedBytes = 0;
const progressStream = createProgressStream(totalBytes, ({ currentBytes, totalBytes: bytesTotal, percent }) => {
streamedBytes = currentBytes;
setJobProgress(job, {
currentBytes,
totalBytes: bytesTotal,
percent: 10 + percent * 0.78,
detail: `Streaming ${formatBytes(currentBytes)} of ${formatBytes(bytesTotal)}`,
});
});
let resticOk = false;
try {
const result = await spawnCommand('restic', ['backup', '--stdin', '--stdin-filename', `${job.vmName}.zfs`, '--tag', job.vmName, '--tag', 'data', '--tag', 'container'], {
env: { ...process.env, ...config.resticEnv },
input: zfs.stdout.pipe(progressStream),
log: (line) => appendJobLog(job, line),
});
const snapshotId = parseResticSnapshotId(result);
await verifyResticFileSize(job, snapshotId, `${job.vmName}.zfs`, streamedBytes || totalBytes);
resticOk = true;
} finally {
if (!resticOk && !zfs.killed) zfs.kill('SIGTERM');
}
await zfsClosed;
setJobStep(job, 'Deleting temporary Incus snapshot');
setJobProgress(job, { percent: 93 });
await deleteIncusSnapshotWithRetry(job, snapshotName);
setJobStep(job, 'Backing up Incus metadata');
setJobProgress(job, { percent: 95 });
await backupInstanceMetadata(job, 'container');
setJobStep(job, 'Applying Restic retention policy');
setJobProgress(job, { percent: 97 });
await runRestic(retentionArgs(job.vmName), {
log: (line) => appendJobLog(job, line),
});
finishJob(job, 'success');
} catch (error) {
appendJobLog(job, error.message);
await deleteIncusSnapshotWithRetry(job, snapshotName, { ignoreExitCode: true });
finishJob(job, 'failed', error);
}
}
async function deviceReadStream(path) {
const { createReadStream } = await import('node:fs');
return createReadStream(path);
}
async function zfsVolumeSize(zvol) {
const result = await spawnCommand('zfs', ['get', '-Hp', '-o', 'value', 'volsize', zvol]);
const size = Number(result.stdout.trim());
return Number.isFinite(size) && size > 0 ? size : null;
}
async function zfsDatasetUsed(dataset) {
const result = await spawnCommand('zfs', ['get', '-Hp', '-o', 'value', 'used', dataset]);
const size = Number(result.stdout.trim());
return Number.isFinite(size) && size > 0 ? size : null;
}
async function backupInstanceMetadata(job, instanceType) {
const tempDir = await mkdtemp(path.join(tmpdir(), `incus-backup-${job.vmName}-`));
try {
const metadataDir = path.join(tempDir, job.vmName);
await writeMetadataFile(metadataDir, 'README.txt', metadataReadme(job.vmName, instanceType));
await writeCommandOutput(metadataDir, 'config.yaml', ['config', 'show', job.vmName]);
await writeCommandOutput(metadataDir, 'config-expanded.yaml', ['config', 'show', job.vmName, '--expanded']);
await writeCommandOutput(metadataDir, 'info.txt', ['info', job.vmName]);
await writeCommandOutput(metadataDir, 'snapshots.json', ['snapshot', 'list', job.vmName, '--format', 'json']);
await runRestic(['backup', metadataDir, '--tag', job.vmName, '--tag', 'metadata', '--tag', instanceType], {
log: (line) => appendJobLog(job, line),
});
} finally {
await rm(tempDir, { recursive: true, force: true });
}
}
async function writeCommandOutput(directory, filename, args) {
const result = await spawnCommand('incus', args);
await writeMetadataFile(directory, filename, result.stdout);
}
async function writeMetadataFile(directory, filename, content) {
const { mkdir } = await import('node:fs/promises');
await mkdir(directory, { recursive: true });
await writeFile(path.join(directory, filename), content || '', { mode: 0o600 });
}
function metadataReadme(instanceName, instanceType) {
return [
`Instance: ${instanceName}`,
`Type: ${instanceType}`,
`Created: ${new Date().toISOString()}`,
'',
'This directory contains Incus metadata captured alongside the disk/dataset backup.',
'It is intended for disaster-recovery reconstruction and restore preflight workflows.',
'',
].join('\n');
}
function waitForProcess(child, label) {
return new Promise((resolve, reject) => {
child.on('error', reject);
child.on('close', (exitCode) => {
if (exitCode !== 0 && exitCode !== null) {
reject(new Error(`${label} exited with ${exitCode}`));
return;
}
resolve();
});
});
}
function formatBytes(value) {
if (!value) return '-';
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
let size = Number(value);
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex += 1;
}
return `${size.toFixed(unitIndex === 0 ? 0 : 1)} ${units[unitIndex]}`;
}
function retentionArgs(vmName) {
const args = ['forget', '--tag', vmName, '--prune'];
const keepHourly = positiveInteger(config.retention.keepHourly);
const keepDaily = positiveInteger(config.retention.keepDaily);
const keepWeekly = positiveInteger(config.retention.keepWeekly);
const keepMonthly = positiveInteger(config.retention.keepMonthly);
if (keepHourly) args.push('--keep-hourly', String(keepHourly));
if (keepDaily) args.push('--keep-daily', String(keepDaily));
if (keepWeekly) args.push('--keep-weekly', String(keepWeekly));
if (keepMonthly) args.push('--keep-monthly', String(keepMonthly));
if (!keepHourly && !keepDaily && !keepWeekly && !keepMonthly) args.push('--keep-daily', '7');
return args;
}
function positiveInteger(value) {
const number = Number(value);
return Number.isFinite(number) && number > 0 ? Math.floor(number) : 0;
}
function parseResticSnapshotId(result) {
const output = `${result.stdout || ''}\n${result.stderr || ''}`;
const match = output.match(/snapshot\s+([0-9a-f]{8,64})\s+saved/i);
if (!match) {
throw new Error('Restic backup completed but no snapshot ID could be parsed.');
}
return match[1];
}
async function verifyResticFileSize(job, snapshotId, filename, expectedBytes) {
if (!expectedBytes) {
appendJobLog(job, `Skipping size verification for ${snapshotId}: expected size is unknown.`);
return;
}
try {
const storedBytes = await resticSnapshotFileSize(snapshotId, filename);
if (storedBytes !== expectedBytes) {
throw new Error(`Backup verification failed for ${snapshotId}: stored ${formatBytes(storedBytes)}, expected ${formatBytes(expectedBytes)}.`);
}
appendJobLog(job, `Verified Restic snapshot ${snapshotId}: ${formatBytes(storedBytes)}.`);
} catch (error) {
appendJobLog(job, error.message);
await forgetFailedSnapshot(job, snapshotId);
throw error;
}
}
async function forgetFailedSnapshot(job, snapshotId) {
appendJobLog(job, `Removing failed Restic snapshot ${snapshotId}.`);
await runRestic(['forget', snapshotId, '--prune'], {
log: (line) => appendJobLog(job, line),
}).catch((error) => appendJobLog(job, `Failed to remove Restic snapshot ${snapshotId}: ${error.message}`));
}
async function cleanupBackup(job, zvol, snapshotName) {
setJobStep(job, 'Running cleanup');
await spawnCommand('zfs', ['set', 'snapdev=hidden', zvol], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((error) => appendJobLog(job, error.message));
await settleUdev(job);
await deleteIncusSnapshotWithRetry(job, snapshotName, { ignoreExitCode: true });
}
async function settleUdev(job) {
await spawnCommand('udevadm', ['trigger'], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((error) => appendJobLog(job, error.message));
await spawnCommand('udevadm', ['settle'], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((error) => appendJobLog(job, error.message));
}
async function deleteIncusSnapshotWithRetry(job, snapshotName, options = {}) {
const maxAttempts = 5;
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
try {
return await spawnCommand('incus', ['snapshot', 'delete', job.vmName, snapshotName], {
ignoreExitCode: options.ignoreExitCode,
log: (line) => appendJobLog(job, line),
});
} catch (error) {
const retryable = error.message.includes('dataset is busy');
if (!retryable || attempt === maxAttempts) {
if (options.ignoreExitCode) {
appendJobLog(job, error.message);
return null;
}
throw error;
}
appendJobLog(job, `Snapshot device still busy; retrying delete (${attempt}/${maxAttempts}).`);
await delay(2000);
}
}
return null;
}
+88
View File
@@ -0,0 +1,88 @@
import { Router } from 'express';
import { access } from 'node:fs/promises';
import { constants } from 'node:fs';
import { config, missingEnvVars } from '../config.js';
import { spawnCommand, runRestic } from '../executor.js';
export const healthRouter = Router();
healthRouter.get('/', async (_req, res) => {
const missing = missingEnvVars();
const checks = {
config: checkValue(!missing.length, missing.length ? `missing: ${missing.join(', ')}` : 'ok'),
commands: await checkCommands({
incus: ['version'],
zfs: ['version'],
zpool: ['version'],
restic: ['version'],
udevadm: ['--version'],
dd: ['--version'],
}),
zfsPool: await checkZfsPool(),
zvol: await checkZvolAccess(),
resticRepository: await checkResticRepository(),
};
const ok = Object.values(checks).every((value) => value.ok);
res.status(ok ? 200 : 503).json({ ok, checks });
});
async function checkCommands(commands) {
const results = {};
await Promise.all(Object.entries(commands).map(async ([command, args]) => {
results[command] = await checkCommand(command, args);
}));
const failed = Object.entries(results).filter(([, result]) => !result.ok);
return {
ok: failed.length === 0,
message: failed.length ? `failed: ${failed.map(([command]) => command).join(', ')}` : 'ok',
details: results,
};
}
async function checkCommand(command, args) {
try {
const result = await spawnCommand(command, args, { ignoreExitCode: true });
return checkValue(result.exitCode === 0, result.exitCode === 0 ? 'ok' : result.stderr.trim() || result.stdout.trim() || 'failed');
} catch (error) {
return checkValue(false, error.message);
}
}
async function checkZfsPool() {
if (!config.zfsPoolName) return checkValue(false, 'ZFS_POOL_NAME is not configured');
try {
const result = await spawnCommand('zpool', ['list', '-Hp', '-o', 'name,size,alloc,free,cap', config.zfsPoolName], { ignoreExitCode: true });
if (result.exitCode !== 0) return checkValue(false, result.stderr.trim() || 'zpool list failed');
const [name, size, allocated, free, capacity] = result.stdout.trim().split('\t');
return {
ok: true,
message: 'ok',
details: { name, size: Number(size), allocated: Number(allocated), free: Number(free), capacity },
};
} catch (error) {
return checkValue(false, error.message);
}
}
async function checkZvolAccess() {
try {
await access('/dev/zvol', constants.R_OK);
return checkValue(true, 'ok');
} catch (error) {
return checkValue(false, error.message);
}
}
async function checkResticRepository() {
try {
const result = await runRestic(['--no-lock', 'snapshots', '--json'], { ignoreExitCode: true });
return checkValue(result.exitCode === 0, result.exitCode === 0 ? 'ok' : result.stderr.trim() || 'restic repository check failed');
} catch (error) {
return checkValue(false, error.message);
}
}
function checkValue(ok, message, details = null) {
return { ok, message, ...(details ? { details } : {}) };
}
+17
View File
@@ -0,0 +1,17 @@
import { Router } from 'express';
import { getJob, listJobs } from '../jobs.js';
export const jobsRouter = Router();
jobsRouter.get('/', (_req, res) => {
res.json(listJobs());
});
jobsRouter.get('/:jobId', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) {
res.status(404).json({ error: 'Job not found.' });
return;
}
res.json(job);
});
+201
View File
@@ -0,0 +1,201 @@
import { Router } from 'express';
import { constants } from 'node:fs';
import { access } from 'node:fs/promises';
import { setTimeout as delay } from 'node:timers/promises';
import { config } from '../config.js';
import { resticSnapshotFileSize, spawnCommand, streamResticDumpToDd } from '../executor.js';
import { appendJobLog, createJob, finishJob, setJobProgress, setJobRunning, setJobStep } from '../jobs.js';
import { validateSnapshotForVm, validateVmExists } from '../validators.js';
export const restoreRouter = Router();
restoreRouter.post('/:vmName', async (req, res, next) => {
try {
const vmName = req.params.vmName;
const { snapshotId, confirmVmName } = req.body || {};
if (confirmVmName !== vmName) {
const error = new Error('Restore confirmation does not match VM name.');
error.status = 400;
throw error;
}
const instance = await validateVmExists(vmName);
if (instance.type === 'container') {
const error = new Error('Container restore is not implemented yet. Container backups can be created, but restore needs a safe zfs receive workflow.');
error.status = 501;
throw error;
}
const snapshot = await validateSnapshotForVm(vmName, snapshotId);
const job = createJob('restore', vmName);
runRestoreJob(job, snapshot.id).catch(() => {});
res.status(202).json({ jobId: job.id, message: 'Restore job started.' });
} catch (error) {
next(error);
}
});
async function runRestoreJob(job, snapshotId) {
const zvol = `${config.zfsPoolName}/virtual-machines/${job.vmName}.block`;
const timestamp = new Date().toISOString().replace(/[-:.TZ]/g, '').slice(0, 14);
const tempZvol = `${config.zfsPoolName}/virtual-machines/${job.vmName}.restore-${timestamp}.block`;
const backupZvol = `${zvol}.pre-restore-${timestamp}`;
const failedZvol = `${zvol}.failed-restore-${timestamp}`;
const tempDevice = `/dev/zvol/${tempZvol}`;
let tempCreated = false;
let oldVolumeRenamed = false;
let swapped = false;
try {
setJobRunning(job, 'Checking restore size');
setJobProgress(job, { percent: 3 });
const totalBytes = await zfsVolumeSize(zvol);
const resticBytes = await resticSnapshotFileSize(snapshotId, `${job.vmName}.raw`);
if (totalBytes && resticBytes && totalBytes !== resticBytes) {
throw new Error(`Restore size mismatch: Restic file is ${formatBytes(resticBytes)}, target volume is ${formatBytes(totalBytes)}.`);
}
setJobStep(job, 'Creating staged restore volume');
setJobProgress(job, { percent: 6 });
await createRestoreVolume(job, zvol, tempZvol, totalBytes || resticBytes);
tempCreated = true;
setJobStep(job, 'Waiting for staged restore device');
setJobProgress(job, { percent: 10 });
await settleUdev(job);
await waitForDevice(tempDevice);
setJobStep(job, 'Writing Restic snapshot to staged volume');
await streamResticDumpToDd(snapshotId, `${job.vmName}.raw`, tempDevice, (line) => appendJobLog(job, line), {
totalBytes,
onProgress: ({ currentBytes, totalBytes: bytesTotal, percent }) => {
setJobProgress(job, {
currentBytes,
totalBytes: bytesTotal,
percent: 12 + percent * 0.72,
detail: `Writing ${formatBytes(currentBytes)} of ${formatBytes(bytesTotal)}`,
});
},
});
setJobStep(job, 'Preparing staged volume for swap');
setJobProgress(job, { percent: 86 });
await spawnCommand('zfs', ['set', 'volmode=none', tempZvol], { log: (line) => appendJobLog(job, line) });
await settleUdev(job);
setJobStep(job, 'Stopping VM');
setJobProgress(job, { percent: 89 });
await spawnCommand('incus', ['stop', job.vmName, '--force'], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
});
setJobStep(job, 'Swapping restored volume into place');
setJobProgress(job, { percent: 92 });
await spawnCommand('zfs', ['rename', zvol, backupZvol], { log: (line) => appendJobLog(job, line) });
oldVolumeRenamed = true;
await spawnCommand('zfs', ['rename', tempZvol, zvol], { log: (line) => appendJobLog(job, line) });
tempCreated = false;
swapped = true;
setJobStep(job, 'Starting VM');
setJobProgress(job, { percent: 97 });
await spawnCommand('incus', ['start', job.vmName], { log: (line) => appendJobLog(job, line) });
appendJobLog(job, `Pre-restore ZFS volume kept for rollback: ${backupZvol}`);
finishJob(job, 'success');
} catch (error) {
appendJobLog(job, error.message);
await cleanupRestoreFailure(job, { zvol, tempZvol, backupZvol, failedZvol, tempCreated, oldVolumeRenamed, swapped });
appendJobLog(job, 'VM was not restarted because restore did not complete successfully.');
finishJob(job, 'failed', error);
}
}
async function zfsVolumeSize(zvol) {
const result = await spawnCommand('zfs', ['get', '-Hp', '-o', 'value', 'volsize', zvol]);
const size = Number(result.stdout.trim());
return Number.isFinite(size) && size > 0 ? size : null;
}
async function zfsProperty(zvol, property) {
const result = await spawnCommand('zfs', ['get', '-Hp', '-o', 'value', property, zvol]);
return result.stdout.trim();
}
async function createRestoreVolume(job, sourceZvol, targetZvol, bytes) {
if (!bytes) throw new Error('Could not determine restore volume size.');
const volblocksize = await zfsProperty(sourceZvol, 'volblocksize');
const args = ['create', '-V', `${bytes}B`, '-o', 'volmode=dev'];
if (volblocksize) args.push('-o', `volblocksize=${volblocksize}`);
args.push(targetZvol);
await spawnCommand('zfs', args, { log: (line) => appendJobLog(job, line) });
}
async function settleUdev(job) {
await spawnCommand('udevadm', ['trigger'], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((error) => appendJobLog(job, error.message));
await spawnCommand('udevadm', ['settle'], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((error) => appendJobLog(job, error.message));
}
async function waitForDevice(devicePath, timeoutMs = 30000) {
const started = Date.now();
while (Date.now() - started < timeoutMs) {
try {
await access(devicePath, constants.R_OK | constants.W_OK);
return;
} catch {
await delay(500);
}
}
throw new Error(`Timed out waiting for restore device ${devicePath}.`);
}
async function cleanupRestoreFailure(job, state) {
const { zvol, tempZvol, backupZvol, failedZvol, tempCreated, oldVolumeRenamed, swapped } = state;
if (swapped) {
setJobStep(job, 'Rolling back swapped restore volume');
await spawnCommand('zfs', ['rename', zvol, failedZvol], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((cleanupError) => appendJobLog(job, cleanupError.message));
await spawnCommand('zfs', ['rename', backupZvol, zvol], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((cleanupError) => appendJobLog(job, cleanupError.message));
appendJobLog(job, `Rolled back to pre-restore volume ${backupZvol}. Failed restored volume, if present, is ${failedZvol}.`);
return;
}
if (oldVolumeRenamed) {
setJobStep(job, 'Restoring original volume name after failed swap');
await spawnCommand('zfs', ['rename', backupZvol, zvol], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((cleanupError) => appendJobLog(job, cleanupError.message));
}
if (tempCreated) {
setJobStep(job, 'Removing staged restore volume');
await spawnCommand('zfs', ['destroy', '-r', tempZvol], {
ignoreExitCode: true,
log: (line) => appendJobLog(job, line),
}).catch((cleanupError) => appendJobLog(job, cleanupError.message));
}
}
function formatBytes(value) {
if (!value) return '-';
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
let size = Number(value);
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex += 1;
}
return `${size.toFixed(unitIndex === 0 ? 0 : 1)} ${units[unitIndex]}`;
}
+17
View File
@@ -0,0 +1,17 @@
import { Router } from 'express';
import { listSchedules, replaceSchedules } from '../scheduler.js';
export const schedulesRouter = Router();
schedulesRouter.get('/', (_req, res) => {
res.json(listSchedules());
});
schedulesRouter.put('/', async (req, res, next) => {
try {
const schedules = await replaceSchedules(req.body?.schedules || []);
res.json(schedules);
} catch (error) {
next(error);
}
});
+23
View File
@@ -0,0 +1,23 @@
import { Router } from 'express';
import { readEnvSettings, writeEnvSettings } from '../config.js';
export const settingsRouter = Router();
settingsRouter.get('/', async (_req, res, next) => {
try {
res.json({ fields: await readEnvSettings() });
} catch (error) {
next(error);
}
});
settingsRouter.put('/', async (req, res, next) => {
try {
res.json({
fields: await writeEnvSettings(req.body?.values || {}),
message: 'Settings saved.',
});
} catch (error) {
next(error);
}
});
+37
View File
@@ -0,0 +1,37 @@
import { Router } from 'express';
import { runRestic } from '../executor.js';
import { assertSnapshotIdShape, listSnapshotsForVm, validateSnapshotForVm } from '../validators.js';
export const snapshotsRouter = Router();
snapshotsRouter.get('/:vmName', async (req, res, next) => {
try {
res.json(await listSnapshotsForVm(req.params.vmName));
} catch (error) {
next(error);
}
});
snapshotsRouter.get('/:vmName/:snapshotId/files', async (req, res, next) => {
try {
const snapshot = await validateSnapshotForVm(req.params.vmName, req.params.snapshotId);
assertSnapshotIdShape(snapshot.id);
const result = await runRestic(['--no-lock', 'ls', '--json', snapshot.id]);
const entries = result.stdout
.split('\n')
.filter(Boolean)
.map((line) => JSON.parse(line))
.filter((entry) => entry.struct_type === 'node')
.map((entry) => ({
name: entry.name,
path: entry.path,
type: entry.type,
size: entry.size || 0,
mode: entry.mode || '',
mtime: entry.mtime || null,
}));
res.json(entries);
} catch (error) {
next(error);
}
});
+34
View File
@@ -0,0 +1,34 @@
import { Router } from 'express';
import { activeJobForVm, latestJobForVm } from '../jobs.js';
import { listIncusVms } from '../validators.js';
export const vmsRouter = Router();
vmsRouter.get('/', async (_req, res, next) => {
try {
const vms = await listIncusVms();
res.json(vms.map((vm) => {
const activeJob = activeJobForVm(vm.name);
const latestJob = latestJobForVm(vm.name);
return {
name: vm.name,
type: vm.type,
status: vm.status,
activeJob: activeJob ? summarizeJob(activeJob) : null,
lastJobStatus: latestJob?.status || null,
};
}));
} catch (error) {
next(error);
}
});
function summarizeJob(job) {
return {
id: job.id,
type: job.type,
status: job.status,
currentStep: job.currentStep,
progress: job.progress,
};
}
+129
View File
@@ -0,0 +1,129 @@
import crypto from 'node:crypto';
import { readFile, rename, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { startBackupForVm } from './routes/backup.js';
const schedulesPath = path.resolve(process.cwd(), 'schedules.json');
const checkIntervalMs = 30 * 1000;
let schedules = [];
let timer = null;
export async function startScheduler() {
await loadSchedules();
if (timer) return;
timer = setInterval(runDueSchedules, checkIntervalMs);
timer.unref?.();
runDueSchedules();
}
export function listSchedules() {
return schedules
.map((schedule) => ({ ...schedule }))
.sort((a, b) => a.vmName.localeCompare(b.vmName));
}
export async function replaceSchedules(nextSchedules) {
schedules = normalizeSchedules(nextSchedules);
await saveSchedules();
return listSchedules();
}
async function runDueSchedules() {
const now = new Date();
let changed = false;
for (const schedule of schedules) {
if (!schedule.enabled) continue;
if (!schedule.nextRunAt) {
schedule.nextRunAt = nextRunFrom(now, schedule.intervalHours, schedule.timeOfDay);
changed = true;
continue;
}
if (new Date(schedule.nextRunAt) > now) continue;
try {
await startBackupForVm(schedule.vmName);
schedule.lastRunAt = now.toISOString();
schedule.lastError = '';
schedule.nextRunAt = nextRunFrom(now, schedule.intervalHours, schedule.timeOfDay);
} catch (error) {
schedule.lastError = String(error.message || error);
schedule.nextRunAt = new Date(now.getTime() + 5 * 60 * 1000).toISOString();
}
changed = true;
}
if (changed) {
await saveSchedules();
}
}
async function loadSchedules() {
try {
const content = await readFile(schedulesPath, 'utf8');
schedules = normalizeSchedules(JSON.parse(content));
} catch (error) {
if (error.code === 'ENOENT') {
schedules = [];
return;
}
throw error;
}
}
async function saveSchedules() {
const tempPath = `${schedulesPath}.tmp`;
await writeFile(tempPath, `${JSON.stringify(schedules, null, 2)}\n`, { mode: 0o600 });
await rename(tempPath, schedulesPath);
}
function normalizeSchedules(values) {
const seen = new Set();
return (Array.isArray(values) ? values : [])
.map((value) => {
const vmName = String(value.vmName || '').trim();
const intervalHours = Math.max(1, Number(value.intervalHours) || 24);
if (!vmName || seen.has(vmName)) return null;
seen.add(vmName);
return {
id: value.id || `schedule_${crypto.randomBytes(8).toString('hex')}`,
vmName,
enabled: Boolean(value.enabled),
intervalHours,
timeOfDay: normalizeTimeOfDay(value.timeOfDay),
nextRunAt: value.nextRunAt || nextRunFrom(new Date(), intervalHours, normalizeTimeOfDay(value.timeOfDay)),
lastRunAt: value.lastRunAt || null,
lastError: value.lastError || '',
};
})
.filter(Boolean);
}
function nextRunFrom(date, intervalHours, timeOfDay) {
const intervalMs = intervalHours * 60 * 60 * 1000;
const candidate = withTimeOfDay(date, timeOfDay);
while (candidate <= date) {
candidate.setTime(candidate.getTime() + intervalMs);
}
return candidate.toISOString();
}
function withTimeOfDay(date, timeOfDay) {
const [hours, minutes] = normalizeTimeOfDay(timeOfDay).split(':').map(Number);
const next = new Date(date);
next.setHours(hours, minutes, 0, 0);
return next;
}
function normalizeTimeOfDay(value) {
const text = String(value || '02:00').trim();
if (/^\d{2}:\d{2}$/.test(text)) {
const [hours, minutes] = text.split(':').map(Number);
if (hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59) {
return text;
}
}
return '02:00';
}
+73
View File
@@ -0,0 +1,73 @@
import { spawnCommand, runRestic } from './executor.js';
const vmNamePattern = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}$/;
const snapshotIdPattern = /^[A-Fa-f0-9]{8,64}$/;
export function assertVmNameShape(vmName) {
if (!vmNamePattern.test(vmName || '')) {
const error = new Error('Invalid VM name.');
error.status = 400;
throw error;
}
}
export function assertSnapshotIdShape(snapshotId) {
if (snapshotId === 'latest') return;
if (!snapshotIdPattern.test(snapshotId || '')) {
const error = new Error('Invalid snapshot id.');
error.status = 400;
throw error;
}
}
export async function listIncusInstances() {
const result = await spawnCommand('incus', ['list', '--format', 'json']);
const entries = JSON.parse(result.stdout || '[]');
return entries.filter((entry) => ['virtual-machine', 'container'].includes(entry.type));
}
export async function listIncusVms() {
return listIncusInstances();
}
export async function validateVmExists(instanceName) {
assertVmNameShape(instanceName);
const instances = await listIncusInstances();
const instance = instances.find((entry) => entry.name === instanceName);
if (!instance) {
const error = new Error(`Instance "${instanceName}" was not found.`);
error.status = 404;
throw error;
}
return instance;
}
export async function listSnapshotsForVm(vmName) {
await validateVmExists(vmName);
const result = await runRestic(['--no-lock', 'snapshots', '--json', '--tag', vmName]);
const snapshots = JSON.parse(result.stdout || '[]');
return snapshots
.filter((snapshot) => !(snapshot.tags || []).includes('metadata'))
.sort((a, b) => String(b.time || '').localeCompare(String(a.time || '')));
}
export async function validateSnapshotForVm(vmName, snapshotId) {
assertSnapshotIdShape(snapshotId);
const snapshots = await listSnapshotsForVm(vmName);
if (snapshotId === 'latest') {
if (!snapshots.length) {
const error = new Error(`No snapshots found for ${vmName}.`);
error.status = 404;
throw error;
}
return snapshots[0];
}
const snapshot = snapshots.find((entry) => entry.id?.startsWith(snapshotId));
if (!snapshot) {
const error = new Error(`Snapshot "${snapshotId}" was not found for ${vmName}.`);
error.status = 404;
throw error;
}
return snapshot;
}