-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates uptime-kuma to pvcs + reduces daily backups by ~300MB
- Loading branch information
Showing
4 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters