当前位置:网站首页>Mysql database backup under Windows Environment

Mysql database backup under Windows Environment

2022-06-24 05:41:00 User 1685462

Use mysqldump Database backup

mysql The database has its own backup command mysqldump, The database can be backed up

The simplest backup is to back up the database locally , Generate **.sql file

Write a backup script file

( Create a txt file , Write batch script , Then change the suffix of the file to .bat Become a batch script file )

rem autherBeginnerXiao
rem date:20200814
rem ******Backup MySQL Start******
@echo off

:: Set the time variable

set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%"

:: Create a stored folder

if not exist "D:\mysql_backup" md "D:\mysql_backup"

:: Perform backup operations

"D:\mysql-8.0.20-winx64\bin\mysqldump" --opt --user=root --password=root --host=127.0.0.1 --protocol=tcp --port=3306 --default-character-set=utf8 --single-transaction=TRUE --routines --events "demo" >D:\mysql_backup\backup_demo_%Ymd%.sql

:: Delete the backup data two weeks ago

forfiles /p "D:\mysql_backup" /m backup_*.sql -d -14 /c "cmd /c del /f @path"

@echo on
rem ******Backup MySQL End******

Some of the key statements explain :

Set up a folder for the storage location of backup files , That is, if the file does not exist md Create the folder

:: Create a stored folder

    if not exist "D:\mysql_backup" md "D:\mysql_backup"

Backup operations :

“D:\mysql-8.0.20-winx64\bin\mysqldump” : Execute local mysql File installation path bin In folder mysqldump

–single-transaction=TRUE: There is no lock table during backup

–user=root :mysql Account number

–password=root :mysql password

–host=127.0.0.1 :mysql Of ip Address

–port=3306 :mysql Port number

–default-character-set=utf8 : Backup default encoding

events “demo” : Name of the database being backed up

>D:\mysql_backup\backup_demo_%Ymd%.sql : Backup file storage path

set “Ymd=%date:0,4%%date:5,2%%date:~8,2%” : Set the date parameter for backup , To supply sql After using

Reference to time parameters :

%date:~0,10% // Extract the date information

%date:~-3% // Extract day of the week information

%time:~0,5% // Extract the hours and minutes of time

%time:~0,-3% // Extract hour, minute and second information

* Note that the script file is separated by spaces and commas , So if there are spaces in the path , Must be enclosed in double quotation marks

* It is generally recommended that the path be enclosed in double quotation marks

:: Perform backup operations

"D:\mysql-8.0.20-winx64\bin\mysqldump" --opt --user=root --password=root --host=127.0.0.1 --protocol=tcp --port=3306 --default-character-set=utf8 --single-transaction=TRUE --routines --events "demo" >D:\mysql_backup\backup_demo_%Ymd%.sql

Expire and delete the backup files

Forfiles: Select the file to batch from the folder or tree .

grammar :

forfiles [/p Path ] [/m SearchMask ] [/s ] [/c Command ] [/d [{+ | - }] [{MM / DD / YYYY | DD }]]

Parameters :

/p Path: Appoint Path , Indicate where to start searching . The default folder is the current working directory , The directory by typing a period (.) Appoint .

/m SearchMask: according to SearchMask Search for files . default SearchMask yes *.* .

/m backup_*.sql It means to search by backup_ The prefix ,.sql A file with a suffix

/s: instructions forfiles Search in subdirectories .

/c Command: Run the specified... On each file Command . Command strings with spaces must be enclosed in quotation marks . default Command yes "cmd /c echo @file" .

/d [{+ | - }] [{MM / DD / YYYY | DD }]: Select a date greater than or equal to (+ )( Or less than or equal to (- )) File with specified date , among MM / DD / YYYY Is the specified date ,DD Is the current date minus DD God . If not specified + or - , Then use + .DD The effective range of is 0 - 32768.

-d -14: The former 14 God

:: Delete the backup data two weeks ago

forfiles /p "D:\mysql_backup" /m backup_*.sql -d -14 /c "cmd /c del /f @path"

Cross host backup

( One ) Back up the required database to the specified database of another host

grammar :

mysqldump --host= Source database ip -u Source database account  -p Source database password  --opt  Database to back up  | mysql --host= Target machine ip -u Target database user name  -p Target database password  -C  Target database 

mysqldump --host=127.0.0.1 -uroot -proot --opt demo | mysql --host=11.11.11.11  -uroot -proot -C demo

( Two ) Remote backup database sql file

grammar :

"D:\mysql-8.0.20-winx64\bin\mysqldump" --opt --user=root --password=root --host= Remote database ip --protocol=tcp --port=3306 --default-character-set=utf8 --single-transaction=TRUE --routines --events "demo" >D:\mysql_backup\backup_demo_%Ymd%.sql

notes :

The prerequisite for remote backup is , The remote database needs to create a local database IP Users who can access , Create remote access user actions , see blog:MySQL Database users create 、 modify 、 Authorization and remote access

Restore database

Two restore methods :

mysqldump -uroot -proot -h127.0.0.1 demo < "D:\mysql_backup\backup_demo_20200814.sql"
mysql -uroot -proot demo< "D:\mysql_backup\backup_demo_20200814.sql"

Windows Execute script tasks regularly

We can set Windows Scheduled task to run the backup script regularly , To achieve the purpose of scheduled backup

1. You can search in the program search list “ Task scheduler ”

2. After opening the task scheduler, click... On the right “ Create basic tasks ”, And write the name and description of the planned task :

3. Select the period of scheduled execution :

4. Set the time you need to execute , Backup can usually be done late at night

5. Let the scheduled task start the script

6. Select the completed backup script file :

7. This will give us a task Overview , Click finish after confirming that the information is correct

8. Create good after , The task we created can be seen in the task list , You can right-click this task to delete it

原网站

版权声明
本文为[User 1685462]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210805190122092K.html

随机推荐