当前位置:网站首页>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
边栏推荐
- What domain name is good? How to choose a good domain name?
- How to apply for free website domain name does the domain name need authentication
- How to do domain name resolution? What does domain name resolution mean?
- Tamp the foundation, step into the cloud and rise to the original cloud Devops
- When a beef cow has an "electronic ID card"
- [performance test] 3 How to calculate the number of concurrent users
- [competition experience sharing] a problem-solving summary of the whole DFS (internal track)
- How to build a website after registering a domain name? Do you need maintenance later?
- How do virtual hosts bind domain names? Can binding failure be used normally?
- What is the domain name of Org? Why do I need domain name resolution
猜你喜欢
What cloud native knowledge should programmers master?

Answer questions! This article explains the automated testing framework in software testing from beginning to end
Learning routes and materials for cloud native O & M engineers

How should we learn cloud native in 2022?
Easy to understand JDBC tutorial - absolutely suitable for zero Foundation
随机推荐
Answer questions! This article explains the automated testing framework in software testing from beginning to end
Talk about my working experience in Tencent and byte
What is the domain name of the website? What problems should be paid attention to when applying for a domain name
How to check the Chinese domain name of the website and through what channels?
What enlightenment does it consulting giant Accenture get from the blackmail attack?
Supply chain innovation of industrial Internet -- supply chain +5g Technology
How do users check the domain name registrar? What are the conditions for domain name registration?
How do users purchase domain names? What should I pay attention to when buying a domain name?
What is the domain name of Org? Why do I need domain name resolution
Go's package management learning notes
What is the meaning of domain name being walled and what is the solution
How to resolve computer domain name resolution errors how to select a good domain name
How to build a website after registering a domain name? Do you need maintenance later?
How to build a website with a domain name? Can I build a website before applying for a domain name?
How to apply for a primary domain name? Is primary domain name good or secondary domain name good?
Easy to understand JDBC tutorial - absolutely suitable for zero Foundation
How to build a website after successfully registering a domain name? Can I build a website without registering a domain name?
How do users in the insurance upgrade industry choose?
When a beef cow has an "electronic ID card"
How to get a secondary domain name? What does a secondary domain name mean?