Hi,
Ich habe meine VM soweit am Laufen mit 1.10 und Bookworm bzw. SV3.4. Danke nochmal an alle die mich dabei unterstützt haben.
Eine Sache die ich noch realisieren möchte, ist ein regelmäßiges Backup meine smarthome.db.
Dazu läuft aktuell eine .py via crontab einmal täglich (derzeit noch eine Dummy.db), dass die Datei auf eine NFS Freigabe kopiert.
Sinnvollerweise sollte shng jedoch vorher gestoppt und danach wieder gestartet werden.
'sudo systemctl stop smarthome.service' liefert aber eine Fehlermeldung wenn ich diesen in meine .py integriere.
Kann mir dabei jemand helfen?
mfg
Markus
Ich habe meine VM soweit am Laufen mit 1.10 und Bookworm bzw. SV3.4. Danke nochmal an alle die mich dabei unterstützt haben.
Eine Sache die ich noch realisieren möchte, ist ein regelmäßiges Backup meine smarthome.db.
Dazu läuft aktuell eine .py via crontab einmal täglich (derzeit noch eine Dummy.db), dass die Datei auf eine NFS Freigabe kopiert.
Sinnvollerweise sollte shng jedoch vorher gestoppt und danach wieder gestartet werden.
'sudo systemctl stop smarthome.service' liefert aber eine Fehlermeldung wenn ich diesen in meine .py integriere.
Kann mir dabei jemand helfen?
Code:
#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals
import sqlite3
import shutil
import time
import os
DESCRIPTION = """
Create a timestamped SQLite database backup, and
clean backups older than a defined number of days
"""
sudo systemctl stop smarthome.service
print("smarthome stopped")
# How old a file needs to be in order
NO_OF_DAYS = 5
def sqlite3_backup(dbfile, backupdir):
"""Create timestamped database copy"""
if not os.path.isdir(backupdir):
raise Exception("Backup directory does not exist: {}".format(backupdir))
backup_file = os.path.join(backupdir, os.path.basename(dbfile) +
time.strftime("_%Y-%m-%d"))
connection = sqlite3.connect(dbfile)
cursor = connection.cursor()
# Lock database before making a backup
cursor.execute('begin immediate')
# Make new backup file
shutil.copyfile(dbfile, backup_file)
print("\nCreating {}".format(backup_file))
# Unlock database
connection.rollback()
def clean_data(backup_dir):
"""Delete files older than NO_OF_DAYS days"""
print("Cleaning up old backups")
for filename in os.listdir(backup_dir):
backup_file = os.path.join(backup_dir, filename)
if os.path.isfile(backup_file):
if os.stat(backup_file).st_ctime < (time.time() - NO_OF_DAYS * 86400):
os.remove(backup_file)
print("Deleting {}...".format(backup_file))
#
if __name__ == "__main__":
db_file = "/usr/local/smarthome/var/db/smarthomeng____.db"
backup_dir = "/mnt/FuxlHomeBackupDB"
sqlite3_backup(db_file, backup_dir)
clean_data(backup_dir)
print("Backup update has been successful.")
sudo systemctl start smarthome.service
print("smarthome started")
Code:
0 4 * * * /usr/bin/python3 /usr/local/smarthome/tools/BackupScript.py >> /usr/local/smarthome/var/log/smarthome-dbbackup.log 2>&1
Markus


Kommentar