예제 #1
0
def get_logical_disk(bkpid, diskid):
    dev = "None"
    b = True
    urlget = "vms/" + bkpid + "/diskattachments/"
    serial = diskid[0:20]
    cmd = "grep -Rlw '" + serial + "' /sys/block/*/serial|awk -F '/' '{print $4}'"
    while b:
        # Trying to  identify VIRTIO devices
        try:
            path = subprocess.check_output(cmd, shell=True).replace("\n", "")
            if path.startswith("vd") or path.startswith("sd"):
                dev = "/dev/" + path
                time.sleep(1)
        except:
            continue
        if str(dev) != "None":
            printf.INFO("Disk found using serial number")
            b = False
        else:
            # Trying to identify devices through API
            disk_service = connection.service(urlget)
            for disk in disk_service.list():
                dev = str(disk.logical_name)
                if disk.id == diskid and str(dev) != "None":
                    printf.INFO("Disk found using API")
                    b = False
                else:
                    dev = "None"
                    time.sleep(1)
    return dev
예제 #2
0
 def backup(self,vmid,snapid,disk_id,bkpvm):
   # Se agrega el disco a la VM que tomara el backup
   
   try: 
     signal.signal(signal.SIGALRM, self.handler_timeout)  
     signal.alarm(self.timeout_detect) 
     printf.INFO("Timeout is defined to %s" % self.timeout_detect)
     printf.INFO("Attach snap disk to bkpvm")
     self.attach_disk(bkpvm,disk_id,snapid)
     # Se obtiene el nombre del dispositivo
     printf.INFO("Identifying disk device, this might take a while")
     dev = self.get_logical_disk(bkpvm,disk_id)
     # Se obtiene el nombre del disco
     diskname = self.get_disk_name(vmid,snapid,disk_id)
     time.sleep(10)
     signal.alarm(0)
     # Se crea la image qcow que seria el  backup como tal
     self.create_image_bkp(dev,diskname)
     # Se desactiva el disco del cual se hizo el backup
     self.deactivate_disk(bkpvm,disk_id)
     time.sleep(10)
     # Se detacha el disco de la BKPVM
     printf.INFO("Dettach snap disk of bkpvm")
     self.detach_disk(bkpvm,disk_id)
   except:
     printf.ERROR("Time Out reached ")
     self.deactivate_disk(bkpvm,disk_id)
     time.sleep(10)
     printf.INFO("Dettach snap disk of bkpvm")
     self.detach_disk(bkpvm,disk_id)
예제 #3
0
def create_snap(idvm):
 vm_service = connection.service("vms")
 snapshots_service = vm_service.vm_service(idvm).snapshots_service()
 snapshots_service.add(types.Snapshot(description=snapname, persist_memorystate=False))
 snapid = get_snap_id(vmid)
 status = get_snap_status(idvm,snapid)
 printf.INFO("Trying to create snapshot of VM: " + idvm)
 while str(status) == "locked":
    time.sleep(10)
    printf.INFO("Waiting until snapshot creation ends")
    status = get_snap_status(idvm,snapid)
 printf.OK("Snapshot created")
예제 #4
0
 def create_snap(self,vmid,snapname,my_disk):
  vm_service = self.connection.service("vms")
  snapshots_service = vm_service.vm_service(vmid).snapshots_service()
  snapshots_service.add(types.Snapshot(description=snapname,persist_memorystate=False, disk_attachments=[ types.DiskAttachment( disk=types.Disk( id=my_disk)) ]))
  snapid = self.get_snap_id(vmid)
  status = self.get_snap_status(vmid,snapid)
  printf.INFO("Trying to create snapshot of VM: " + vmid)
  while str(status) == "locked":
    time.sleep(10)
    printf.INFO("Waiting until snapshot creation ends")
    status = self.get_snap_status(vmid,snapid)
  printf.OK("Snapshot created")
예제 #5
0
 def create_image_bkp(self,dev,diskname):
   bckfiledir = self.bckdir + "/" + self.vmname + "/" + self.date
   mkdir = "mkdir -p " + bckfiledir
   time.sleep(5)
   subprocess.call(mkdir, shell=True) 
   bckfile = bckfiledir + "/" + diskname + ".qcow2"
   printf.INFO("Creating qcow2 file: " + bckfile)
   cmd = "qemu-img convert -U -O qcow2 " + dev + " " +bckfile
   printf.INFO("cmd:" + cmd)
   utils=virtbkp_utils.virtbkp_utils()
   thread.start_new_thread(self.run_qemu_convert,(cmd,))
   utils.progress_bar_qcow(bckfile)
