Exemple #1
0
    def copy(source_file, save_dir):
        """复制文件/文件夹
        """
        try:
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            basename = os.path.basename(source_file)
            if os.path.isfile(source_file):
                file_name = os.path.splitext(basename)[0]
                file_ext = os.path.splitext(basename)[1]
                save_filepath = os.path.join(save_dir, basename)
                if os.path.exists(save_filepath):
                    save_filepath = os.path.join(
                        save_dir, file_name + '_' +
                        StringHelper.get_random_num() + file_ext)
                shutil.copy(source_file, save_filepath)
            elif os.path.isdir(source_file):
                save_dir_path = os.path.join(save_dir, basename)
                if os.path.exists(save_dir_path):
                    save_dir_path = os.path.join(
                        save_dir,
                        basename + '_' + StringHelper.get_random_num())
                shutil.copytree(source_file, save_dir_path)
            else:
                return False, source_file + ' 不是目录也不是文件'

            return True, ''
        except Exception as e:
            return False, str(e)
Exemple #2
0
 def create_archive_bulk(files, archive_type):
     """创建多个存档
     返回 flag,msg   flag:操作是否成功 msg成功则为存档文件路径,失败则为错误信息
     """
     msg = ''
     flag = True
     save_file_name = StringHelper.get_random_num() + '多个文件'
     is_gztar = False
     if archive_type == 'gztar':  #gztar无法设置为追加 所以先打包为tar然后在打包为gztar
         is_gztar = True
         archive_type = 'tar'
     for file in files:
         _flag, _msg = FileHelper.create_archive(
             file, archive_type, save_file_name=save_file_name,
             mode='a')  #设置为追加
         flag = _flag and flag
         if _msg != '':
             if msg == '':
                 msg = _msg
             else:
                 msg = msg + ',' + _msg
     if is_gztar:
         tarfilepath = msg.split(',')[0]
         _flag, _msg = FileHelper.create_archive(tarfilepath, 'gztar')
         FileHelper.delete(tarfilepath)
         flag = _flag and flag
         msg = msg + ',' + _msg
     return flag, msg
Exemple #3
0
    def backup(params,
               taskName,
               tmp_dir_base='./temp',
               archivePath='./archive'):
        """备份方法
        params:{u:root,p:123456,databases:db1}
        """
        _temp_dir = ''.join(
            random.sample(string.ascii_letters + string.digits, 8))
        tmp_dir = os.path.join(tmp_dir_base, _temp_dir)
        os.makedirs(tmp_dir)

        cmd = 'mongodump '
        for (k, v) in params.items():
            cmd += ('--' if len(k) > 1 else '-')
            cmd += f'{k}={v} '

        cmd += f' --out={tmp_dir}'
        logger.info(f'start exec backup cmd: {cmd}')
        archive_type = 'zip'
        status = 0
        proc = subprocess.Popen(cmd,
                                shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        while True:
            buff = proc.stdout.readline()
            logger.debug(buff)
            if proc.poll() is not None:
                break

        status = proc.returncode
        if status != 0:
            logger.info(f'backup failed,cmd: {cmd}')
            return None
        # 获得db_filepath下的子目录,作为数据库名称
        zip_file = os.path.join(archivePath, taskName,
                                StringHelper.get_datestr())
        zip_file = shutil.make_archive(zip_file, archive_type, tmp_dir)

        # 删除原始目录
        shutil.rmtree(tmp_dir)
        return zip_file