当前位置:网站首页>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
边栏推荐
- How to apply for a website domain name and what problems should be paid attention to
- How to buy a network domain name? Is the domain expensive
- How to build a website after registering a domain name? Can individuals register domain names to build websites?
- Pylin tool usage
- [latest offer] 2-core 4G server is limited to 188 yuan, and it is in the process of being snapped up
- What is primary domain name? What are the divisions of domain names
- The 2021 smart Expo is about to open. Tencent Youtu and "Ai Gallery" will "Chongqing" with you
- Where to register the HK domain name? What should I pay attention to when registering the domain name
- [performance test] 3 How to calculate the number of concurrent users
- What cloud native knowledge should programmers master?
猜你喜欢
What cloud native knowledge should programmers master?
Learning routes and materials for cloud native O & M engineers
Easy to understand JDBC tutorial - absolutely suitable for zero Foundation

How should we learn cloud native in 2022?

Answer questions! This article explains the automated testing framework in software testing from beginning to end
随机推荐
What does it mean that the domain name is being walled? How to solve the problem of domain name by wall?
Learning routes and materials for cloud native O & M engineers
Spirit breath development log (7)
How to register an overseas domain name what should be paid attention to when registering a domain name
How to build a website after registering a domain name? Do you need maintenance later?
Easy to understand JDBC tutorial - absolutely suitable for zero Foundation
How to apply for a primary domain name? Is primary domain name good or secondary domain name good?
What is the website domain name and how to register the secondary domain name?
What is the domain name of Org? Why do I need domain name resolution
[JS reverse hundred examples] Dangle login interface parameters reverse
Why storage?
The personal information protection law was passed today. What problems should enterprises pay attention to?
How to register an enterprise domain name? Can an enterprise domain name be directly registered by individuals?
How do users check the domain name registrar? What are the conditions for domain name registration?
Threat discovery under automated data analysis
How to apply for a domain name? Why should domain names be filed in advance?
Tencent (t-sec NTA) was listed in the report emerging trends: best use cases for network detection and response recently released by Gartner
How to buy a domain name? Do you need to file a domain name purchase?
How to use a Chinese domain name? Would you prefer a Chinese domain name or an English domain name?
How to apply for a company domain name? Does it cost money to apply for a company domain name?