parent
f88c9d1d06
commit
3a6b93c85d
@ -18,21 +18,25 @@
|
|||||||
#
|
#
|
||||||
# -----------------------------------------------------------------------
|
# -----------------------------------------------------------------------
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
import logging
|
|
||||||
import re
|
|
||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
|
from common import *
|
||||||
|
import XenAPI
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
import os.path
|
||||||
|
import os
|
||||||
|
import datetime
|
||||||
|
import select
|
||||||
import urllib
|
import urllib
|
||||||
import socket
|
import socket
|
||||||
import tarfile
|
import tarfile
|
||||||
import hashlib
|
import hashlib
|
||||||
from stat import *
|
from stat import *
|
||||||
import ssl
|
import ssl
|
||||||
|
|
||||||
from common import *
|
if hasattr(ssl, '_create_unverified_context'):
|
||||||
import XenAPI
|
|
||||||
|
|
||||||
if hasattr(ssl, '_create_unverified_context'):
|
|
||||||
ssl._create_default_https_context = ssl._create_unverified_context
|
ssl._create_default_https_context = ssl._create_unverified_context
|
||||||
|
|
||||||
|
|
||||||
@ -49,14 +53,14 @@ class backup_xva(backup_generic):
|
|||||||
reuse_snapshot = "no"
|
reuse_snapshot = "no"
|
||||||
ignore_proxies = "yes"
|
ignore_proxies = "yes"
|
||||||
use_compression = "true"
|
use_compression = "true"
|
||||||
|
|
||||||
if str2bool(ignore_proxies):
|
if str2bool(ignore_proxies) == True:
|
||||||
os.environ['http_proxy']=""
|
os.environ['http_proxy']=""
|
||||||
os.environ['https_proxy']=""
|
os.environ['https_proxy']=""
|
||||||
|
|
||||||
def verify_export_xva(self,filename):
|
def verify_export_xva(self,filename):
|
||||||
self.logger.debug("[%s] Verify xva export integrity",self.server_name)
|
self.logger.debug("[%s] Verify xva export integrity",self.server_name)
|
||||||
tar = tarfile.open(filename)
|
tar = tarfile.open(filename)
|
||||||
members = tar.getmembers()
|
members = tar.getmembers()
|
||||||
for tarinfo in members:
|
for tarinfo in members:
|
||||||
if re.search('^[0-9]*$',os.path.basename(tarinfo.name)):
|
if re.search('^[0-9]*$',os.path.basename(tarinfo.name)):
|
||||||
@ -80,8 +84,8 @@ class backup_xva(backup_generic):
|
|||||||
session = XenAPI.Session('https://'+xcphost)
|
session = XenAPI.Session('https://'+xcphost)
|
||||||
session.login_with_password(user_xen,password_xen)
|
session.login_with_password(user_xen,password_xen)
|
||||||
|
|
||||||
if not session.xenapi.VM.get_by_name_label(vdi_name):
|
if not session.xenapi.VM.get_by_name_label(vdi_name):
|
||||||
return "bad VM name: %s" % vdi_name
|
return "bad VM name: %s" % vdi_name
|
||||||
|
|
||||||
vm = session.xenapi.VM.get_by_name_label(vdi_name)[0]
|
vm = session.xenapi.VM.get_by_name_label(vdi_name)[0]
|
||||||
status_vm = session.xenapi.VM.get_power_state(vm)
|
status_vm = session.xenapi.VM.get_power_state(vm)
|
||||||
@ -95,7 +99,7 @@ class backup_xva(backup_generic):
|
|||||||
|
|
||||||
|
|
||||||
#add snapshot option
|
#add snapshot option
|
||||||
if not str2bool(halt_vm):
|
if str2bool(halt_vm) == False:
|
||||||
self.logger.debug("[%s] Check if previous tisbackups snapshots exist",vdi_name)
|
self.logger.debug("[%s] Check if previous tisbackups snapshots exist",vdi_name)
|
||||||
old_snapshots = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))
|
old_snapshots = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))
|
||||||
self.logger.debug("[%s] Old snaps count %s", vdi_name, len(old_snapshots))
|
self.logger.debug("[%s] Old snaps count %s", vdi_name, len(old_snapshots))
|
||||||
@ -130,26 +134,26 @@ class backup_xva(backup_generic):
|
|||||||
#get snapshot opaqueRef
|
#get snapshot opaqueRef
|
||||||
vm = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))[0]
|
vm = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))[0]
|
||||||
session.xenapi.VM.set_name_description(snapshot,"snapshot created by tisbackup on: %s"%(now.strftime("%Y-%m-%d %H:%M")))
|
session.xenapi.VM.set_name_description(snapshot,"snapshot created by tisbackup on: %s"%(now.strftime("%Y-%m-%d %H:%M")))
|
||||||
else:
|
else:
|
||||||
self.logger.debug("[%s] Status of VM: %s",self.backup_name,status_vm)
|
self.logger.debug("[%s] Status of VM: %s",self.backup_name,status_vm)
|
||||||
if status_vm == "Running":
|
if status_vm == "Running":
|
||||||
self.logger.debug("[%s] Shudown in progress",self.backup_name)
|
self.logger.debug("[%s] Shudown in progress",self.backup_name)
|
||||||
if dry_run:
|
if dry_run:
|
||||||
print "session.xenapi.VM.clean_shutdown(vm)"
|
print "session.xenapi.VM.clean_shutdown(vm)"
|
||||||
else:
|
else:
|
||||||
session.xenapi.VM.clean_shutdown(vm)
|
session.xenapi.VM.clean_shutdown(vm)
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
filename_temp = filename+".tmp"
|
filename_temp = filename+".tmp"
|
||||||
self.logger.debug("[%s] Copy in progress",self.backup_name)
|
self.logger.debug("[%s] Copy in progress",self.backup_name)
|
||||||
if not str2bool(self.use_compression):
|
if not str2bool(use_compression):
|
||||||
socket.setdefaulttimeout(120)
|
socket.setdefaulttimeout(120)
|
||||||
|
|
||||||
scheme = "http://"
|
scheme = "http://"
|
||||||
if str2bool(enable_https):
|
if str2bool(enable_https) == True:
|
||||||
scheme = "https://"
|
scheme = "https://"
|
||||||
url = scheme+user_xen+":"+password_xen+"@"+self.xcphost+"/export?use_compression="+self.use_compression+"&uuid="+session.xenapi.VM.get_uuid(vm)
|
url = scheme+user_xen+":"+password_xen+"@"+self.xcphost+"/export?use_compression="+self.use_compression+"&uuid="+session.xenapi.VM.get_uuid(vm)
|
||||||
|
|
||||||
urllib.urlretrieve(url, filename_temp)
|
urllib.urlretrieve(url, filename_temp)
|
||||||
urllib.urlcleanup()
|
urllib.urlcleanup()
|
||||||
|
|
||||||
@ -160,7 +164,7 @@ class backup_xva(backup_generic):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
if not str2bool(halt_vm):
|
if str2bool(halt_vm) == False:
|
||||||
self.logger.debug("[%s] Destroy snapshot",'tisbackup-%s'%(vdi_name))
|
self.logger.debug("[%s] Destroy snapshot",'tisbackup-%s'%(vdi_name))
|
||||||
try:
|
try:
|
||||||
for vbd in session.xenapi.VM.get_VBDs(snapshot):
|
for vbd in session.xenapi.VM.get_VBDs(snapshot):
|
||||||
@ -169,10 +173,10 @@ class backup_xva(backup_generic):
|
|||||||
else:
|
else:
|
||||||
vdi = session.xenapi.VBD.get_VDI(vbd)
|
vdi = session.xenapi.VBD.get_VDI(vbd)
|
||||||
if not 'NULL' in vdi:
|
if not 'NULL' in vdi:
|
||||||
session.xenapi.VDI.destroy(vdi)
|
session.xenapi.VDI.destroy(vdi)
|
||||||
session.xenapi.VM.destroy(snapshot)
|
session.xenapi.VM.destroy(snapshot)
|
||||||
except XenAPI.Failure, error:
|
except XenAPI.Failure, error:
|
||||||
return("error when destroy snapshot %s"%(error))
|
return("error when destroy snapshot %s"%(error))
|
||||||
|
|
||||||
elif status_vm == "Running":
|
elif status_vm == "Running":
|
||||||
self.logger.debug("[%s] Starting in progress",self.backup_name)
|
self.logger.debug("[%s] Starting in progress",self.backup_name)
|
||||||
@ -186,7 +190,7 @@ class backup_xva(backup_generic):
|
|||||||
if os.path.exists(filename_temp):
|
if os.path.exists(filename_temp):
|
||||||
tar = tarfile.open(filename_temp)
|
tar = tarfile.open(filename_temp)
|
||||||
if not tar.getnames():
|
if not tar.getnames():
|
||||||
os.unlink(filename_temp)
|
unlink(filename_temp)
|
||||||
return("Tar error")
|
return("Tar error")
|
||||||
tar.close()
|
tar.close()
|
||||||
if str2bool(self.verify_export):
|
if str2bool(self.verify_export):
|
||||||
@ -202,7 +206,7 @@ class backup_xva(backup_generic):
|
|||||||
try:
|
try:
|
||||||
dest_filename = os.path.join(self.backup_dir,"%s-%s.%s" % (self.backup_name,self.backup_start_date,'xva'))
|
dest_filename = os.path.join(self.backup_dir,"%s-%s.%s" % (self.backup_name,self.backup_start_date,'xva'))
|
||||||
|
|
||||||
options = []
|
options = []
|
||||||
options_params = " ".join(options)
|
options_params = " ".join(options)
|
||||||
cmd = self.export_xva( vdi_name= self.server_name,filename= dest_filename, halt_vm= self.halt_vm, enable_https=self.enable_https, dry_run= self.dry_run, reuse_snapshot=self.reuse_snapshot)
|
cmd = self.export_xva( vdi_name= self.server_name,filename= dest_filename, halt_vm= self.halt_vm, enable_https=self.enable_https, dry_run= self.dry_run, reuse_snapshot=self.reuse_snapshot)
|
||||||
if os.path.exists(dest_filename):
|
if os.path.exists(dest_filename):
|
||||||
@ -216,7 +220,7 @@ class backup_xva(backup_generic):
|
|||||||
stats['backup_location'] = dest_filename
|
stats['backup_location'] = dest_filename
|
||||||
if cmd == 0:
|
if cmd == 0:
|
||||||
stats['log']='XVA backup from %s OK, %d bytes written' % (self.server_name,stats['written_bytes'])
|
stats['log']='XVA backup from %s OK, %d bytes written' % (self.server_name,stats['written_bytes'])
|
||||||
stats['status']='OK'
|
stats['status']='OK'
|
||||||
else:
|
else:
|
||||||
raise Exception(cmd)
|
raise Exception(cmd)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user