From 25d122d6be2c0528877602070eddeb7d6eccb15e Mon Sep 17 00:00:00 2001 From: Aleem Haji Date: Thu, 17 Oct 2024 22:16:50 -0400 Subject: [PATCH] Updates uptime-kuma to pvcs + reduces daily backups by ~300MB --- hope.yaml | 7 ++- uptime-kuma/backup.js | 68 +++++++++++++++++++++++ uptime-kuma/uptime-kuma-backup.yaml | 85 +++++++++++++++++++++++++++++ uptime-kuma/uptime-kuma.yaml | 19 +++++-- 4 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 uptime-kuma/backup.js create mode 100644 uptime-kuma/uptime-kuma-backup.yaml diff --git a/hope.yaml b/hope.yaml index ef4a92c..edaf5d0 100644 --- a/hope.yaml +++ b/hope.yaml @@ -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 @@ -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: diff --git a/uptime-kuma/backup.js b/uptime-kuma/backup.js new file mode 100644 index 0000000..7affb5e --- /dev/null +++ b/uptime-kuma/backup.js @@ -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