2025-07-28 11:01:46 +02:00
|
|
|
|
|
|
|
|
import { fetcher } from '@cloudron/pankow';
|
|
|
|
|
import { API_ORIGIN } from '../constants.js';
|
|
|
|
|
|
|
|
|
|
function create() {
|
|
|
|
|
const accessToken = localStorage.token;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
async list() {
|
|
|
|
|
const page = 1;
|
|
|
|
|
const per_page = 1000;
|
|
|
|
|
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/backup_sites`, { page, per_page, access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
2025-09-12 09:48:37 +02:00
|
|
|
return [null, result.body.backupSites];
|
2025-07-28 11:01:46 +02:00
|
|
|
},
|
2025-09-24 17:22:10 +02:00
|
|
|
async add(name, format, contents, enableForUpdates, provider, config, schedule, retention, limits = null) {
|
|
|
|
|
const data = { name, format, contents, enableForUpdates, provider, config, schedule, retention };
|
2025-07-31 17:25:31 +02:00
|
|
|
|
|
|
|
|
if (limits !== null) data.limits = limits;
|
|
|
|
|
|
2025-07-28 11:01:46 +02:00
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites`, data, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null, result.body.id];
|
|
|
|
|
},
|
|
|
|
|
async get(id) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/backup_sites/${id}`, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
|
|
|
|
|
return [null, result.body];
|
|
|
|
|
},
|
|
|
|
|
async del(id) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.del(`${API_ORIGIN}/api/v1/backup_sites/${id}`, {}, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 204) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
|
|
|
|
async createBackup(id) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/create_backup`, {}, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 202) return [error || result];
|
|
|
|
|
return [null, result.body.taskId];
|
|
|
|
|
},
|
2025-09-24 17:22:10 +02:00
|
|
|
async setEnableForUpdates(id, enable) {
|
2025-07-28 11:01:46 +02:00
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-24 17:22:10 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/enable_for_updates`, { enable }, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
|
|
|
|
async setRetention(id, retention) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/retention`, { retention }, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
2025-08-04 14:40:29 +02:00
|
|
|
async setName(id, name) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/name`, { name }, { access_token: accessToken });
|
2025-08-04 14:40:29 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
2025-07-28 11:01:46 +02:00
|
|
|
async setLimits(id, limits) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/limits`, { limits }, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
|
|
|
|
async setSchedule(id, schedule) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/schedule`, { schedule }, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
2025-08-06 12:52:49 +02:00
|
|
|
async setEncryption(id, encryptionPassword, encryptedFilenames, encryptionPasswordHint = '') {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/encryption`, { encryptionPassword, encryptedFilenames, encryptionPasswordHint }, { access_token: accessToken });
|
2025-08-06 12:52:49 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
2025-07-28 11:01:46 +02:00
|
|
|
async setConfig(id, config) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/config`, { config }, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
2025-09-24 18:11:48 +02:00
|
|
|
async setContents(id, contents) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
|
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/configure/contents`, { contents }, { access_token: accessToken });
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null];
|
|
|
|
|
},
|
2025-07-28 11:01:46 +02:00
|
|
|
async cleanup(id) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/cleanup`, {}, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 202) return [error || result];
|
|
|
|
|
return [null, result.body.taskId];
|
|
|
|
|
},
|
2025-08-04 11:48:00 +02:00
|
|
|
async status(id) {
|
2025-07-28 11:01:46 +02:00
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.get(`${API_ORIGIN}/api/v1/backup_sites/${id}/status`, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 200) return [error || result];
|
|
|
|
|
return [null, result.body];
|
|
|
|
|
},
|
|
|
|
|
async remount(id) {
|
|
|
|
|
let error, result;
|
|
|
|
|
try {
|
2025-09-12 09:48:37 +02:00
|
|
|
result = await fetcher.post(`${API_ORIGIN}/api/v1/backup_sites/${id}/remount`, {}, { access_token: accessToken });
|
2025-07-28 11:01:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error || result.status !== 202) return [error || result];
|
|
|
|
|
return [null, result.body];
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-26 11:05:49 +02:00
|
|
|
const backupRetentions = [
|
|
|
|
|
{ name: '2 days', id: { keepWithinSecs: 2 * 24 * 60 * 60 }},
|
|
|
|
|
{ name: '1 week', id: { keepWithinSecs: 7 * 24 * 60 * 60 }}, // default
|
|
|
|
|
{ name: '1 month', id: { keepWithinSecs: 30 * 24 * 60 * 60 }},
|
|
|
|
|
{ name: '3 months', id: { keepWithinSecs: 3 * 30 * 24 * 60 * 60 }},
|
|
|
|
|
{ name: '2 daily, 4 weekly', id: { keepDaily: 2, keepWeekly: 4 }},
|
|
|
|
|
{ name: '3 daily, 4 weekly, 6 monthly', id: { keepDaily: 3, keepWeekly: 4, keepMonthly: 6 }},
|
|
|
|
|
{ name: '7 daily, 4 weekly, 12 monthly', id: { keepDaily: 7, keepWeekly: 4, keepMonthly: 12 }},
|
|
|
|
|
{ name: 'Forever', id: { keepWithinSecs: -1 }}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// values correspond to cron days
|
|
|
|
|
const cronDays = [
|
|
|
|
|
{ id: 0, name: 'Sunday' },
|
|
|
|
|
{ id: 1, name: 'Monday' },
|
|
|
|
|
{ id: 2, name: 'Tuesday' },
|
|
|
|
|
{ id: 3, name: 'Wednesday' },
|
|
|
|
|
{ id: 4, name: 'Thursday' },
|
|
|
|
|
{ id: 5, name: 'Friday' },
|
|
|
|
|
{ id: 6, name: 'Saturday' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// generates 24h time sets (instead of american 12h) to avoid having to translate everything to locales eg. 12:00
|
|
|
|
|
const cronHours = Array.from({ length: 24 }).map(function (v, i) { return { id: i, name: (i < 10 ? '0' : '') + i + ':00' }; });
|
|
|
|
|
|
|
|
|
|
function prettyBackupSchedule(pattern) {
|
|
|
|
|
if (!pattern) return '';
|
|
|
|
|
const tmp = pattern.split(' ');
|
|
|
|
|
const hours = tmp[2].split(','), days = tmp[5].split(',');
|
|
|
|
|
let prettyDay;
|
|
|
|
|
if (days.length === 7 || days[0] === '*') {
|
|
|
|
|
prettyDay = 'Everyday';
|
|
|
|
|
} else {
|
|
|
|
|
prettyDay = days.map(function (day) { return cronDays[parseInt(day, 10)].name.substr(0, 3); }).join(',');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const prettyHour = hours.map(function (hour) { return cronHours[parseInt(hour, 10)].name; }).join(',');
|
|
|
|
|
return prettyDay + ' at ' + prettyHour;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function prettyBackupRetention(retention) {
|
|
|
|
|
function stableStringify(obj) { return JSON.stringify(obj, Object.keys(obj).sort()); }
|
|
|
|
|
const tmp = backupRetentions.find(function (p) { return stableStringify(p.id) === stableStringify(retention); });
|
|
|
|
|
return tmp ? tmp.name : '';
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-26 11:51:10 +02:00
|
|
|
function prettyBackupContents(contents) {
|
|
|
|
|
if (!contents) return 'Everything';
|
|
|
|
|
console.log(contents);
|
|
|
|
|
if (contents.include) return `Only ${contents.include.length} item(s)`;
|
|
|
|
|
if (contents.exclude) return `Exclude ${contents.exclude.length} item(s)`;
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 11:01:46 +02:00
|
|
|
export default {
|
|
|
|
|
create,
|
2025-09-26 11:05:49 +02:00
|
|
|
backupRetentions,
|
|
|
|
|
cronDays,
|
|
|
|
|
cronHours,
|
|
|
|
|
prettyBackupSchedule,
|
2025-09-26 11:51:10 +02:00
|
|
|
prettyBackupRetention,
|
|
|
|
|
prettyBackupContents
|
2025-07-28 11:01:46 +02:00
|
|
|
};
|