zipdata.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:tumanov_castleoaks 作者: Roamdev 项目源码 文件源码
def handle(self, *args, **options):
        date = datetime.now().date()

        backup_name = '{}.zip'.format(date.strftime('%Y_%m_%d'))
        if args:
            backup_dir = os.path.abspath(args[0])
        else:
            backup_dir = settings.BACKUP_ROOT

        if not os.path.exists(backup_dir):
            raise CommandError('output directory does not exists')

        backup_path = os.path.join(backup_dir, backup_name)

        with zipfile.ZipFile(backup_path, 'w') as ziph:
            for root, dirs, files in os.walk(settings.MEDIA_ROOT):
                for file in files:
                    abspath = os.path.abspath(os.path.join(root, file))
                    relpath = os.path.relpath(abspath, settings.MEDIA_ROOT)
                    ziph.write(abspath, os.path.join('media', relpath))

            # db dump
            dump_path = os.path.join(settings.BASE_DIR, 'dump.json')
            call_command('dump', output=dump_path)
            ziph.write(dump_path, 'dump.json')

        os.unlink('dump.json')
        self.stdout.write('backup saved to "%s"' % backup_path)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号