Skip to content

Commit

Permalink
Updates uptime-kuma to pvcs + reduces daily backups by ~300MB
Browse files Browse the repository at this point in the history
  • Loading branch information
Eagerod committed Oct 18, 2024
1 parent 9d5b03a commit 25d122d
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 5 deletions.
7 changes: 6 additions & 1 deletion hope.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ resources:
source: bitnami/kubectl:1.21.0
pull: *docker_hub_upstream_pull
tag: registry.internal.aleemhaji.com/kubectl:1.21.0
tags: [docker-cache, kubectl]
tags: [docker-cache, kubectl, uptime-kuma]
- name: bitwarden-image-cache
build:
source: vaultwarden/server:1.32.0
Expand Down Expand Up @@ -873,6 +873,11 @@ resources:
- name: uptime-kuma
file: uptime-kuma/uptime-kuma.yaml
tags: [apps, uptime-kuma]
- name: uptime-kuma-backup
file: uptime-kuma/uptime-kuma-backup.yaml
fileParameters:
- BACKUP_SCRIPT=uptime-kuma/backup.js
tags: [apps, uptime-kuma]
- name: knowledge-service
file: barebones-nginx/service.yaml
parameters:
Expand Down
68 changes: 68 additions & 0 deletions uptime-kuma/backup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const sqlite3 = require('@louislam/sqlite3').verbose();

function exportTable(db, tableName, callback) {
let output = 'PRAGMA foreign_keys=OFF;\n';
output += 'BEGIN TRANSACTION;\n';

// Step 1: Get the schema of the table (CREATE TABLE statement)
db.get(`SELECT sql FROM sqlite_master WHERE type='table' AND name=?`, [tableName], (err, row) => {
if (err) {
console.error(err);
return;
}

if (row) {
const createTableSql = row.sql.replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS');
output += `${createTableSql};\n`;
} else {
console.error(`No record found for schema: ${tableName}`);
return;
}

db.all(`SELECT * FROM \`${tableName}\``, [], (err, rows) => {
if (err) {
console.error(err);
return;
}

rows.forEach(row => {
//const columns = Object.keys(row).map(col => `"${col}"`).join(', ');
const values = Object.values(row).map(val => {
if (val === null) {
return 'NULL';
} else if (typeof val === 'string') {
if (val.indexOf("\n") != -1) {
return `replace('${val.replace(/'/g, "''").replace("\n", "\\n")}','\\n',char(10))`; // Escape single quotes in strings
}
return `'${val.replace(/'/g, "''")}'`;
} else {
return val;
}
}).join(',');

// sqlite3 binary dump wraps with quotes for keywords; could be expanded to support more.
if (tableName == "group") {
tableName = '"group"';
}
// output += `INSERT INTO ${tableName} (${columns}) VALUES (${values});\n`;
output += `INSERT INTO ${tableName} VALUES(${values});\n`;
});

output += 'COMMIT;\n';
callback(output);
});
});
}

// node <script> <source> <table>
if (process.argv.length != 4) {
console.error("Usage:");
console.error(` ${process.argv[0]} ${process.argv[1]} <source/path> <tablename>`);
process.exit(1);
}

const db = new sqlite3.Database(process.argv[2]);

exportTable(db, process.argv[3], (dump) => {
console.log(dump);
});
85 changes: 85 additions & 0 deletions uptime-kuma/uptime-kuma-backup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: uptime-kuma-backup
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: uptime-kuma-backup
rules:
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create"]
resourceNames:
- uptime-kuma-0
- apiGroups: [""]
resources: ["pods"]
verbs: ["get"]
resourceNames:
- uptime-kuma-0
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: uptime-kuma-backup
subjects:
- kind: ServiceAccount
name: uptime-kuma-backup
roleRef:
kind: Role
name: uptime-kuma-backup
apiGroup: rbac.authorization.k8s.io
---
kind: ConfigMap
apiVersion: v1
metadata:
name: uptime-kuma-backup-script
binaryData:
script.js: ${BACKUP_SCRIPT}
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: uptime-kuma-backup
spec:
schedule: "0 6 * * *"
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 3
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
serviceAccountName: uptime-kuma-backup
imagePullSecrets:
- name: registry.internal.aleemhaji.com
containers:
- name: backup
image: registry.internal.aleemhaji.com/kubectl:1.21.0
# Have to copy into /app to make sure node_modules are picked up.
command:
- sh
- -xeufc
- |
# kubectl cp /scripts/script.js uptime-kuma-0:/app/backup.js
cat /scripts/script.js | kubectl exec -i uptime-kuma-0 -- /bin/sh -c 'cat > /app/backup.js'
kubectl exec uptime-kuma-0 -- sh -c "(cd /app && node ./backup.js /app/data/kuma.db monitor)" > /backup/monitor.sql
kubectl exec uptime-kuma-0 -- sh -c "(cd /app && node ./backup.js /app/data/kuma.db notification)" > /backup/notification.sql
kubectl exec uptime-kuma-0 -- sh -c "(cd /app && node ./backup.js /app/data/kuma.db monitor_notification)" > /backup/monitor_notification.sql
kubectl exec uptime-kuma-0 -- sh -c "(cd /app && node ./backup.js /app/data/kuma.db group)" > /backup/group.sql
kubectl exec uptime-kuma-0 -- sh -c "(cd /app && node ./backup.js /app/data/kuma.db monitor_group)" > /backup/monitor_group.sql
volumeMounts:
- name: backup-source
mountPath: /backup
- name: scripts
mountPath: /scripts
volumes:
- name: backup-source
nfs:
server: 192.168.96.4
path: /mnt/main/backup/apps/uptime-kuma
- name: scripts
configMap:
name: uptime-kuma-backup-script
19 changes: 15 additions & 4 deletions uptime-kuma/uptime-kuma.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: uptime-kuma
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 10Gi
storageClassName: freenas-iscsi
---
apiVersion: apps/v1
kind: Deployment
kind: StatefulSet
metadata:
name: uptime-kuma
labels:
Expand All @@ -10,6 +21,7 @@ spec:
selector:
matchLabels:
app: uptime-kuma
serviceName: uptime-kuma
template:
metadata:
labels:
Expand All @@ -27,9 +39,8 @@ spec:
mountPath: /app/data
volumes:
- name: uptime-kuma-persistent-storage
nfs:
server: 192.168.96.4
path: /mnt/main/apps/uptime-kuma
persistentVolumeClaim:
claimName: uptime-kuma
---
apiVersion: v1
kind: Service
Expand Down

0 comments on commit 25d122d

Please sign in to comment.