Daily backup of settings on FTP

Below is a description of the script, designed to save the settings of a group of master units on the ftp server.

Running the script is provided on the operating system of the user (administrator). To periodically run this script you need to use the task scheduler of your operating system (cron, anacron - for * nix, Task Scheduler, nnCron - for Win, etc.)

To run the script requires Python 3, the distributive which can be downloaded here: https://www.python.org/downloads/, or installed from the repository of your operating system.

To configure the script You must specify the parameters in the User settings tab:

Parameter

Function

Parameter

Function

username

User account name with administrator privileges on the device

password

User account password with administrator privileges on the device

ftpurl

FTP server address

ftpuser

FTP server user

ftppassword

FTP server password

ftpdir

FTP directory for copies of setting files

The list of monitoring units' IP addresses is defined in the file hosts.txt with one IP address on each line. For Example:

hosts.txt
192.168.0.193 192.168.0.194 telemetry.asia

Configuration files are stored on the ftp server in the specified directory as settings_mm.dd.yy-hh: mm: ss.vut

The text of the script is attached below:

vtbackup.py Group settings backup
#!/usr/bin/python3 # # vtbackup.py # # Vutlan backup settings script v.0.3 # __author__ = 'Vutlan' import datetime import ftplib import hashlib import os import sys import time import urllib.request from urllib.request import urlretrieve import urllib.parse from xml.dom.minidom import parse, parseString # get Python version # print(sys.version_info) #----------------------------------- # # User settings # #----------------------------------- # admin username on device username = 'guest' # password on device password = 'guest' # or setup hash: h=... hash_object = hashlib.sha1(str.encode(password)) h = hash_object.hexdigest() #print(h) # ftp url: ftpurl = 'ftp.server.com' # ftp user name: ftpuser = 'user' # password for ftp user: ftppassword = 'pass' # directory on ftp server: ftpdir = '/savesettings/' #----------------------------------- # # make filename by timestamp # def MakeFilename(host, prefix='settings_', ext='.vut'): timeStamp = datetime.datetime.fromtimestamp(time.time()).strftime("%b-%d-%y-%H:%M:%S") filename = prefix+host+'-'+timeStamp+ext return filename # # auth procedure # def QueryAuth(host, username, h): param = "querytype=auth&name=%s&h=%s" % (username, h) hosturl="http://"+host+"/" response = urllib.request.urlopen(hosturl+"engine.htm", param.encode("ascii")) #print(response.read()); xmldoc = parseString(response.read().decode("utf-8")) itemlist = xmldoc.getElementsByTagName('error') if len(itemlist) : print("Error"+itemlist[0].attributes['type'].value) return else : itemlist = xmldoc.getElementsByTagName('user') if len(itemlist) : k=itemlist[0].attributes['k'].value #print(k) return k # # get settings procedure # def QuerySettingsGet(host, k, filename): hosturl="http://"+host+"/" param = ("k=%s" % (k)) urlretrieve(hosturl+"settings.htm?"+param, filename) # # get dump procedure # def QueryDumpGet(host, k): param = "&txt=true&id=201001" pparam = ("k=%s&%s" % (k,param)) print(pparam) urlretrieve(hosturl+"dump.htm?"+pparam, "dump.xml") # # Upload to FTP server # def FtpUpload(ftpurl, ftpuser,ftppassword, ftpdir, filename): # file to send file = open(filename,'rb') # send the file session = ftplib.FTP(ftpurl, ftpuser, ftppassword) session.cwd(ftpdir) session.storbinary('STOR '+filename, file) session.quit() # close file file.close() # # backup device settings # def HostBackup(host): # make filename filename = MakeFilename(host) #print(filename) #return try : # auth on device k = QueryAuth(host, username,h) #print(k) # get file settings from device QuerySettingsGet(host, k, filename) # upload to ftp FtpUpload(ftpurl, ftpuser,ftppassword, ftpdir, filename) # tidy os.remove(filename) print("%s\t - backup succeeded" % (host)) except: print("%s\t - backup failed" % (host)) # # backup settings for devices from list (text file) # def HostsListBackup(hosts): with open(hosts, 'r') as f: lines = f.readlines() for host in lines: HostBackup(host.rstrip('\n')) # # main programm # HostsListBackup("hosts.txt");

Download: vtbackup.py


To run the script, you must run:

Start script
> python vtbackup.py or $ python3 vtbackup.py

In this case, the path to the Python interpreter must be specified in the PATH, or you must specify the full path in the start line.

The result of the script:

Result