faire le backup xen-metadata avec une clef ssh plutôt qu'avec le mdp

This commit is contained in:
ssamson-tis 2016-04-14 14:25:39 +02:00
parent c08bbd165b
commit f624d28f28
2 changed files with 41 additions and 39 deletions

View File

@ -20,57 +20,53 @@
import sys
import shutil
import datetime
import base64
import os
from common import * from common import *
import paramiko
class backup_xcp_metadata(backup_generic): class backup_xcp_metadata(backup_generic):
"""Backup metatdata of a xcp pool using xe pool-dump-database""" """Backup metatdata of a xcp pool using xe pool-dump-database"""
type = 'xcp-dump-metadata' type = 'xcp-dump-metadata'
required_params = ['type','server_name','password_file','backup_name'] required_params = ['type','server_name','private_key','backup_name']
optional_params = backup_generic.optional_params + ['ssh_port']
ssh_port = 22
def do_backup(self,stats): def do_backup(self,stats):
self.logger.debug('[%s] Connecting to %s with user root and key %s',self.backup_name,self.server_name,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)
if os.path.isfile('/opt/xensource/bin/xe') == False:
raise Exception('Aborting, /opt/xensource/bin/xe binary not present"')
t = datetime.datetime.now() t = datetime.datetime.now()
backup_start_date = t.strftime('%Y%m%d-%Hh%Mm%S') backup_start_date = t.strftime('%Y%m%d-%Hh%Mm%S')
# dump pool medatadata # dump pool medatadata
localpath = os.path.join(self.backup_dir , 'xcp_metadata-' + backup_start_date + '.dump.gz') localpath = os.path.join(self.backup_dir , 'xcp_metadata-' + backup_start_date + '.dump')
temppath = '/tmp/xcp_metadata-' + backup_start_date + '.dump' try:
mykey = paramiko.RSAKey.from_private_key_file(self.private_key)
except paramiko.SSHException:
mykey = paramiko.DSSKey.from_private_key_file(self.private_key)
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(self.server_name,username='root',pkey = mykey, port=self.ssh_port)
stats['status']='Dumping' stats['status']='Dumping'
if not self.dry_run: if not self.dry_run:
cmd = "/opt/xensource/bin/xe -s %s -pwf %s pool-dump-database file-name=%s" %(self.server_name,self.password_file,temppath) cmd = "/opt/xensource/bin/xe pool-dump-database file-name="
self.logger.debug('[%s] Dump XCP Metadata : %s',self.backup_name,cmd) self.logger.debug('[%s] Dump XCP Metadata : %s', self.backup_name, cmd)
call_external_process(cmd) (error_code, output) = ssh_exec(cmd, ssh=self.ssh)
with open(localpath,"w") as f:
f.write(output)
# zip the file # zip the file
stats['status']='Zipping' stats['status']='Zipping'
cmd = 'gzip %s ' %temppath cmd = 'gzip %s ' % localpath
self.logger.debug('[%s] Compress backup : %s',self.backup_name,cmd) self.logger.debug('[%s] Compress backup : %s',self.backup_name,cmd)
if not self.dry_run: if not self.dry_run:
call_external_process(cmd) call_external_process(cmd)
localpath += ".gz"
# get the file
stats['status']='move to backup directory'
self.logger.debug('[%s] Moving temp backup file %s to backup new path %s',self.backup_name,self.server_name,localpath)
if not self.dry_run:
shutil.move (temppath + '.gz' ,localpath)
if not self.dry_run: if not self.dry_run:
stats['total_files_count']=1 stats['total_files_count']=1
stats['written_files_count']=1 stats['written_files_count']=1

View File

@ -76,18 +76,24 @@ maximum_backup_age=30
;db_user=root ;db_user=root
;db_passwd= ;db_passwd=
;[[backup_sql_srvintranet]] ;[backup_sql_srvintranet]
;[type=sqlserver+ssh] ;type=sqlserver+ssh
;[server_name=srvintranet ] ;server_name=srvintranet
;[private_key=/root/.ssh/id_dsa] ;private_key=/root/.ssh/id_dsa
;[db_name=testbackup] ;db_name=testbackup
;[db_user=sa] ;db_user=sa
;[db_password=xxx] ;db_password=xxx
;[debian-ovf]] ;[debian-ovf]
;[type=esx-vmdk] ;type=esx-vmdk
;[server_name=debian] ;server_name=debian
;[esxhost=192.168.149.213] ;esxhost=192.168.149.213
;[password_file=/etc/.esx_passwd] ;password_file=/etc/.esx_passwd
;[create_ovafile=yes] ;create_ovafile=yes
;[srvxen1-metadata]
;type=xcp-dump-metadata
;server_name=srvxen1
;private_key=/root/.ssh/id_dsa