/system backup save [name=]
/tool fetch address= mode=ftp dst-path= src-path= user= password= upload=yes
/file remove
#!/usr/bin/env python # -*- coding: utf-8 -*- # for SSH from paramiko import SSHClient from paramiko import AutoAddPolicy # for versioning import datetime # for file operations import os # for sleep import time # versioning Version = datetime.date.today() #print "\n" + str(Version) # hosts array IP1, IP2, IP3 hosts = ( "1.2.3.4", "5.6.7.8" , "9.10.11.12") # username users = ( "user1", "user2", "user3") iterUser = iter(users) # userpassword passwords = ( "pass1", "pass2", "pass3" ) iterPassword = iter(passwords) # FTPD IP FtpdIP = "13.14.15.16" # ftp user account ftpUser = "ftpuser" ftpPass = "ftppass" # keep backups for 4 weeks backtime = datetime.timedelta(weeks=-4) sshCli = SSHClient() sshCli.set_missing_host_key_policy(AutoAddPolicy()) print "header done" # loop host adresses for host in hosts: print "\n" + str(host) # iterate through user-password pairs user = iterUser.next() Password = iterPassword.next() # define operations CreateLocalBckp = "system backup save name=" + str(host) + "_" + str(Version) + ".backup" UploadToFtp = "tool fetch address=" + str(FtpdIP) + " mode=ftp dst-path=" + str(host) + "_" + str(Version) + ".backup src-path=" + str(host) + "_" + str(Version)+ ".backup" + " user=" + str(ftpUser) + " password=" + str(ftpPass) + " upload=yes" RemoveLocalBckp = 'file remove "' + str(host) + "_" + str(Version) + ".backup" + '"' # try for not to fail the whole script on one error try: print "connecting.." + str(host) + "@" + str(user) + ":" + str(Password) sshCli.connect(str(host), port=2022, username=str(user), password=str(Password)) print "connected.." # creating local backup print "creating local backup.. /" + CreateLocalBckp sshCli.exec_command(CreateLocalBckp) # sleep after each command because mikrotik can not do it so fast as script executes time.sleep(2) print "local backup created.." # uloading local backup to ftp print "uploading local backup to ftp.. /" + UploadToFtp sshCli.exec_command(UploadToFtp) time.sleep(2) print "backup uploaded to remote location.." # removing local backup time.sleep(2) print "removing local backup.. /" + RemoveLocalBckp sshCli.exec_command(RemoveLocalBckp) time.sleep(2) print "local backup removed.." sshCli.close() # try delete old file (if exists) try: os.remove("/tftp/" + str(host) + "_" + str(Version + backtime) +".cfg") except: print "Error while trying to delete old backup " + "/tftp/" + str(host) + "_" + str(Version + backtime) +".cfg" except: print "Error connecting to host", host
Source: https://habr.com/ru/post/135541/
All Articles