Files
SnarfCode/scripts/test_swarm_sqlite_backup.py

46 lines
1.6 KiB
Python

import paramiko
test_script = """
import sqlite3, os, datetime
now = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
os.makedirs('/mnt/nfs_shares/gotify/backups', exist_ok=True)
os.makedirs('/mnt/nfs_shares/homebox/backups', exist_ok=True)
# 1. Gotify
print('Testing Gotify SQLite VACUUM INTO...')
gotify_src = '/mnt/swarm_shared/legacy_volumes/gotify/_data/gotify.db'
gotify_dst = f'/mnt/nfs_shares/gotify/backups/gotify_test_{now}.db'
con = sqlite3.connect(gotify_src)
con.execute(f"VACUUM INTO '{gotify_dst}'")
con.close()
print(f'Gotify backup created: {os.path.getsize(gotify_dst)} bytes')
# 2. Homebox
print('Testing Homebox SQLite VACUUM INTO...')
homebox_src = '/mnt/swarm_shared/legacy_volumes/homebox/homebox.db'
homebox_dst = f'/mnt/nfs_shares/homebox/backups/homebox_test_{now}.db'
con = sqlite3.connect(homebox_src)
con.execute(f"VACUUM INTO '{homebox_dst}'")
con.close()
print(f'Homebox backup created: {os.path.getsize(homebox_dst)} bytes')
"""
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('140.44.4.71', username='snarf', key_filename=r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet', timeout=10)
stdin, stdout, stderr = ssh.exec_command('python3 -c "' + test_script.replace('"', '\\"').replace('\n', '; ') + '"')
# Better to write script to a file on remote
sftp = ssh.open_sftp()
with sftp.file('/tmp/test_sqlite_vacuum.py', 'w') as f:
f.write(test_script)
sftp.close()
stdin, stdout, stderr = ssh.exec_command('python3 /tmp/test_sqlite_vacuum.py')
print("STDOUT:", stdout.read().decode())
print("STDERR:", stderr.read().decode())
ssh.close()