Backup Python Script: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Thomas (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „<pre> #!/usr/bin/python import sys import re import os import datetime import paramiko mandant = sys.argv[1] configdir = "/share/backup/config/" configfile = c…“) |
Thomas (Diskussion | Beiträge) |
||
Zeile 6: | Zeile 6: | ||
import datetime | import datetime | ||
import paramiko | import paramiko | ||
− | + | import getopt | |
− | |||
− | |||
− | |||
− | |||
class mandant_class(object): | class mandant_class(object): | ||
Zeile 17: | Zeile 13: | ||
self.cdir = cdir | self.cdir = cdir | ||
self.datei = self.cdir + self.man + ".cfg" | self.datei = self.cdir + self.man + ".cfg" | ||
− | |||
self.bdir = bdir | self.bdir = bdir | ||
self.ldir = ldir | self.ldir = ldir | ||
+ | |||
+ | def try_open(self): | ||
+ | if os.path.exists(self.datei): | ||
+ | self.dat = open(self.datei,"r") | ||
+ | self.dat_open = True | ||
+ | return True | ||
+ | else: | ||
+ | self.dat_open = False | ||
+ | return False | ||
+ | |||
def computer(self): | def computer(self): | ||
Zeile 47: | Zeile 52: | ||
def source(self,r,v): | def source(self,r,v): | ||
− | src = | + | src = r + ":" + v + "/ " |
return src | return src | ||
Zeile 75: | Zeile 80: | ||
return False | return False | ||
− | def __del__(self): | + | def backup_dirs(self,r): |
+ | for verzeichnis in self.dirs(r): | ||
+ | if self.ssh_check(r): | ||
+ | cmd = "rsync -azx " + self.source(r,verzeichnis) + self.dest(r,verzeichnis) + " > " + self.log(r,verzeichnis) + ".0" | ||
+ | else: | ||
+ | cmd = "echo ssh error > " + self.log(r,verzeichnis) + ".1" | ||
+ | print cmd | ||
+ | #os.system(cmd) | ||
+ | |||
+ | def list_dirs(self,r): | ||
+ | for verzeichnis in self.dirs(r): | ||
+ | cmd = self.source(r,verzeichnis) | ||
+ | print cmd | ||
+ | |||
+ | |||
+ | |||
+ | def backup_mandant(self): | ||
+ | for rechner in self.computer(): | ||
+ | self.backup_dirs(rechner) | ||
+ | |||
+ | def __del__(self): | ||
+ | if self.dat_open: | ||
self.dat.close() | self.dat.close() | ||
− | |||
− | + | ||
− | + | options, mandant = getopt.getopt(sys.argv[1:], 'r:l:' ) | |
− | + | if len(mandant) == 0: | |
− | + | print sys.argv[0] + " (-r rechner|-l rechner) mandant" | |
+ | else: | ||
+ | configdir = "/share/backup/config/" | ||
+ | configfile = configdir + mandant[0] + ".cfg" | ||
+ | backupdir = "/share/backup/" | ||
+ | logdir = "/var/log/backup" | ||
+ | x = mandant_class(mandant[0],backupdir,logdir,configdir) | ||
+ | if x.try_open(): | ||
+ | if len(options) == 0: | ||
+ | x.backup_mandant() | ||
else: | else: | ||
− | + | for opt, arg in options: | |
− | + | if opt in ('-r'): | |
− | + | rechner = arg | |
+ | x.backup_dirs(rechner) | ||
+ | elif opt in ('-l'): | ||
+ | rechner = arg | ||
+ | x.list_dirs(rechner) | ||
+ | else: | ||
+ | print "can't open mandant file" | ||
</pre> | </pre> |
Aktuelle Version vom 16. Dezember 2017, 09:08 Uhr
#!/usr/bin/python import sys import re import os import datetime import paramiko import getopt class mandant_class(object): def __init__(self,man,bdir,ldir,cdir): self.man = man self.cdir = cdir self.datei = self.cdir + self.man + ".cfg" self.bdir = bdir self.ldir = ldir def try_open(self): if os.path.exists(self.datei): self.dat = open(self.datei,"r") self.dat_open = True return True else: self.dat_open = False return False def computer(self): self.dat.seek(0) comp_array = [] for line in self.dat: compi,dirs = line.split(":") comp_array.append(compi) return comp_array def dirs(self,comp): self.dat.seek(0) return_value = "" for zeile in self.dat: line = zeile.rstrip() pattern = re.match(comp,line) if pattern: compi,dirs = line.split(":") dir_array = dirs.split(";") return_value = dir_array return return_value def today(self): today = datetime.date.today() str_today = str(today) return str_today def source(self,r,v): src = r + ":" + v + "/ " return src def dest(self,r,v): mod_y = v.replace("/","-") mod_v = mod_y[1:] dst = self.bdir + self.man + "/" + r + "/" + self.today() + "/" + mod_v if not os.path.isdir(dst): os.makedirs(dst) return dst def log(self,r,v): mod_y = v.replace("/","-") mod_v = mod_y[1:] manlogdir = self.ldir + "/" + self.man + "/" + r + "/" + mod_v + "/" if not os.path.isdir(manlogdir): os.makedirs(manlogdir) return manlogdir + self.today() def ssh_check(self,r): client = paramiko.SSHClient() client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) try: client.connect(r, username="root",port="8472") return True except Exception, e: return False def backup_dirs(self,r): for verzeichnis in self.dirs(r): if self.ssh_check(r): cmd = "rsync -azx " + self.source(r,verzeichnis) + self.dest(r,verzeichnis) + " > " + self.log(r,verzeichnis) + ".0" else: cmd = "echo ssh error > " + self.log(r,verzeichnis) + ".1" print cmd #os.system(cmd) def list_dirs(self,r): for verzeichnis in self.dirs(r): cmd = self.source(r,verzeichnis) print cmd def backup_mandant(self): for rechner in self.computer(): self.backup_dirs(rechner) def __del__(self): if self.dat_open: self.dat.close() options, mandant = getopt.getopt(sys.argv[1:], 'r:l:' ) if len(mandant) == 0: print sys.argv[0] + " (-r rechner|-l rechner) mandant" else: configdir = "/share/backup/config/" configfile = configdir + mandant[0] + ".cfg" backupdir = "/share/backup/" logdir = "/var/log/backup" x = mandant_class(mandant[0],backupdir,logdir,configdir) if x.try_open(): if len(options) == 0: x.backup_mandant() else: for opt, arg in options: if opt in ('-r'): rechner = arg x.backup_dirs(rechner) elif opt in ('-l'): rechner = arg x.list_dirs(rechner) else: print "can't open mandant file"