Advertisement
Guest User

Untitled

a guest
Nov 12th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. def lvm_snap(backup_opt_dict):
  2.     """
  3.    Checks the provided parameters and create the lvm snapshot if requested
  4.  
  5.    The path_to_backup might be adjusted in case the user requested
  6.    a lvm snapshot without specifying an exact path for the snapshot
  7.    (lvm_auto_snap).
  8.    The assumption in this case is that the user wants to use the lvm snapshot
  9.    capability to backup the specified filesystem path, leaving out all
  10.    the rest of the parameters which will guessed and set by freezer.
  11.  
  12.    if a snapshot is requested using the --snapshot flag, but lvm_auto_snap
  13.    is not provided, then path_to_backup is supposed to be the path to backup
  14.    *before* any information about the snapshot is added and will be
  15.    adjusted.
  16.  
  17.    :param backup_opt_dict: the configuration dict
  18.    :return: True if the snapshot has been taken, False otherwise
  19.    """
  20.     if backup_opt_dict.snapshot:
  21.         if not backup_opt_dict.lvm_auto_snap:
  22.             # 1) the provided path_to_backup has the meaning of
  23.             #    the lvm_auto_snap and is therefore copied into it
  24.             # 2) the correct value of path_to_backup, which takes into
  25.             #    consideration the snapshot mount-point, is cleared
  26.             #    and will be calculated by freezer
  27.             backup_opt_dict.lvm_auto_snap =\
  28.                 backup_opt_dict.path_to_backup
  29.             backup_opt_dict.path_to_backup = ''
  30.  
  31.     if backup_opt_dict.lvm_auto_snap:
  32.         # adjust/check lvm parameters according to provided lvm_auto_snap
  33.         lvm_info = get_lvm_info(backup_opt_dict.lvm_auto_snap)
  34.  
  35.         if not backup_opt_dict.lvm_volgroup:
  36.             backup_opt_dict.lvm_volgroup = lvm_info['volgroup']
  37.  
  38.         if not backup_opt_dict.lvm_srcvol:
  39.             backup_opt_dict.lvm_srcvol = lvm_info['srcvol']
  40.  
  41.         path_to_backup = os.path.join(backup_opt_dict.lvm_dirmount,
  42.                                       lvm_info['snap_path'])
  43.         if backup_opt_dict.path_to_backup:
  44.             # path_to_backup is user-provided, check if consistent
  45.             if backup_opt_dict.path_to_backup != path_to_backup:
  46.                 raise Exception('Path to backup mismatch. '
  47.                                 'provided: {0}, should be LVM-mounted: {1}'.
  48.                                 format(backup_opt_dict.path_to_backup,
  49.                                        path_to_backup))
  50.         else:
  51.             # path_to_backup not provided: use the one calculated above
  52.             backup_opt_dict.path_to_backup = path_to_backup
  53.  
  54.     if not validate_lvm_params(backup_opt_dict):
  55.         logging.info('[*] No LVM requested/configured')
  56.         return False
  57.  
  58.     utils.create_dir(backup_opt_dict.lvm_dirmount)
  59.  
  60.     lvm_create_command = (
  61.         '{0} --size {1} --snapshot --permission {2} '
  62.         '--name {3} {4}'.format(
  63.             utils.find_executable('lvcreate'),
  64.             backup_opt_dict.lvm_snapsize,
  65.             ('r' if backup_opt_dict.lvm_snapperm == 'ro'
  66.              else backup_opt_dict.lvm_snapperm),
  67.             backup_opt_dict.lvm_snapname,
  68.             backup_opt_dict.lvm_srcvol))
  69.  
  70.     # If backup mode is mysql, then the db will be flushed and read locked
  71.     # before the creation of the lvm snap
  72.     if backup_opt_dict.mode == 'mysql':
  73.         cursor = backup_opt_dict.mysql_db_inst.cursor()
  74.         cursor.execute('FLUSH TABLES WITH READ LOCK')
  75.         backup_opt_dict.mysql_db_inst.commit()
  76.  
  77.     lvm_process = subprocess.Popen(
  78.         lvm_create_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  79.         stderr=subprocess.PIPE, shell=True,
  80.         executable=utils.find_executable('bash'))
  81.     (lvm_out, lvm_err) = lvm_process.communicate()
  82.  
  83.     # Unlock MySQL Tables if backup is == mysql
  84.     # regardless of the snapshot being taken or not
  85.     if backup_opt_dict.mode == 'mysql':
  86.         cursor.execute('UNLOCK TABLES')
  87.         backup_opt_dict.mysql_db_inst.commit()
  88.         cursor.close()
  89.         backup_opt_dict.mysql_db_inst.close()
  90.  
  91.     if lvm_process.returncode:
  92.         raise Exception('lvm snapshot creation error: {0}'.format(lvm_err))
  93.  
  94.     logging.debug('[*] {0}'.format(lvm_out))
  95.     logging.warning('[*] Logical volume "{0}" created'.
  96.                     format(backup_opt_dict.lvm_snapname))
  97.  
  98.     # Guess the file system of the provided source volume and st mount
  99.     # options accordingly
  100.     filesys_type = utils.get_vol_fs_type(backup_opt_dict.lvm_srcvol)
  101.     mount_options = '-o {}'.format(backup_opt_dict.lvm_snapperm)
  102.     if 'xfs' == filesys_type:
  103.         mount_options = ' -onouuid '
  104.     # Mount the newly created snapshot to dir_mount
  105.     abs_snap_name = '/dev/{0}/{1}'.format(
  106.         backup_opt_dict.lvm_volgroup,
  107.         backup_opt_dict.lvm_snapname)
  108.     mount_command = '{0} {1} {2} {3}'.format(
  109.         utils.find_executable('mount'),
  110.         mount_options,
  111.         abs_snap_name,
  112.         backup_opt_dict.lvm_dirmount)
  113.     mount_process = subprocess.Popen(
  114.         mount_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  115.         stderr=subprocess.PIPE, shell=True,
  116.         executable=utils.find_executable('bash'))
  117.     mount_err = mount_process.communicate()[1]
  118.     if 'already mounted' in mount_err:
  119.         logging.warning('[*] Volume {0} already mounted on {1}\
  120.        '.format(abs_snap_name, backup_opt_dict.lvm_dirmount))
  121.         return True
  122.     if mount_err:
  123.         logging.error("[*] Snapshot mount error. Removing snapshot")
  124.         lvm_snap_remove(backup_opt_dict)
  125.         raise Exception('lvm snapshot mounting error: {0}'.format(mount_err))
  126.     else:
  127.         logging.warning(
  128.             '[*] Volume {0} succesfully mounted on {1}'.format(
  129.                 abs_snap_name, backup_opt_dict.lvm_dirmount))
  130.  
  131.     return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement