博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django使用manage.py备份与恢复数据
阅读量:6003 次
发布时间:2019-06-20

本文共 2320 字,大约阅读时间需要 7 分钟。

Django dumpdata and loaddata

dumpdata command

  • It is a django management command, which can be use to backup(export) you model instances or whole database

dumpdata for basic database dump

  • Following command will dump whole database in to a db.json file
./manage.py dumpdata > db.json

dumpdata for backup specific app

  • Following command will dump the content in django admin app into admin.json file
./manage.py dumpdata admin > admin.json

dumpdata for backup specific table

  • Following command will dump only the content in django admin.logentry table
./manage.py dumpdata admin.logentry > logentry.json
  • Following command will dump the content in django auth.user table
./manage.py dumpdata auth.user > user.json

dumpdata (--exclude)

  • You can use --exclude option to specify apps/tables which don't need being dumped

  • Following command will dump the whole database with out including auth.permission table content

./manage.py dumpdata --exclude auth.permission > db.json

dumpdata (--indent)

  • By default, dumpdata will output all data on a single line. It isn’t easy for humans to read

  • You can use the --indent option to pretty-print the output with a number of indentation spaces

./manage.py dumpdata auth.user --indent 2 > user.json
  • Example output of above command is below

Picture

dumpdata (--format)

  • By default, dumpdata will format its output in JSON

  • You can specify the format using --format option

  • Command supports for following formats(serialization formats)

  1. json
  2. xml
  3. yaml
./manage.py dumpdata auth.user --indent 2 --format xml > user.xml
  • Above command output an xml file(user.xml)

Picture

loaddata command

  • This command can be use to load the fixtures(database dumps) into database
./manage.py loaddata user.json
  • This command will add the user.json file content into the database

Restore fresh database

  • When you backup whole database by using dumpdata command, it will backup all the database tables

  • If you use this database dump to load the fresh database(in another django project), it can be causes IntegrityError (If you loaddata in same database it works fine)

  • To fix this problem, make sure to backup the database by excluding contenttypes and auth.permissions tables

./manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
  • Now you can use loaddata command with a fresh database
./manage.py loaddata db.json

 

 

转载地址:http://zwdmx.baihongyu.com/

你可能感兴趣的文章
遍历form表单里面的表单元素,取其value
查看>>
PHP TP框架基础
查看>>
directive ngChecked
查看>>
面试110道题
查看>>
python 08 文件操作
查看>>
强势解决:windows 不能在本地计算机中起动Tomcat参考特定错误代码1
查看>>
Gradle 配置debug和release工程目录
查看>>
spring mvc处理ios 请求头不全时空参 无法解析的问题处理
查看>>
SpringBoot RabbitMq集成
查看>>
使用webmagic构建一个分布式的爬虫
查看>>
c运算符和优先级
查看>>
TODO:一不顺眼就换字体Go之代码篇
查看>>
Linux设备驱动程序编写
查看>>
curl指令的使用
查看>>
为什么使用xfs
查看>>
THINKPHP 结合阿里大于发送短信
查看>>
网站故障排查常用命令
查看>>
Python setdaemon守护进程
查看>>
ubuntu10.04下安装LAMP
查看>>
sendmail+tls+java
查看>>