当前位置:网站首页>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 :
边栏推荐
- 项目实训12——解析建表的SQL语句
- Object 有哪些常用方法
- 2022 t elevator repair test question simulation test question bank and online simulation test
- Pulling method of common webcam
- 【QPSK中频】基于FPGA的QPSK中频信号产生模块verilog设计
- 线程池的创建
- [original] analysis of nine price HPV data capture of Yilu app
- DependencyManagement 和 Dependencies
- [original] analysis of nine price HPV data capture of Yilu app
- About string format(String format, Object... args)
猜你喜欢

PwnTheBox,Web:hello
![Authoritative guide to Web3 technology stack [2022]](/img/76/0f64604f5e5355300f5ec498ea23e1.png)
Authoritative guide to Web3 technology stack [2022]

Laravel8 enables alicloud file upload

"Draw the bow as strong, use the arrow as long", Manfu technology opens a new track for the data service industry

ICML2022 | 從零開始重新審視端到端的語音到文本翻譯

Development and implementation of AI intelligent video analysis easycvr platform device channel batch deletion function

Vulnhub practice DC-1 target
![[Interface tutorial] how does easycvr set platform cascading through the interface?](/img/d5/b7cbe9522e91a7ae29558bb2abe1fa.jpg)
[Interface tutorial] how does easycvr set platform cascading through the interface?
![[original] analysis of nine price HPV data capture of Yilu app](/img/e0/af4901d119a6e59de02a6786d427dd.png)
[original] analysis of nine price HPV data capture of Yilu app

【QPSK中频】基于FPGA的QPSK中频信号产生模块verilog设计
随机推荐
Sentinel
Vulnhub练习 DC-1靶机
爬虫学习知识
Clustered and non clustered indexes
通达信股票开户安全吗?如何办理开户呢?
[play with Huawei cloud] take you through the Kunpeng code migration tool to realize source code migration
2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation
Object 有哪些常用方法
[QPSK if] Verilog design of QPSK IF signal generation module based on FPGA
原生支持 ARM64 的首个版本!微软 Win11/10 免费工具集 PowerToys 0.59 发布
laravel8 实现阿里云文件上传
项目实训12——解析建表的SQL语句
[Interface tutorial] how does easycvr set platform cascading through the interface?
Lenovo explained for the first time the five advantages of hybrid cloud Lenovo xcloud and how to build an intelligent digital base
leetcode 130. Surrounded regions (medium)
【GMM+KDE】基于MATLAB的GMM和KDE核估计得目标跟踪仿真
Our understanding of the industrial Internet is still trapped in the logic of an Internet like platform and center
Display of successful cases of target customer matching data table
IP反查域名
数学与素质教育