Compare commits

..

No commits in common. "96233dcd7b99dc4852ac2e0e089a770f6bc3f5c9" and "56cfb4ee787825c9c15ebbab64f9df9de24fa0a7" have entirely different histories.

10 changed files with 74 additions and 192 deletions

View File

@ -2,10 +2,6 @@ import express from "express";
import { doBackupWorkflow } from "../services/orchestrator.js"; import { doBackupWorkflow } from "../services/orchestrator.js";
import { WorkflowType } from "../types/services/orchecstrator.js"; import { WorkflowType } from "../types/services/orchecstrator.js";
import logger from "../config/winston.js"; import logger from "../config/winston.js";
import { clean as webdavClean } from "../services/webdavService.js";
import { getBackupConfig } from "../services/backupConfigService.js";
import { getWebdavConfig } from "../services/webdavConfigService.js";
import { clean } from "../services/homeAssistantService.js";
const actionRouter = express.Router(); const actionRouter = express.Router();
@ -20,20 +16,4 @@ actionRouter.post("/backup", (req, res) => {
res.sendStatus(202); res.sendStatus(202);
}); });
actionRouter.post("/clean", (req, res) => {
const backupConfig = getBackupConfig();
const webdavConfig = getWebdavConfig();
webdavClean(backupConfig, webdavConfig)
.then(() => {
return clean(backupConfig);
})
.then(() => {
logger.info("All good !");
})
.catch(() => {
logger.error("Something wrong !");
});
res.sendStatus(202);
});
export default actionRouter; export default actionRouter;

View File

