Django – selective restore of DB dump

If you are running a Django application, hopefully you have regular backups of the DB.  We had a customer who realized they deleted something over a month ago and needed the deleted objects restored.  Instead of just reverting the entire DB and losing all of the new information, I wrote a simple script that will create a fixture of everything that was deleted that can then be used with loaddata.

Thanks goes to Constantin Berzan for how to programmatically enumerate objects that will get cascade-deleteded in Django.

Step By Step

  1. In a local development environment restore the DB to the version with the objects you need to restore into production.
  2. Run  python manage.py shell
  3. In the shell type  execfile('dump_partial.py') where ‘dump_partial.py’ is what you named your customized version of the script below.
  4. On the production environment, place the dump.json file generated in step 3 in the same directory as manage.py and run python manage.py loaddata dump.json

dump_partial.py

Save this file in the same directory as your Django manage.py on your local development environment.  Update what you need to dump as needed

 

 

Travis Payton
Follow Me

  1 comment for “Django – selective restore of DB dump

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: