From d079b542be3a2d6e11d91054d1c246a7d949647f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gu=C3=A9rineau?= Date: Mon, 10 Aug 2020 10:02:46 +0200 Subject: [PATCH] comment DSSKey and add ed25519 --- libtisbackup/backup_mysql.py | 21 ++++++++------- libtisbackup/backup_oracle.py | 3 ++- libtisbackup/backup_pgsql.py | 3 ++- libtisbackup/backup_samba4.py | 9 ++++--- libtisbackup/backup_sqlserver.py | 45 ++++++++++++++++---------------- libtisbackup/common.py | 3 ++- 6 files changed, 45 insertions(+), 39 deletions(-) diff --git a/libtisbackup/backup_mysql.py b/libtisbackup/backup_mysql.py index 1890427..e45bf5a 100644 --- a/libtisbackup/backup_mysql.py +++ b/libtisbackup/backup_mysql.py @@ -34,13 +34,13 @@ from common import * class backup_mysql(backup_generic): """Backup a mysql database as gzipped sql file through ssh""" - type = 'mysql+ssh' + type = 'mysql+ssh' required_params = backup_generic.required_params + ['db_user','db_passwd','private_key'] optional_params = backup_generic.optional_params + ['db_name'] - db_name='' + db_name='' db_user='' - db_passwd='' + db_passwd='' dest_dir = "" @@ -60,7 +60,8 @@ class backup_mysql(backup_generic): try: mykey = paramiko.RSAKey.from_private_key_file(self.private_key) except paramiko.SSHException: - mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + #mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + mykey = paramiko.Ed25519Key.from_private_key_file(self.private_key) self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) @@ -75,14 +76,14 @@ class backup_mysql(backup_generic): (error_code,output) = ssh_exec(cmd,ssh=self.ssh) self.logger.debug("[%s] Output of %s :\n%s",self.backup_name,cmd,output) if error_code: - raise Exception('Aborting, Not null exit code (%i) for "%s"' % (error_code,cmd)) + raise Exception('Aborting, Not null exit code (%i) for "%s"' % (error_code,cmd)) databases = output.split('\n') for database in databases: - if database != "": + if database != "": self.db_name = database.rstrip() self.do_mysqldump(stats) - else: + else: stats['log']= "Successfully backup processed to the following database :" self.do_mysqldump(stats) @@ -130,9 +131,9 @@ class backup_mysql(backup_generic): stats['total_files_count']=1 + stats.get('total_files_count', 0) stats['written_files_count']=1 + stats.get('written_files_count', 0) stats['total_bytes']=os.stat(localpath).st_size + stats.get('total_bytes', 0) - stats['written_bytes']=os.stat(localpath).st_size + stats.get('written_bytes', 0) + stats['written_bytes']=os.stat(localpath).st_size + stats.get('written_bytes', 0) stats['log'] = '%s "%s"' % (stats['log'] ,self.db_name) - stats['backup_location'] = self.dest_dir + stats['backup_location'] = self.dest_dir stats['status']='RMTemp' cmd = 'rm -f /tmp/' + self.db_name + '-' + backup_start_date + '.sql.gz' @@ -151,7 +152,7 @@ class backup_mysql(backup_generic): filelist = os.listdir(self.backup_dir) filelist.sort() - p = re.compile('^\d{8,8}-\d{2,2}h\d{2,2}m\d{2,2}$') + p = re.compile('^\d{8,8}-\d{2,2}h\d{2,2}m\d{2,2}$') for item in filelist: if p.match(item): dir_name = os.path.join(self.backup_dir,item) diff --git a/libtisbackup/backup_oracle.py b/libtisbackup/backup_oracle.py index 53175cb..6f1a3e6 100644 --- a/libtisbackup/backup_oracle.py +++ b/libtisbackup/backup_oracle.py @@ -49,7 +49,8 @@ class backup_oracle(backup_generic): try: mykey = paramiko.RSAKey.from_private_key_file(self.private_key) except paramiko.SSHException: - mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + #mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + mykey = paramiko.Ed25519Key.from_private_key_file(self.private_key) self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) diff --git a/libtisbackup/backup_pgsql.py b/libtisbackup/backup_pgsql.py index d85f627..86c443b 100644 --- a/libtisbackup/backup_pgsql.py +++ b/libtisbackup/backup_pgsql.py @@ -53,7 +53,8 @@ class backup_pgsql(backup_generic): try: mykey = paramiko.RSAKey.from_private_key_file(self.private_key) except paramiko.SSHException: - mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + #mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + mykey = paramiko.Ed25519Key.from_private_key_file(self.private_key) self.logger.debug('[%s] Trying to connect to "%s" with username root and key "%s"',self.backup_name,self.server_name,self.private_key) self.ssh = paramiko.SSHClient() diff --git a/libtisbackup/backup_samba4.py b/libtisbackup/backup_samba4.py index c94118e..4ce284b 100644 --- a/libtisbackup/backup_samba4.py +++ b/libtisbackup/backup_samba4.py @@ -55,7 +55,8 @@ class backup_samba4(backup_generic): try: mykey = paramiko.RSAKey.from_private_key_file(self.private_key) except paramiko.SSHException: - mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + #mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + mykey = paramiko.Ed25519Key.from_private_key_file(self.private_key) self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) @@ -117,9 +118,9 @@ class backup_samba4(backup_generic): stats['total_files_count']=1 + stats.get('total_files_count', 0) stats['written_files_count']=1 + stats.get('written_files_count', 0) stats['total_bytes']=os.stat(localpath).st_size + stats.get('total_bytes', 0) - stats['written_bytes']=os.stat(localpath).st_size + stats.get('written_bytes', 0) + stats['written_bytes']=os.stat(localpath).st_size + stats.get('written_bytes', 0) stats['log'] = '%s "%s"' % (stats['log'] ,self.db_name) - stats['backup_location'] = self.dest_dir + stats['backup_location'] = self.dest_dir stats['status']='RMTemp' cmd = 'rm -f "%s"' % filepath @@ -138,7 +139,7 @@ class backup_samba4(backup_generic): filelist = os.listdir(self.backup_dir) filelist.sort() - p = re.compile('^\d{8,8}-\d{2,2}h\d{2,2}m\d{2,2}$') + p = re.compile('^\d{8,8}-\d{2,2}h\d{2,2}m\d{2,2}$') for item in filelist: if p.match(item): dir_name = os.path.join(self.backup_dir,item) diff --git a/libtisbackup/backup_sqlserver.py b/libtisbackup/backup_sqlserver.py index 1ab6b20..e155e13 100644 --- a/libtisbackup/backup_sqlserver.py +++ b/libtisbackup/backup_sqlserver.py @@ -37,10 +37,10 @@ from common import * class backup_sqlserver(backup_generic): """Backup a SQLSERVER database as gzipped sql file through ssh""" - type = 'sqlserver+ssh' + type = 'sqlserver+ssh' required_params = backup_generic.required_params + ['db_name','private_key'] optional_params = ['username', 'remote_backup_dir', 'sqlserver_before_2005', 'db_server_name', 'db_user', 'db_password'] - db_name='' + db_name='' db_user='' db_password='' userdb = "-E" @@ -48,15 +48,16 @@ class backup_sqlserver(backup_generic): remote_backup_dir = r'c:/WINDOWS/Temp/' sqlserver_before_2005 = False db_server_name = "localhost" - + def do_backup(self,stats): try: mykey = paramiko.RSAKey.from_private_key_file(self.private_key) except paramiko.SSHException: - mykey = paramiko.DSSKey.from_private_key_file(self.private_key) - + #mykey = paramiko.DSSKey.from_private_key_file(self.private_key) + mykey = paramiko.Ed25519Key.from_private_key_file(self.private_key) + self.logger.debug('[%s] Connecting to %s with user root and key %s',self.backup_name,self.server_name,self.private_key) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) @@ -64,7 +65,7 @@ class backup_sqlserver(backup_generic): t = datetime.datetime.now() backup_start_date = t.strftime('%Y%m%d-%Hh%Mm%S') - + backup_file = self.remote_backup_dir + '/' + self.db_name + '-' + backup_start_date + '.bak' if not self.db_user == '': self.userdb = '-U %s -P %s' % ( self.db_user, self.db_password ) @@ -72,14 +73,14 @@ class backup_sqlserver(backup_generic): # dump db stats['status']='Dumping' if self.sqlserver_before_2005: - cmd = """osql -E -Q "BACKUP DATABASE [%s] - TO DISK='%s' + cmd = """osql -E -Q "BACKUP DATABASE [%s] + TO DISK='%s' WITH FORMAT" """ % ( self.db_name, backup_file ) - + else: - cmd = """sqlcmd %s -S "%s" -d master -Q "BACKUP DATABASE [%s] - TO DISK = N'%s' - WITH INIT, NOUNLOAD , + cmd = """sqlcmd %s -S "%s" -d master -Q "BACKUP DATABASE [%s] + TO DISK = N'%s' + WITH INIT, NOUNLOAD , NAME = N'Backup %s', NOSKIP ,STATS = 10, NOFORMAT" """ % (self.userdb, self.db_server_name, self.db_name, backup_file ,self.db_name ) self.logger.debug('[%s] Dump DB : %s',self.backup_name,cmd) try: @@ -88,7 +89,7 @@ class backup_sqlserver(backup_generic): self.logger.debug("[%s] Output of %s :\n%s",self.backup_name,cmd,output) if error_code: raise Exception('Aborting, Not null exit code (%i) for "%s"' % (error_code,cmd)) - + # zip the file stats['status']='Zipping' cmd = 'gzip "%s"' % backup_file @@ -98,7 +99,7 @@ class backup_sqlserver(backup_generic): self.logger.debug("[%s] Output of %s :\n%s",self.backup_name,cmd,output) if error_code: raise Exception('Aborting, Not null exit code (%i) for "%s"' % (error_code,cmd)) - + # get the file stats['status']='SFTP' filepath = backup_file + '.gz' @@ -109,15 +110,15 @@ class backup_sqlserver(backup_generic): sftp = paramiko.SFTPClient.from_transport(transport) sftp.get(filepath, localpath) sftp.close() - + if not self.dry_run: stats['total_files_count']=1 stats['written_files_count']=1 stats['total_bytes']=os.stat(localpath).st_size - stats['written_bytes']=os.stat(localpath).st_size + stats['written_bytes']=os.stat(localpath).st_size stats['log']='gzip dump of DB %s:%s (%d bytes) to %s' % (self.server_name,self.db_name, stats['written_bytes'], localpath) stats['backup_location'] = localpath - + finally: stats['status']='RMTemp' cmd = 'rm -f "%s" "%s"' % ( backup_file + '.gz', backup_file ) @@ -126,10 +127,10 @@ class backup_sqlserver(backup_generic): (error_code,output) = ssh_exec(cmd,ssh=ssh) self.logger.debug("[%s] Output of %s :\n%s",self.backup_name,cmd,output) if error_code: - raise Exception('Aborting, Not null exit code (%i) for "%s"' % (error_code,cmd)) - + raise Exception('Aborting, Not null exit code (%i) for "%s"' % (error_code,cmd)) + + - stats['status']='OK' def register_existingbackups(self): @@ -139,7 +140,7 @@ class backup_sqlserver(backup_generic): filelist = os.listdir(self.backup_dir) filelist.sort() - p = re.compile('^%s-(?P\d{8,8}-\d{2,2}h\d{2,2}m\d{2,2}).bak.gz$' % self.db_name) + p = re.compile('^%s-(?P\d{8,8}-\d{2,2}h\d{2,2}m\d{2,2}).bak.gz$' % self.db_name) for item in filelist: sr = p.match(item) if sr: @@ -149,7 +150,7 @@ class backup_sqlserver(backup_generic): self.logger.info('Registering %s from %s',file_name,fileisodate(file_name)) size_bytes = int(os.popen('du -sb "%s"' % file_name).read().split('\t')[0]) self.logger.debug(' Size in bytes : %i',size_bytes) - if not self.dry_run: + if not self.dry_run: self.dbstat.add(self.backup_name,self.server_name,'',\ backup_start=start,backup_end=fileisodate(file_name),status='OK',total_bytes=size_bytes,backup_location=file_name) else: diff --git a/libtisbackup/common.py b/libtisbackup/common.py index e662f4e..0357505 100644 --- a/libtisbackup/common.py +++ b/libtisbackup/common.py @@ -476,7 +476,8 @@ def ssh_exec(command,ssh=None,server_name='',remote_user='',private_key='',ssh_p try: mykey = paramiko.RSAKey.from_private_key_file(private_key) except paramiko.SSHException: - mykey = paramiko.DSSKey.from_private_key_file(private_key) + #mykey = paramiko.DSSKey.from_private_key_file(private_key) + mykey = paramiko.Ed25519Key.from_private_key_file(self.private_key) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())