Use mysqldump to realize regular backup of remote database in windows environment

Use mysqldump to realize regular backup of remote database in windows environment

Introduction: mysqldump is a built-in tool of mysql, which can be found in the bin folder under the mysql installation folder
insert image description here

1. Create a script

Create a new .batfile, for example, the file I created is backup.bat. The content of the script file is as follows:

set now=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%

set backup=C:\test\backup
if not exist %backup% md %backup%

set nowfile=%backup%\%now%
if not exist %nowfile% md %nowfile%

C:\test\mysql\mysql-8.0.23\bin\mysqldump -uroot -p123456 -h0.0.0.0 database table > %nowfile%\record.sql

exit

The first paragraph means to get and set the current time.

The second paragraph means to set the backup folder, if not, create a new folder (note the syntax of creating a new folder in the windows environment).

The third paragraph indicates that the folder named after the backup date is set to distinguish.

The fourth paragraph is the syntax of using mysqldump to perform backup:
C:\test\mysql\mysql-8.0.23\bin\mysqldump: the location of mysqldump;
root: remote database user name;
123456: remote database password;
0.0.0.0: remote database address;
database: the name of the database to be backed up;
table: to be backed up The table name.

The fifth paragraph exitsignifies withdrawal.

Save and exit after the setting is complete, double-click the bat file, under normal circumstances the script will be executed once, if not, there is a problem with the script.

Note:
1. Backup multiple tables: mysqldump -u user -p database table 1 table 2 ... table N > backup file path
2. Backup 1 database: mysqldump -u user -p -B database > backup file path
3. Back up multiple databases: mysqldump -u user -p -B library 1 library 2 ... library N > backup file path
4. Backup all data: mysqldump -u user -p -A > backup file path
Reference article: https:// blog.csdn.net/weixin_30361641/article/details/99012330

2. Set up Windows scheduled tasks

1. Use the shortcut key windows+R (the windows shortcut key is the one with a computer pattern drawn on the keyboard)
and open it like this:
insert image description here
2. Input taskschd.msc, click "OK", and the task scheduler page will appear, as shown in the figure below :
insert image description here
3. Click "Create Basic Task".
insert image description here
4. Customize the name and description of the basic task input.
insert image description here
5. Select the basic task execution cycle.
insert image description here
6. Set the basic task start time
insert image description here
7. Select the startup program, that is, the script file set before.
insert image description here
insert image description here
8. Name the script file;
9. Then you can see the scheduled task on the initial interface:
insert image description here

The pitfalls of the project

When I created the script in the first step, I only wrote one line of code in the script

C:\test\mysql\mysql-8.0.23\bin\mysqldump -uroot -p123456 -h0.0.0.0 database table > C:\test\record.sql

This causes the record.sql backup sql file to be refreshed after each backup, which cannot achieve the purpose of saving historical data. After passing through Baidu, the method of naming folders with dates can be used to better backup data and restore historical data.
Because this project only backs up one table, I always back up the whole table every time. If the database is backed up and the data volume is relatively large, an incremental backup method should be considered, such as a full database backup on Monday. Incremental backups etc.

Attachment: If you need full database backup in the windows environment, you can directly use the scheduled backup tool that comes with Navicat

Note: The Navicat version in this article is Navicat 15.
1. Click "Autorun".
insert image description here
2. Click "New Batch Job".
insert image description here

3. Click "Backup" --> Name of the database to be backed up --> Available jobs
insert image description here
4. After double-clicking, the specific information of the selected job will appear at the top:
insert image description here
5. Click the selected job --> Click "Save" -- > Enter the name of the configuration file (it is recommended to name it with time) --> Click "OK"
insert image description here
6. Set the selected job according to the actual situation, and click "OK" after the setting is completed.
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/problemRecord/article/details/118331678