[Python] Backup journalier de Domoticz vers Dropbox

Vous avez créé un script LUA dont vous êtes fier, un .sh génial, un programme Python hors du commun, un Tuto, c'est ici que vous pouvez les partager.
hobbe
Messages : 111
Inscription : 15 déc. 2014, 15:33

[Python] Backup journalier de Domoticz vers Dropbox

Message par hobbe »

Bonjour,

Aujourd'hui, je vous propose un script python pour sauvegarder les scripts et la DB de Domoticz sur Dropbox.

Le dossier scripts et le fichier domoticz.db sont zippés puis l'archive est copiée dans le dossier de synchronisation de Dropbox.
Le client Dropbox de Windows effectuera la synchronisation avec le cloud automatiquement.

Note 1 : ça fonctionnera de la même manière avec d'autres services comme Box, Drive, OneDrive, Hubic, Mega, etc.
Note 2 : ça doit fonctionner également sous Linux si un client est disponible pour votre distribution
Note 3 : il faudra adapter les chemins d'accès à Domoticz et à Dropbox

Code : Tout sélectionner

#!/usr/bin/env python
# C:\domoticz\scripts\domoticz_backup.py
# Backup Domoticz scripts and db to Dropbox folder

import time
import os
import zipfile

def zipdir(path, zipf):
    for root, dirs, files in os.walk(path):
        for file in files:
            zipf.write(os.path.join(root, file))

if __name__ == '__main__':
    thedate = time.strftime("%Y%m%d-%H%M%S")
    filename = 'C:/Dropbox/Domoticz/domoticz_backup_' + thedate + '.zip'
    zipf = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED, True)
    zipdir('C:/domoticz/scripts', zipf)
    zipf.write('C:/domoticz/domoticz.db')
    zipf.close()
Il suffit ensuite de le programmer régulièrement.
Par exemple, ce script lua le déclenche une fois par jour :

Code : Tout sélectionner

-- /domoticz/scripts/lua/script_time_backup.lua
-- Execute Python backup script

-- Path to python backup script
-- backupScript = 'C:\\domoticz\\scripts\\domoticz_backup.py'
backupScript = '/home/pi/domoticz/scripts/domoticz_backup.py'

commandArray = {}
time = os.date("*t")

-- Trigger at 11:30
if (time.min == 30 and time.hour == 11) then
  os.execute(backupScript);
end

return commandArray
Répondre