@ -13,15 +13,13 @@ import { promisify } from "util";
import logger from "../config/winston.js"; import logger from "../config/winston.js";
import messageManager from "../tools/messageManager.js"; import messageManager from "../tools/messageManager.js";
import * as statusTools from "../tools/status.js"; import * as statusTools from "../tools/status.js";
import { import { BackupType } from "../types/services/backupConfig.js";
BackupType,
type BackupConfig,
} from "../types/services/backupConfig.js";
import type { NewBackupPayload } from "../types/services/ha_os_payload.js"; import type { NewBackupPayload } from "../types/services/ha_os_payload.js";
import type { import type {
AddonData, AddonData,
BackupData, BackupData,
BackupDetailModel, BackupDetailModel,
BackupModel,
CoreInfoBody, CoreInfoBody,
SupervisorResponse, SupervisorResponse,
} from "../types/services/ha_os_response.js"; } from "../types/services/ha_os_response.js";
@ -163,7 +161,7 @@ function downloadSnapshot(id: string): Promise<string> {
} }
function delSnap(id: string) { function delSnap(id: string) {
logger.debug(`Deleting Home Assistant backup ${id}`); logger.info(`Deleting Home Assistant backup ${id}`);
const option = { const option = {
headers: { authorization: `Bearer ${token}` }, headers: { authorization: `Bearer ${token}` },
}; };
@ -264,66 +262,36 @@ function createNewBackup(
); );
} }
function clean(backupConfig: BackupConfig) { function clean(backups: BackupModel[], numberToKeep: number) {
if (!backupConfig.autoClean.homeAssistant.enabled) { const promises = [];
logger.debug("Clean disabled for Home Assistant"); if (backups.length < numberToKeep) {
return Promise.resolve(); return;
} }
logger.info("Clean for Home Assistant"); backups.sort((a, b) => {
const status = statusTools.getStatus(); return Date.parse(b.date) - Date.parse(a.date);
status.status = States.CLEAN_HA; });
status.progress = -1; const toDel = backups.slice(numberToKeep);
statusTools.setStatus(status); for (const i of toDel) {
promises.push(delSnap(i.slug));
const numberToKeep = backupConfig.autoClean.homeAssistant.nbrToKeep || 5; }
return getBackups() logger.info("Local clean done.");
.then((response) => { return Promise.allSettled(promises).then((values) => {
const backups = response.body.data.backups; let errors = false;
if (backups.length > numberToKeep) { for (const val of values) {
backups.sort((a, b) => { if (val.status == "rejected") {
return Date.parse(b.date) - Date.parse(a.date); // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
}); messageManager.error("Fail to delete backup", val.reason);
const toDel = backups.slice(numberToKeep); logger.error("Fail to delete backup");
logger.debug(`Number of backup to clean: ${toDel.length}`); logger.error(val.reason);
const promises = toDel.map((value) => delSnap(value.slug)); errors = true;
logger.info("Home Assistant clean done.");
return Promise.allSettled(promises);
} else {
logger.debug("Nothing to clean");
} }
}) }
.then( if (errors) {
(values) => { messageManager.error("Fail to clean backups in Home Assistant");
const status = statusTools.getStatus(); logger.error("Fail to clean backups in Home Assistant");
status.status = States.IDLE; return Promise.reject(new Error());
status.progress = undefined; }
statusTools.setStatus(status); });
let errors = false;
for (const val of values || []) {
if (val.status == "rejected") {
messageManager.error("Fail to delete backup", val.reason as string);
logger.error("Fail to delete backup");
logger.error(val.reason);
errors = true;
}
}
if (errors) {
messageManager.error("Fail to clean backups in Home Assistant");
logger.error("Fail to clean backups in Home Assistant");
return Promise.reject(new Error());
}
return Promise.resolve();
},
(reason: RequestError) => {
logger.error("Fail to clean Home Assistant backup", reason.message);
messageManager.error(
"Fail to clean Home Assistant backup",
reason.message
);
return Promise.reject(reason);
}
);
} }
function uploadSnapshot(path: string) { function uploadSnapshot(path: string) {

View File

@ -92,12 +92,6 @@ export function doBackupWorkflow(type: WorkflowType) {
.then(() => { .then(() => {
return homeAssistantService.startAddons(addonsToStartStop); return homeAssistantService.startAddons(addonsToStartStop);
}) })
.then(() => {
return homeAssistantService.clean(backupConfig);
})
.then(() => {
return webDavService.clean(backupConfig, webdavConfig);
})
.then(() => { .then(() => {
logger.info("Backup workflow finished successfully !"); logger.info("Backup workflow finished successfully !");
messageManager.info( messageManager.info(

View File

@ -23,7 +23,6 @@ import { templateToRegexp } from "./backupConfigService.js";
import { getChunkEndpoint, getEndpoint } from "./webdavConfigService.js"; import { getChunkEndpoint, getEndpoint } from "./webdavConfigService.js";
import { pipeline } from "stream/promises"; import { pipeline } from "stream/promises";
import { humanFileSize } from "../tools/toolbox.js"; import { humanFileSize } from "../tools/toolbox.js";
import type { BackupConfig } from "../types/services/backupConfig.js";
const CHUNK_SIZE = 5 * 1024 * 1024; // 5MiB Same as desktop client const CHUNK_SIZE = 5 * 1024 * 1024; // 5MiB Same as desktop client
const CHUNK_NUMBER_SIZE = 5; // To add landing "0" const CHUNK_NUMBER_SIZE = 5; // To add landing "0"
@ -202,7 +201,6 @@ function extractBackupInfo(backups: WebdavBackup[], template: string) {
} }
export function deleteBackup(path: string, config: WebdavConfig) { export function deleteBackup(path: string, config: WebdavConfig) {
logger.debug(`Deleting Cloud backup ${path}`);
const endpoint = getEndpoint(config); const endpoint = getEndpoint(config);
return got return got
.delete(config.url + endpoint + path, { .delete(config.url + endpoint + path, {
@ -591,7 +589,6 @@ export function downloadFile(
}, },
(reason: RequestError) => { (reason: RequestError) => {
if (fs.existsSync(tmp_file)) fs.unlinkSync(tmp_file); if (fs.existsSync(tmp_file)) fs.unlinkSync(tmp_file);
logger.error("Fail to download Cloud backup", reason.message);
messageManager.error("Fail to download Cloud backup", reason.message); messageManager.error("Fail to download Cloud backup", reason.message);
const status = statusTools.getStatus(); const status = statusTools.getStatus();
status.status = States.IDLE; status.status = States.IDLE;
@ -602,59 +599,39 @@ export function downloadFile(
); );
} }
export function clean(backupConfig: BackupConfig, webdavConfig: WebdavConfig) { // clean() {
if (!backupConfig.autoClean.webdav.enabled) { // let limit = settingsTools.getSettings().auto_clean_backup_keep;
logger.debug("Clean disabled for Cloud"); // if (limit == null) limit = 5;
return Promise.resolve(); // return new Promise((resolve, reject) => {
} // this.getFolderContent(this.getConf()?.back_dir + pathTools.auto)
logger.info("Clean for cloud"); // .then(async (contents: any) => {
const status = statusTools.getStatus(); // if (contents.length < limit) {
status.status = States.CLEAN_CLOUD; // resolve(undefined);
status.progress = -1; // return;
statusTools.setStatus(status); // }
const limit = backupConfig.autoClean.homeAssistant.nbrToKeep || 5; // contents.sort((a: any, b: any) => {
return getBackups(pathTools.auto, webdavConfig, backupConfig.nameTemplate) // return a.date < b.date ? 1 : -1;
.then((backups) => { // });
if (backups.length > limit) {
const toDel = backups.splice(limit);
logger.debug(`Number of backup to clean: ${toDel.length}`);
const promises = toDel.map((value) =>
deleteBackup(value.path, webdavConfig)
);
return Promise.allSettled(promises);
} else {
logger.debug("Nothing to clean");
}
})
.then(
(values) => {
const status = statusTools.getStatus();
status.status = States.IDLE;
status.progress = undefined;
statusTools.setStatus(status);
let errors = false; // const toDel = contents.slice(limit);
for (const val of values || []) { // for (const i in toDel) {
if (val.status == "rejected") { // await this.client?.deleteFile(toDel[i].filename);
messageManager.error("Fail to delete backup", val.reason); // }
logger.error("Fail to delete backup"); // logger.info("Cloud clean done.");
logger.error(val.reason); // resolve(undefined);
errors = true; // })
} // .catch((error) => {
} // const status = statusTools.getStatus();
// status.status = "error";
// status.error_code = 6;
// status.message = "Fail to clean Nexcloud (" + error + ") !";
// statusTools.setStatus(status);
// logger.error(status.message);
// reject(status.message);
// });
// });
// }
// }
if (errors) { // const INSTANCE = new WebdavTools();
messageManager.error("Fail to clean backups in Cloud"); // export default INSTANCE;
logger.error("Fail to clean backups in Cloud");
return Promise.reject(new Error());
}
return Promise.resolve();
},
(reason: RequestError) => {
logger.error("Fail to clean cloud backup", reason.message);
messageManager.error("Fail to clean cloud backup", reason.message);
return Promise.reject(reason);
}
);
}

View File

@ -9,8 +9,6 @@ export enum States {
BKUP_UPLOAD_CLOUD = "BKUP_UPLOAD_CLOUD", BKUP_UPLOAD_CLOUD = "BKUP_UPLOAD_CLOUD",
STOP_ADDON = "STOP_ADDON", STOP_ADDON = "STOP_ADDON",
START_ADDON = "START_ADDON", START_ADDON = "START_ADDON",
CLEAN_CLOUD = "CLEAN_CLOUD",
CLEAN_HA = "CLEAN_HA",
} }
export interface Status { export interface Status {

View File

@ -9,7 +9,7 @@
"type-check": "vue-tsc --noEmit" "type-check": "vue-tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"@mdi/font": "7.4.47", "@mdi/font": "7.0.96",
"core-js": "^3.34.0", "core-js": "^3.34.0",
"ky": "^1.2.0", "ky": "^1.2.0",
"luxon": "^3.4.4", "luxon": "^3.4.4",

View File

@ -6,8 +6,8 @@ settings:
dependencies: dependencies:
'@mdi/font': '@mdi/font':
specifier: 7.4.47 specifier: 7.0.96
version: 7.4.47 version: 7.0.96
core-js: core-js:
specifier: ^3.34.0 specifier: ^3.34.0
version: 3.36.0 version: 3.36.0
@ -391,8 +391,8 @@ packages:
/@jridgewell/sourcemap-codec@1.4.15: /@jridgewell/sourcemap-codec@1.4.15:
resolution: {integrity: sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==} resolution: {integrity: sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==}
/@mdi/font@7.4.47: /@mdi/font@7.0.96:
resolution: {integrity: sha512-43MtGpd585SNzHZPcYowu/84Vz2a2g31TvPMTm9uTiCSWzaheQySUcSyUH/46fPnuPQWof2yd0pGBtzee/IQWw==} resolution: {integrity: sha512-rzlxTfR64hqY8yiBzDjmANfcd8rv+T5C0Yedv/TWk2QyAQYdc66e0kaN1ipmnYU3RukHRTRcBARHzzm+tIhL7w==}
dev: false dev: false
/@nodelib/fs.scandir@2.1.5: /@nodelib/fs.scandir@2.1.5:

View File

@ -3,34 +3,13 @@
<v-card-title class="text-center">Action</v-card-title> <v-card-title class="text-center">Action</v-card-title>
<v-divider class="border-opacity-25"></v-divider> <v-divider class="border-opacity-25"></v-divider>
<v-card-text> <v-card-text>
<v-row> <v-btn color="success" @click="launchBackup">Backup Now</v-btn>
<v-col class="d-flex justify-center">
<v-btn
block
color="success"
@click="launchBackup"
prepend-icon="mdi-cloud-plus"
>
Backup Now
</v-btn>
</v-col>
<v-col class="d-flex justify-center">
<v-btn
block
color="orange-darken-3"
@click="launchClean"
prepend-icon="mdi-broom"
>
Clean
</v-btn>
</v-col>
</v-row>
</v-card-text> </v-card-text>
</v-card> </v-card>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { backupNow, clean } from "@/services/actionService"; import { backupNow } from "@/services/actionService";
import { useAlertStore } from "@/store/alert"; import { useAlertStore } from "@/store/alert";
const alertStore = useAlertStore(); const alertStore = useAlertStore();
@ -44,14 +23,4 @@ function launchBackup() {
alertStore.add("error", "Fail to start backup workflow !"); alertStore.add("error", "Fail to start backup workflow !");
}); });
} }
function launchClean() {
clean()
.then(() => {
alertStore.add("success", "Backup workflow started !");
})
.catch(() => {
alertStore.add("error", "Fail to start backup workflow !");
});
}
</script> </script>

View File

@ -50,10 +50,10 @@
</div> </div>
</v-col> </v-col>
</v-row> </v-row>
<v-row v-if="status?.status != States.IDLE"> <v-row>
<v-divider class="border-opacity-25 mx-n1"></v-divider> <v-divider class="border-opacity-25 mx-n1"></v-divider>
</v-row> </v-row>
<v-row v-if="status?.status != States.IDLE"> <v-row>
<v-col> <v-col>
<v-progress-linear <v-progress-linear
height="25" height="25"

View File

@ -3,7 +3,3 @@ import kyClient from "./kyClient";
export function backupNow() { export function backupNow() {
return kyClient.post("action/backup"); return kyClient.post("action/backup");
} }
export function clean() {
return kyClient.post("action/clean");
}