MySQL backup restore database batch

  • 2020-05-07 20:35:47
  • OfStack

Backup database (single table backup) bk_table.bat


mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database1 table1>table1.sql 
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database2 table2>table2.sql 
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database2 table3>table3.sql 
pause 

Restore database (single table restore) restore.bat

mysql -h127.0.0.1 -P3306 -uroot -proot database1 < table1.sql 
mysql -h127.0.0.1 -P3306 -uroot -proot database2 < table2.sql 
mysql -h127.0.0.1 -P3306 -uroot -proot database2 < table3.sql 
pause 

Backup the database and automatically package and compress it into.rar file (single table backup) bk_table.bat

mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database1 table1>table1.sql 
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database2 table2>table2.sql 
mysqldump -h127.0.0.1 -P3306 -uroot -proot --default-character-set=gbk -t database2 table3>table3.sql 
winrar a Db_Bak_%date:~0,10%.rar D:\DB_Backup\*.sql 
DEL D:\DB_Backup\*.sql 
pause 

After performing the batch processing, your backup data script will be compressed into the D:\DB_Backup Db_Bak_ date.rar file on your computer.
If "'WinRAR' is not an internal or external command, nor is it a runnable program or batch file." Error in the computer - > Property - > Advanced - > Environment variable - > In Path
Add your WinRAR path like: C:\Program Files\WinRAR.
4. Back up for the entire library

mysqldump -h127.0.0.1 -P3306 -uroot -proot database1 > database1.sql 
Pause 

Description:
1. Open notepad > Copy the above script to notepad - > Save as *.bat file can be double-clicked to open the execution.
2. You need to add your MySql installation bin directory path to the Path computer environment variable:
A computer - > Property - > Advanced - > Environment variable - > Add your Mysql installation bin directory path to Path :\Program Files\MySQL\MySQL Server 5.0\bin
Or copy the batch file to C:\Program \ Files\MySQL\ Server 5.0\bin for execution.
All above have been tested, O(∩_∩)O~


Related articles: