Added systemd services for backups
This commit is contained in:
@@ -1,14 +0,0 @@
|
|||||||
[Unit]
|
|
||||||
Description=Backup Home Assistant
|
|
||||||
After=network.target
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
ExecStart=/home/willem/.bun/bin/bun /home/willem/repos/homeAutomation/home-server/homeassistant-backup/homeassistant-backup/index.ts
|
|
||||||
WorkingDirectory=/home/willem/repos/homeAutomation/home-server/homeassistant-backup/homeassistant-backup
|
|
||||||
Restart=on-failure
|
|
||||||
User=willem
|
|
||||||
Environment=PATH=/usr/bin:/usr/local/bin
|
|
||||||
Environment=NODE_ENV=production
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
import { $ } from "bun";
|
|
||||||
import moment from "moment";
|
|
||||||
import logger from "pino";
|
|
||||||
|
|
||||||
const log = logger();
|
|
||||||
|
|
||||||
const timestamp = moment().format("YYYY-MM-DD_hh-mm-ss"); // Return today's date time
|
|
||||||
|
|
||||||
log.info('Creating new copy')
|
|
||||||
// Sync new copy
|
|
||||||
await $`rsync -a /etc/homeassistant /mnt/usb/backup_homeassistant_${timestamp}`;
|
|
||||||
|
|
||||||
log.info('Only keep last 3 copies')
|
|
||||||
// Only keep last 3 copies
|
|
||||||
await $`cd /mnt/usb && ls | grep homeassistant | tail -n +4 | xargs rm -rf`;
|
|
||||||
BIN
home-server/systemd/recurring-tasks/homeassistant-backup/homeassistant-backup
Executable file
BIN
home-server/systemd/recurring-tasks/homeassistant-backup/homeassistant-backup
Executable file
Binary file not shown.
@@ -0,0 +1,86 @@
|
|||||||
|
import { $ } from "bun";
|
||||||
|
import moment from "moment";
|
||||||
|
import logger from "pino";
|
||||||
|
import { parseArgs } from "util";
|
||||||
|
|
||||||
|
const { values } = parseArgs({
|
||||||
|
args: Bun.argv,
|
||||||
|
options: {
|
||||||
|
mode: {
|
||||||
|
type: "string",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
sourceFolder: {
|
||||||
|
type: "string",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
targetFolder: {
|
||||||
|
type: "string",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
backupName: {
|
||||||
|
type: "string",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
strict: true,
|
||||||
|
allowPositionals: true,
|
||||||
|
});
|
||||||
|
const log = logger();
|
||||||
|
|
||||||
|
switch (values.mode) {
|
||||||
|
case "hot":
|
||||||
|
await hotCopy(
|
||||||
|
values.sourceFolder!,
|
||||||
|
values.targetFolder!,
|
||||||
|
values.backupName!,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "cold":
|
||||||
|
await coldCopy(
|
||||||
|
values.sourceFolder!,
|
||||||
|
values.targetFolder!,
|
||||||
|
values.backupName!,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3 cold copies are stored
|
||||||
|
async function hotCopy(
|
||||||
|
sourceFolder: string,
|
||||||
|
targetFolder: string,
|
||||||
|
backupName: string,
|
||||||
|
) {
|
||||||
|
const timestamp = moment().format("YYYY-MM-DD_HH-mm-ss"); // Return today's date time
|
||||||
|
|
||||||
|
log.info("Creating new copy");
|
||||||
|
await $`mkdir -p ${targetFolder}`;
|
||||||
|
// Sync new copy
|
||||||
|
await $`rsync -a ${sourceFolder} ${targetFolder}/backup_${backupName}_${timestamp}`;
|
||||||
|
|
||||||
|
log.info("Only keep last 3 copies");
|
||||||
|
// Only keep last 3 copies
|
||||||
|
await $`cd ${targetFolder} && ls | grep ${backupName} | sort -r | tail -n +4 | xargs rm -rf`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only 1 cold copy is retained
|
||||||
|
async function coldCopy(
|
||||||
|
sourceFolder: string,
|
||||||
|
targetFolder: string,
|
||||||
|
backupName: string,
|
||||||
|
) {
|
||||||
|
const timestamp = moment().format("YYYY-MM-DD_HH-mm-ss"); // Return today's date time
|
||||||
|
|
||||||
|
log.info("Creating new copy");
|
||||||
|
|
||||||
|
// find latest backup
|
||||||
|
const sourceFolderToBackup =
|
||||||
|
await $`cd ${sourceFolder} && ls | grep ${backupName} | sort | tail -n 1`.text().then(resp => resp.replace('\n',''));
|
||||||
|
log.info(`Folder to back up: ${sourceFolderToBackup}`);
|
||||||
|
await $`mkdir -p ${targetFolder}`;
|
||||||
|
await $`zip -r ${targetFolder}/${backupName}_${timestamp}.zip ${sourceFolder}/${sourceFolderToBackup}`;
|
||||||
|
|
||||||
|
log.info("Only keep last copy");
|
||||||
|
// Only keep last copy
|
||||||
|
await $`cd ${targetFolder} && ls | grep ${backupName} | sort -r | tail -n +2 | xargs rm -rf`;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Backup Home Assistant - Cold
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=homeassistant-backup --mode cold --sourceFolder /mnt/usb/homeassistant/hot --targetFolder /mnt/usb/homeassistant/cold --backupName homeassistant
|
||||||
|
WorkingDirectory=/etc/homeassistant
|
||||||
|
Restart=off
|
||||||
|
User=willem
|
||||||
|
Environment=PATH=/usr/bin:/usr/local/bin
|
||||||
|
Environment=NODE_ENV=production
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Run Home assistant cold backup Service every day
|
||||||
|
|
||||||
|
[Timer]
|
||||||
|
OnBootSec=1min
|
||||||
|
OnUnitActiveSec=1day
|
||||||
|
Persistent=true
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Backup Home Assistant - Hot
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=homeassistant-backup --mode hot --sourceFolder /etc/homeassistant --targetFolder /mnt/usb/homeassistant/hot --backupName homeassistant
|
||||||
|
WorkingDirectory=/etc/homeassistant
|
||||||
|
Restart=off
|
||||||
|
User=willem
|
||||||
|
Environment=PATH=/usr/bin:/usr/local/bin
|
||||||
|
Environment=NODE_ENV=production
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Run Home assistant backup Service every hour
|
Description=Run Home assistant hot backup Service every hour
|
||||||
|
|
||||||
[Timer]
|
[Timer]
|
||||||
OnBootSec=1min
|
OnBootSec=1min
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Pull zettelkasten
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=git pull
|
||||||
|
WorkingDirectory=/home/willem/repos/zettelkasten
|
||||||
|
Restart=no
|
||||||
|
User=willem
|
||||||
|
Environment=PATH=/usr/bin:/usr/local/bin
|
||||||
|
Environment=NODE_ENV=production
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
11
home-server/systemd/recurring-tasks/pull-zettelkasten.timer
Normal file
11
home-server/systemd/recurring-tasks/pull-zettelkasten.timer
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Pull zettelkasten every hour
|
||||||
|
|
||||||
|
[Timer]
|
||||||
|
OnBootSec=1min
|
||||||
|
OnUnitActiveSec=1min
|
||||||
|
Persistent=true
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
||||||
|
|
||||||
14
home-server/systemd/recurring-tasks/send-todo-mail.service
Normal file
14
home-server/systemd/recurring-tasks/send-todo-mail.service
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Send todo mail
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=sh -c "rg '\- \[ \]' . | mail -s todos willemserruys@proton.me"
|
||||||
|
WorkingDirectory=/home/willem/repos/zettelkasten
|
||||||
|
Restart=no
|
||||||
|
User=willem
|
||||||
|
Environment=PATH=/usr/bin:/usr/local/bin
|
||||||
|
Environment=NODE_ENV=production
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
11
home-server/systemd/recurring-tasks/send-todo-mail.timer
Normal file
11
home-server/systemd/recurring-tasks/send-todo-mail.timer
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Send ToDo mail
|
||||||
|
|
||||||
|
[Timer]
|
||||||
|
OnBootSec=1min
|
||||||
|
OnUnitActiveSec=5min
|
||||||
|
Persistent=true
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
||||||
|
|
||||||
Reference in New Issue
Block a user