Files
cloudron-box/migrations/20260330000000-backupSites-remove-disk-provider.js

47 lines
1.8 KiB
JavaScript
Raw Normal View History

'use strict';
const child_process = require('node:child_process');
function patchMountFile(mountPath, fstype) {
const mountFilename = child_process.execSync(`systemd-escape -p --suffix=mount "${mountPath}"`, { encoding: 'utf8' }).trim();
const mountFile = `/etc/systemd/system/${mountFilename}`;
try {
child_process.execSync(`sed -i 's/^Type=auto$/Type=${fstype}/' "${mountFile}"`);
child_process.execSync('systemctl daemon-reload');
console.log(`Patched ${mountFile}: Type=auto -> Type=${fstype}`);
} catch (e) {
console.log(`Warning: failed to patch ${mountFile}: ${e.message}`);
}
}
exports.up = async function (db) {
const results = await db.runSql('SELECT id, configJson FROM backupSites WHERE provider = ?', ['disk']);
for (const row of results) {
const config = JSON.parse(row.configJson);
let fstype = 'ext4';
try {
const diskPath = config.mountOptions.diskPath;
const output = child_process.execSync(`lsblk --paths --json --list --fs ${diskPath}`, { encoding: 'utf8' });
const info = JSON.parse(output);
if (info.blockdevices[0].fstype === 'xfs') fstype = 'xfs';
} catch (e) {
console.log(`Could not detect filesystem type for backup site ${row.id}, defaulting to ext4: ${e.message}`);
}
config._provider = fstype;
console.log(`Migrating backup site ${row.id} from disk to ${fstype}`);
await db.runSql('UPDATE backupSites SET provider = ?, configJson = ? WHERE id = ?', [fstype, JSON.stringify(config), row.id]);
if (config._managedMountPath) {
patchMountFile(config._managedMountPath, fstype);
}
}
};
exports.down = function (db, callback) {
callback();
};