예제 #6
0
def delete_snap(idvm, snapid):
    snap_service = connection.service("vms/" + idvm + "/snapshots/" + snapid)
    snap_service.remove()
    status = get_snap_status(idvm, snapid)
    while str(status) != "None":
        time.sleep(30)
        printf.INFO("Waiting until snapshot deletion ends")
        status = get_snap_status(idvm, snapid)
    printf.OK("Snapshot deleted")
예제 #7
0
def backup(vmid, snapid, disk_id, bkpvm):
    # Se agrega el disco a la VM que tomara el backup
    printf.INFO("Attach snap disk to bkpvm")
    attach_disk(bkpvm, disk_id, snapid)
    # Se obtiene el nombre del dispositivo
    printf.INFO("Identifying disk device, this might take a while")
    dev = get_logical_disk(bkpvm, disk_id)
    # Se obtiene el nombre del disco
    diskname = get_disk_name(vmid, snapid, disk_id)
    # Se crea la image qcow que seria el  backup como tal
    create_image_bkp(dev, diskname)
    # Se desactiva el disco del cual se hizo el backup
    deactivate_disk(bkpvm, disk_id)
    time.sleep(10)
    # Se detacha el disco de la BKPVM
    printf.INFO("Dettach snap disk of bkpvm")
    detach_disk(bkpvm, disk_id)
    time.sleep(10)
예제 #8
0
def create_image_bkp(dev, diskname):
    bckfiledir = bckdir + "/" + vmname + "/" + date
    mkdir = "mkdir -p " + bckfiledir
    subprocess.call(mkdir, shell=True)
    bckfile = bckfiledir + "/" + diskname + ".qcow2"
    printf.INFO("Creating qcow2 file: " + bckfile)
    cmd = "qemu-img convert -O qcow2 " + dev + " " + bckfile
    utils = virtbkp_utils.virtbkp_utils()
    thread.start_new_thread(run_qemu_convert, (cmd, ))
    utils.progress_bar_qcow(bckfile)
예제 #9
0
 def main(self):
     self.start()
     ## Parte Principal
     # Se consigue el ID de la VM a la cual se hara backup y la VM donde se montaran sus discos
     self.vmid = self.get_id_vm(self.vmname)
     self.bkpvm = self.get_id_vm(self.bkpvm)
     # Se crea el snap y se espera un rato para que termine sin problemas y pueda detectar el nombre del disco en VM de backup
     self.create_snap(self.vmid, self.snapname)
     #time.sleep(60)
     # Se obtiene el id del snap
     self.snapid = self.get_snap_id(self.vmid)
     # Se obtiene el ID del disco
     vm_disks = self.snap_disk_id(self.vmid, self.snapid)
     for disk_id in vm_disks:
         printf.INFO("Trying to create a qcow2 file of disk " + disk_id)
         # Backup
         self.backup(self.vmid, self.snapid, disk_id, self.bkpvm)
     printf.INFO("Trying to delete snapshot of " + self.vmname)
     self.delete_snap(self.vmid, self.snapid)
예제 #10
0
def main():
    ## Parte Principal
    # Se consigue el ID de la VM a la cual se hara backup y la VM donde se montaran sus discos
    global vmid
    global vmname
    global bkpvm
    vmid = get_id_vm(vmname)
    bkpvm = get_id_vm(bkpvm)
    # Se crea el snap y se espera un rato para que termine sin problemas y pueda detectar el nombre del disco en VM de backup
    create_snap(vmid)
    #time.sleep(60)
    # Se obtiene el id del snap
    snapid = get_snap_id(vmid)
    # Se obtiene el ID del disco
    vm_disks = snap_disk_id(vmid, snapid)
    for disk_id in vm_disks:
        printf.INFO("Trying to create a qcow2 file of disk " + disk_id)
        # Backup
        backup(vmid, snapid, disk_id, bkpvm)
    printf.INFO("Trying to delete snapshot of " + vmname)
    delete_snap(vmid, snapid)