cron job replace (to restore) a database
I am setting up a test site to allow users to make changes and I need to restore the database every hour.
I have found this script for the cron that works fine but just for the first time. This script will not restore an existing database because of duplicate entries, so is not good for my test site.
mysql -u user -ppassword databasename < /path/to/backup.sql
All I want to do is to restore my dump database (and possibly all the files as well) but I can't find how to do it.
This is the error that I receive via email when I run this script:
ERROR 1062 (2300开发者_开发问答0) at line 62: Duplicate entry '1' for key 'PRIMARY'
I guess I should have a way of deleting the database first and immediately after restore the original dump.
There are two options that I can think of here:
You can create another backup of your original database with an option that DELETEs the tables before restoring the database. This effectively adds DELETE lines to the generated .sql file. The command to do this is:
mysqldump --add-drop-table -u [username] -p [password] [database] > backup.sql
Use mysqlimport to import a backup for a database that already exists like so:
mysqlimport -u [username] -p [password] [database] backup.sql
Hope this helps.
精彩评论