当前位置:网站首页>Project training 10 - backup of specific databases
Project training 10 - backup of specific databases
2022-06-10 23:14:00 【ScarlettWX】
A valuable data collation system should have the function of database backup . Database management software such as Navicat You can simply back up , And you can extract the backup sql The file exists locally . The project has the functions of backup before deleting the user database and regular backup of the user database , This blog first discusses how to back up a database before deleting it .
The command to back up the database is mysqldump -u user name -p password Database name > route \\ file name , among mysqldump yes MySQL Server Of bin A... In the catalog exe Executable file , When executing the backup statement on the command line, you should first move the working directory to bin Under the table of contents , And the password position is omitted to ensure data security , Enter and execute the command before entering the password . stay Java No more... In code JDBC Execute backup command , have access to Runtime.getRuntime().exec(command); Calls to perform cmd command . I mentioned earlier that when you execute the backup command on the command line, you should go to MySQL Server Of bin Catalog , So in Java In the code, you should also go to this directory and execute the statement . Can be found in mysqldump with bin Directory address , You can also create a new folder in the project directory , take mysqldump.exe Copy in . Before the table of contents cmd /c,cmd /c dir After execution dir Close the command window after the command , If it is not added, the command will not be executed . The specific code is in serviceImpl In file , As shown below :
// Back up before deleting !
@Override
public int backupDB(int id) throws IOException {
DB db=dbMapper.findDBById(id);
String dbName=db.getDBEN();
System.out.println(dbName);
String user = "root";
String password = "xxxxxxxx";
logger.info(" The system backs up the database to be deleted ");
// current time Date format
LocalDate localDate = LocalDate.now();
// Backup file name
String fileName = dbName+localDate.toString() + ".sql";
String outFilePath = "D:\\backup\\"+fileName;
String command="cmd /c D:\\mysql\\mysqldump -u" + user + " -p" + password +" "+ dbName+ " > " +outFilePath;
Runtime.getRuntime().exec(command);
System.out.println(" Backup complete !");
return 1;
}
I encountered many errors when implementing the backup function .
- mysqldump The command execution environment must be in MySQL Server Of bin Under the table of contents , There has to be cmd /c, And there must be no extra space in the command ,“>” Add the absolute path to the file name after , On the one hand, problems may lead to 0KB The file of .
- Many online tutorials are biased towards Runtime Object executes the command and stores a Process object , Then, this object is marked with InputStream Input stream format storage BufferedReader In the object , Finally, a file output stream is generated to write the backup contents to the newly created file . This is not only troublesome , And invalid , You'll get one 0KB The file of . Direct the command to “>” Change the file name after to the absolute path + file name , The suffix is .sql, You can get the backup content directly sql file .
- The resulting backup file lacks table structure and table data . After solving the above two problems , I got a non - 0KB Of sql file , But only the database name information and some comments are available after opening , There is no table structure and data in the database . I tried again and again and found , Just delete the database after executing the backup command , Got sql The file has only comments . There may be a deviation in the execution order of the statements . So I chose to change the logic , A backup button is added to the delete database interface , You can choose to back up before deleting , If you directly click the delete button , You will be prompted to back up first . This will solve the problem sql The file lacks table structure and data .
After testing , The backed up database can restore its table structure and table data through the command line . Note that you should create a database with the same name before executing the command ! The order ismysql -u user name -p Database name < Absolute path \\ file name, It's also going to be bin Execute command under directory .
controller The layer code is :
// Backup database
@RequestMapping("/backupDB/{id}")
public Map<String, Object> backupDB(@PathVariable int id) throws Exception {
Map<String, Object> map = new HashMap<>();
// Backup first !
System.out.println(" To the back end id:"+id);
int bu=db.backupDB(id);
System.out.println(" The result of backing up the user database :"+bu);
if (bu>0) {
map.put("state", true);
map.put("msg", " Delete successful !");
} else {
map.put("state", false);
map.put("msg", " Delete failed !");
}
return map;
}
Front page display :
边栏推荐
- What are the restrictions on opening futures accounts? Where is the safest?
- 【Flutter 问题系列第 6 篇】如何在 Flutter 中实现列表消息的滚动效果
- Sentinel
- Software features and functions of the blind box mall app system development
- [Interface tutorial] how does easycvr set platform cascading through the interface?
- Locking mechanism
- Vulnhub练习 DC-1靶机
- Sdn/nfv application in cloud data center
- 样板小作坊
- 【GMM+KDE】基于MATLAB的GMM和KDE核估计得目标跟踪仿真
猜你喜欢

Simulated 100 questions and simulated examination of G2 utility boiler stoker examination in 2022

软件测试入门之软件测试的概念与过程(精辟内容)

Vulnhub practice DC-1 target

Ride the storm and explore the secret behind the open source of flyfish, a data visualization development platform!
![[original] analysis of nine price HPV data capture of Yilu app](/img/f2/c792367beea0956fe69aad5d35581a.png)
[original] analysis of nine price HPV data capture of Yilu app

Vscode common shortcuts

Design language testing for functional testing: what tests are included in functional testing? What is the role of each

数据与信息资源共享平台(四)

数据与信息资源共享平台(六)

200个C语言单词,请收藏!
随机推荐
中银证券股票开户安全吗?是正规的吗?
How Photoshop opens, edits and exports pictures in webp format
Several reasons and solutions of virtual machine Ping failure
Is there any risk in opening an account with BOC securities? Is it safe?
Optimize code to remove if else
Vulnhub's DC3
Sentinel
Multus CNI deployment and basic use of kubernetes multi network card scheme
That's great. The Ministry of industry and information technology has launched an internet account with a "one click unbinding" mobile phone number, which can be called an artifact
Image mosaic camera mosaic notes
kubernetes多網卡方案之Multus CNI部署和基本使用
Which securities company does qiniu's securities belong to? Is it safe?
06151020 mysterious code, waiting for you to decipher
28岁自学编程会不会太晚了?靠谱吗?
Auto.js pro 开发环境配置
[Interface tutorial] how does easycvr set platform cascading through the interface?
Display of successful cases of target customer matching data table
IP anti query domain name
[QPSK if] Verilog design of QPSK IF signal generation module based on FPGA
MySQL composite index is not used by hits