how to backup a django db

前端 未结 2 1717
再見小時候
再見小時候 2021-01-30 12:53

I have a Django application that uses a Postgres database. I need to be able to backup and restore the db, both to ensure no data is lost and to be able to copy data from the p

2条回答
  •  深忆病人
    2021-01-30 13:29

    The problem with options 1-3 are that media files (anything uploaded through FileField) are not included in the backup. It is possible to separately backup the directory containing the media files. However, because Django doesn't remove files when they are no longer referenced by a FileField, you will inevitably end up with files in the backup that don't need to be there.

    That's why I would go with option #4. In particular, I recommend django-archive*. Some of its features include:

    • Dumps the contents of all important models (by default ContentType, Permission, and Session are excluded since they are populated by manage.py migrate) and lets you choose additional models to exclude.

    • Includes media files referenced by FileField and ImageField fields. Note that only the files referenced by rows in the database are included; files left over by deleted rows are ignored.

    • Produces a single archive containing both the database backup and media files.

    • Provides options for customizing the location where archives should be stored, the filename format, and archive type (gz and bz2).

    Installation is as simple as adding django_archive to INSTALLED_APPS and setting options in settings.py if needed. Once installed, you can immediately create an archive of your entire database (including media files) by running:

    ./manage.py archive
    

    * Disclaimer: I am the author of the package

提交回复
热议问题