Rake task to backup and restore database

后端 未结 2 2050
北海茫月
北海茫月 2021-02-04 16:07

I am working on a Rails project, and sometimes I program at home and sometimes at work. In my development process, I add data to the database, and I really need a way to synchro

2条回答
  •  广开言路
    2021-02-04 16:43

    I use a script that dumps the the database to a particular location, and a second that fetches the dump and uses it to restore a specified database. I use the Whenever gem to schedule daily backups (by calling the first script), by putting this in the schedule.rb file:

      every :day, :at => "05:00" do
        command "/var/www/current/script/db_backup.sh -n #{@db_name}"
      end
    

    The exact contents of the script depends on what database you're using. As I'm using postgreSQL, the backup script, after figuring the proper location for the dump, runs pg_dump:

    pg_dump -F t -U username -f file_location.dat database_name
    

    And the 'restore' script, which I use to copy the production backup to a local database for testing, uses pg_restore:

    pg_restore -U username -O -x -d database_name_new path/to/file
    

    If you're using some other database, these tools would obviously be different, but most databases support backup and restoration in some form.

提交回复
热议问题