66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
import paramiko
|
|
import sys
|
|
import datetime
|
|
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
|
|
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
|
db_targets = [
|
|
{
|
|
"cluster": "aerodrome",
|
|
"ip": "140.44.4.11",
|
|
"namespace": "infra",
|
|
"pod": "authentik-postgresql-0",
|
|
"name": "authentik",
|
|
"dump_cmd": "kubectl exec -n infra authentik-postgresql-0 -- pg_dumpall -U postgres | gzip > /var/backups/authentik_db_{timestamp}.sql.gz"
|
|
},
|
|
{
|
|
"cluster": "aerodrome",
|
|
"ip": "140.44.4.11",
|
|
"namespace": "awx",
|
|
"pod": "awx-postgres-15-0",
|
|
"name": "awx",
|
|
"dump_cmd": "kubectl exec -n awx awx-postgres-15-0 -- pg_dumpall -U postgres | gzip > /var/backups/awx_db_{timestamp}.sql.gz"
|
|
},
|
|
{
|
|
"cluster": "spaceport",
|
|
"ip": "140.44.4.41",
|
|
"namespace": "infrastructure",
|
|
"pod": "gitea-postgresql-0",
|
|
"name": "gitea",
|
|
"dump_cmd": "kubectl exec -n infrastructure gitea-postgresql-0 -- pg_dumpall -U postgres | gzip > /var/backups/gitea_db_{timestamp}.sql.gz"
|
|
},
|
|
{
|
|
"cluster": "spaceport",
|
|
"ip": "140.44.4.41",
|
|
"namespace": "monitoring",
|
|
"pod": "zabbix-postgresql-0",
|
|
"name": "zabbix",
|
|
"dump_cmd": "kubectl exec -n monitoring zabbix-postgresql-0 -- pg_dumpall -U postgres | gzip > /var/backups/zabbix_db_{timestamp}.sql.gz"
|
|
}
|
|
]
|
|
|
|
for target in db_targets:
|
|
cluster = target["cluster"]
|
|
ip = target["ip"]
|
|
name = target["name"]
|
|
print(f"[{cluster.upper()}] Backing up {name} database...")
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
ssh.connect(ip, username='root', key_filename=r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet', timeout=15)
|
|
|
|
cmd = f"mkdir -p /var/backups && {target['dump_cmd'].replace('{timestamp}', timestamp)}"
|
|
stdin, stdout, stderr = ssh.exec_command(cmd, timeout=300)
|
|
exit_status = stdout.channel.recv_exit_status()
|
|
err = stderr.read().decode().strip()
|
|
|
|
if exit_status == 0:
|
|
check_cmd = f"ls -lh /var/backups/{name}_db_{timestamp}.sql.gz"
|
|
_, chk_out, _ = ssh.exec_command(check_cmd)
|
|
print(f" SUCCESS: {chk_out.read().decode().strip()}")
|
|
else:
|
|
print(f" FAILED (exit {exit_status}): {err}")
|
|
|
|
ssh.close()
|