backuptarget: pseudo target for import and restore

This commit is contained in:
Girish Ramakrishnan
2025-08-01 23:20:51 +02:00
parent 3cabbc1328
commit a01e1bad0f
8 changed files with 90 additions and 77 deletions

View File

@@ -20,7 +20,6 @@ const appstore = require('./appstore.js'),
domains = require('./domains.js'),
eventlog = require('./eventlog.js'),
fs = require('fs'),
hush = require('./hush.js'),
mail = require('./mail.js'),
mailServer = require('./mailserver.js'),
network = require('./network.js'),
@@ -172,8 +171,8 @@ async function activate(username, password, email, displayName, ip, auditSource)
};
}
async function restoreTask(backupConfig, remotePath, ipv4Config, ipv6Config, options, auditSource) {
assert.strictEqual(typeof backupConfig, 'object');
async function restoreTask(backupTarget, remotePath, ipv4Config, ipv6Config, options, auditSource) {
assert.strictEqual(typeof backupTarget, 'object');
assert.strictEqual(typeof remotePath, 'string');
assert.strictEqual(typeof ipv4Config, 'object');
assert.strictEqual(typeof ipv6Config, 'object');
@@ -181,15 +180,17 @@ async function restoreTask(backupConfig, remotePath, ipv4Config, ipv6Config, opt
assert.strictEqual(typeof auditSource, 'object');
try {
setProgress('restore', 'Preparing backup target');
await backupTargets.storageApi(backupTarget).setup(backupTarget.config);
setProgress('restore', 'Downloading box backup');
backupConfig.rootPath = backupTargets.getRootPath(backupConfig, paths.MANAGED_BACKUP_MOUNT_DIR);
await backuptask.restore(backupConfig, remotePath, (progress) => setProgress('restore', progress.message));
await backuptask.restore(backupTarget, remotePath, (progress) => setProgress('restore', progress.message));
setProgress('restore', 'Downloading mail backup');
const mailBackups = await backups.getByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_MAIL, backups.BACKUP_STATE_NORMAL, 1, 1);
if (mailBackups.length === 0) throw new BoxError(BoxError.NOT_FOUND, 'mail backup not found');
const mailRestoreConfig = { backupConfig, remotePath: mailBackups[0].remotePath, backupFormat: mailBackups[0].format };
await backuptask.downloadMail(mailRestoreConfig, (progress) => setProgress('restore', progress.message));
const mailRemotePath = mailBackups[0].remotePath;
await backuptask.downloadMail(backupTarget, mailRemotePath, (progress) => setProgress('restore', progress.message));
await ensureDhparams();
await network.setIPv4Config(ipv4Config);
@@ -204,8 +205,6 @@ async function restoreTask(backupConfig, remotePath, ipv4Config, ipv6Config, opt
}
await dashboard.setupLocation(location.subdomain, location.domain, auditSource);
delete backupConfig.rootPath;
await backupTargets.setConfig(backupConfig);
await eventlog.add(eventlog.ACTION_RESTORE, auditSource, { remotePath });
setImmediate(() => safe(platform.onActivated({ skipDnsSetup: options.skipDnsSetup }), { debug }));
@@ -236,24 +235,19 @@ async function restore(backupConfig, remotePath, version, ipv4Config, ipv6Config
const activated = await users.isActivated();
if (activated) throw new BoxError(BoxError.CONFLICT, 'Already activated. Restore with a fresh Cloudron installation.');
let error = backupTargets.validateFormat(backupConfig.format);
const backupTarget = await backupTargets.createPseudo({
id: `cloudron-restore`,
provider: backupConfig.provider,
config: backupConfig.config,
format: backupConfig.format,
encryptionPassword: backupConfig.encryptionPassword,
encryptedFilenames: backupConfig.encryptedFilenames
});
const error = await network.testIPv4Config(ipv4Config);
if (error) throw error;
if ('password' in backupConfig) {
backupConfig.encryption = hush.generateEncryptionKeysSync(backupConfig.password);
delete backupConfig.password;
} else {
backupConfig.encryption = null;
}
await backupTargets.setupManagedStorage(backupConfig, paths.MANAGED_BACKUP_MOUNT_DIR); // this validates mountOptions
error = await backupTargets.testStorage(Object.assign({ mountPath: paths.MANAGED_BACKUP_MOUNT_DIR }, backupConfig)); // this validates provider and it's api options. requires mountPath
if (error) throw error;
error = await network.testIPv4Config(ipv4Config);
if (error) throw error;
safe(restoreTask(backupConfig, remotePath, ipv4Config, ipv6Config, options, auditSource), { debug }); // now that args are validated run the task in the background
safe(restoreTask(backupTarget, remotePath, ipv4Config, ipv6Config, options, auditSource), { debug }); // now that args are validated run the task in the background
} catch (error) {
debug('restore: error. %o', error);
gStatus.restore.active = false;