Django backup strategy with dumps and migrations

As with this question , I set up a backup system based on dumpdatafor my database. This setting is akin to running a cron script that invokes dumpdataand moves the backup to a remote server, for easy use loaddatato restore the database. However, I'm not sure if this works well with migrations . loaddatanow has a switch ignorenonexistentto handle remote models / fields, but it cannot allow cases where columns have been added with one-time default values ​​or apply code RunPython.

As I see, there are two problems:

  • Mark each output file with the dumpdatacurrent version of each application
  • Combining fixtures into a migration path

I'm at a dead end on how to solve the first problem without introducing tons of overhead. Would it be enough to save an additional file to a backup containing the mapping {app_name: migration_number}?

The second problem, which I think is easier when the first is solved, as the process is approximately equal:

  • Create a new database
  • Starting migration forward to the appropriate point for each application
  • Call loaddatawith this fixture file
  • Run the rest of the migration

There is some code in this question (related to the error message), which, I think, can be adapted for this purpose.

Since these are fairly regular / large database snapshots, I don’t want to save them as a data transfer cluttering up the migration directory.

+5
1

, postgresql :

, , manage.py makemigrations .

, . , - . , .., .

, -as , before- , . :

  • .
  • .

. , .

, ( require.txt), , -of course- git .

  1. dumpdata , . dumpdata , , :

    ./manage.py dumpdata --exclude auth.permission --exclude contenttypes  --exclude admin.LogEntry --exclude sessions --indent 2 > db.json
    
  2. pg_dump :

    pg_dump -U $user -Fc $database --exclude-table=django_migrations > path/to/backup-dir/db.dump
    
  3. , .

    migrations , :

    #!/bin/bash
    for dir in $(find -L -name "migrations")
    do
      rm -Rf $dir/*
    done
    
  4. :

    , bash :

    su -l postgres -c "PGPASSWORD=$password psql -c 'drop database $database ;'"
    su -l postgres -c "createdb --owner $username $database"
    su -l postgres -c "PGPASSWORD=$password psql $database -U $username -c 'CREATE EXTENSION $extension ;'"
    
  5. :

    pg_restore -Fc -U $username -d $database path/to/backup-dir/db.dump
    
  6. 3, :

    ./manage.py makemigrations <app1> <app2> ... <appn>
    

    ... :

    #!/bin/bash
    apps=()
    for app in $(find ./ -maxdepth 1 -type d ! -path "./<project-folder> ! -path "./.*" ! -path "./")
    do
      apps+=(${app#??})
    done
    all_apps=$(printf "%s "  "${apps[@]}")
    
    ./manage.py makemigrations $all_apps
    
  7. :

    ./manage.py migrate --fake
    

, - , *** ( ), , . db.json , :

pg_dump pg_restore

:

  • 3 ( )
  • 4 ( )
  • 6 (makemigrations)

:

  • :

    ./manage.py migrate
    
  • db.json:

    ./manage.py loaddata path/to/db.json
    

, .

, .

, pg_dump pg_restore , .

+5

Source: https://habr.com/ru/post/1624530/


All Articles