Beispiel #1
0
    def backup_path_to(self,spath,dfile,exclude = [],siteName = None):
        if not os.path.exists(spath):
            self.echo_error('The specified directory {} does not exist!'.format(spath))
            return False

        if spath[-1] == '/':
            spath = spath[:-1]

        dirname = os.path.basename(spath)
        dpath = os.path.dirname(dfile)
        if not os.path.exists(dpath):
            os.makedirs(dpath,384)
        
        p_size = public.get_path_size(spath)
        self.get_exclude(exclude)
        exclude_config = self._exclude
        if not self._exclude:
            exclude_config = "Not set"
        
        if siteName:
            self.echo_info('Backup site: {}'.format(siteName))
            self.echo_info('Website root directory: {}'.format(spath))
        else:
            self.echo_info('Backup directory: {}'.format(spath))
        
        self.echo_info("Directory size: {}".format(public.to_size(p_size)))
        self.echo_info('Exclusion setting: {}'.format(exclude_config))
        disk_path,disk_free,disk_inode = self.get_disk_free(dfile)
        self.echo_info("Partition {} available disk space is: {}, available Inode is: {}".format(disk_path,public.to_size(disk_free),disk_inode))
        if disk_path:
            if disk_free < p_size:
                self.echo_error("The available disk space of the target partition is less than {}, and the backup cannot be completed. Please increase the disk capacity or change the default backup directory on the settings page!".format(public.to_size(p_size)))
                return False

            if disk_inode < self._inode_min:
                self.echo_error("The available Inode of the target partition is less than {}, and the backup cannot be completed. Please increase the disk capacity or change the default backup directory on the settings page!".format(self._inode_min))
                return False

        stime = time.time()
        self.echo_info("Start compressing files: {}".format(public.format_date(times=stime)))
        if os.path.exists(dfile):
            os.remove(dfile)
        public.ExecShell("cd " + os.path.dirname(spath) + " && tar zcvf '" + dfile + "' " + self._exclude + " '" + dirname + "' 2>{err_log} 1> /dev/null".format(err_log = self._err_log))
        tar_size = os.path.getsize(dfile)
        if tar_size < 1:
            self.echo_error("Data compression failed")
            self.echo_info(public.readFile(self._err_log))
            return False
        self.echo_info("File compression completed, took {:.2f} seconds, compressed package size: {}".format(time.time() - stime,public.to_size(tar_size)))
        if siteName:
            self.echo_info("Site backed up to: {}".format(dfile))
        else:
            self.echo_info("Directory has been backed up to: {}".format(dfile))
        if os.path.exists(self._err_log):
            os.remove(self._err_log)
        return dfile
Beispiel #2
0
 def get_path_size(self, get):
     if sys.version_info[0] == 2: get.path = get.path.encode('utf-8')
     data = {}
     data['path'] = get.path
     data['size'] = public.get_path_size(get.path)
     return data
Beispiel #3
0
    def backup_path_to(self,spath,dfile,exclude = [],siteName = None):
        if not os.path.exists(spath):
            self.echo_error(public.getMsg('BACKUP_DIR_NOT_EXIST',(spath,)))
            return False

        if spath[-1] == '/':
            spath = spath[:-1]

        dirname = os.path.basename(spath)
        dpath = os.path.dirname(dfile)
        if not os.path.exists(dpath):
            os.makedirs(dpath,384)

        self.get_exclude(exclude)
        exclude_config = self._exclude
        exclude_list = self.get_exclude_list(exclude)
        p_size = public.get_path_size(spath, exclude=exclude_list)
        if not self._exclude:
            exclude_config = "Not set"
        
        if siteName:
            self.echo_info(public.getMsg('BACKUP_SITE',(siteName,)))
            self.echo_info(public.getMsg('WEBSITE_DIR',(spath,)))
        else:
            self.echo_info(public.getMsg('BACKUP_DIR',(spath,)))
        
        self.echo_info(public.getMsg(
            "DIR_SIZE",
            (str(public.to_size(p_size),))
        ))
        self.echo_info(public.getMsg('BACKUP_EXCLUSION',(exclude_config,)))
        disk_path,disk_free,disk_inode = self.get_disk_free(dfile)
        self.echo_info(public.getMsg(
            "PARTITION_INFO",
            (disk_path,str(public.to_size(disk_free)),str(disk_inode))
        ))
        if disk_path:
            if disk_free < p_size:
                self.echo_error(public.getMsg(
                    "PARTITION_LESS_THEN",
                    (str(public.to_size(p_size)),)
                ))
                return False

            if disk_inode < self._inode_min:
                self.echo_error(public.getMsg(
                    "INODE_LESS_THEN",
                    (str(self._inode_min,))
                ))
                return False

        stime = time.time()
        self.echo_info(public.getMsg("START_COMPRESS",(public.format_date(times=stime),)))
        if os.path.exists(dfile):
            os.remove(dfile)
        public.ExecShell("cd " + os.path.dirname(spath) + " && tar zcvf '" + dfile + "' " + self._exclude + " '" + dirname + "' 2>{err_log} 1> /dev/null".format(err_log = self._err_log))
        tar_size = os.path.getsize(dfile)
        if tar_size < 1:
            self.echo_error(public.getMsg('ZIP_ERR'))
            self.echo_info(public.readFile(self._err_log))
            return False
        compression_time = str('{:.2f}'.format(time.time() - stime))
        self.echo_info(public.getMsg(
            'COMPRESS_TIME',
            (compression_time,str(public.to_size(tar_size)))
        ))
        if siteName:
            self.echo_info(public.getMsg("WEBSITE_BACKUP_TO",(dfile,)))
        else:
            self.echo_info(public.getMsg("DIR_BACKUP_TO",(dfile,)))
        if os.path.exists(self._err_log):
            os.remove(self._err_log)
        return dfile
Beispiel #4
0
    def backup_path_to(self, spath, dfile, exclude=[], siteName=None):
        if not os.path.exists(spath):
            self.echo_error('指定目录{}不存在!'.format(spath))
            return False

        if spath[-1] == '/':
            spath = spath[:-1]

        dirname = os.path.basename(spath)
        dpath = os.path.dirname(dfile)
        if not os.path.exists(dpath):
            os.makedirs(dpath, 384)

        p_size = public.get_path_size(spath)
        self.get_exclude(exclude)
        exclude_config = self._exclude
        if not self._exclude:
            exclude_config = "未设置"

        if siteName:
            self.echo_info('备份网站:{}'.format(siteName))
            self.echo_info('网站根目录:{}'.format(spath))
        else:
            self.echo_info('备份目录:{}'.format(spath))

        self.echo_info("目录大小:{}".format(public.to_size(p_size)))
        self.echo_info('排除设置:{}'.format(exclude_config))
        disk_path, disk_free, disk_inode = self.get_disk_free(dfile)
        self.echo_info("分区{}可用磁盘空间为:{},可用Inode为:{}".format(
            disk_path, public.to_size(disk_free), disk_inode))
        if disk_path:
            if disk_free < p_size:
                self.echo_error(
                    "目标分区可用的磁盘空间小于{},无法完成备份,请增加磁盘容量,或在设置页面更改默认备份目录!".format(
                        public.to_size(p_size)))
                return False

            if disk_inode < self._inode_min:
                self.echo_error(
                    "目标分区可用的Inode小于{},无法完成备份,请增加磁盘容量,或在设置页面更改默认备份目录!".format(
                        self._inode_min))
                return False

        stime = time.time()
        self.echo_info("开始压缩文件:{}".format(public.format_date(times=stime)))
        if os.path.exists(dfile):
            os.remove(dfile)
        public.ExecShell("cd " + os.path.dirname(spath) + " && tar zcvf '" +
                         dfile + "' " + self._exclude + " '" + dirname +
                         "' 2>{err_log} 1> /dev/null".format(
                             err_log=self._err_log))
        tar_size = os.path.getsize(dfile)
        if tar_size < 1:
            self.echo_error("数据压缩失败")
            self.echo_info(public.readFile(self._err_log))
            return False
        self.echo_info("文件压缩完成,耗时{:.2f}秒,压缩包大小:{}".format(
            time.time() - stime, public.to_size(tar_size)))
        if siteName:
            self.echo_info("网站已备份到:{}".format(dfile))
        else:
            self.echo_info("目录已备份到:{}".format(dfile))
        if os.path.exists(self._err_log):
            os.remove(self._err_log)
        return dfile