1. Set the relevant database connection statements in the jdbc.properties file
jdbc_url=jdbc:mysql://ip address: port / database name? Characterencoding = utf8 & zerodatetimebehavior = round
jdbc_username=User name
jdbc_password=User password
2. Read the modified properties file in applicationContext.xml
<context:property-placeholder location="classpath:classpath:conf/jdbc.properties" />
3. Declare a database connection statement object (sysBackupAndRecoveryConfig) with the following properties (omit get, set)
private String port;
private String username;
private String host;
private String password;
private String database;
4. The serviceimpl layer injects the above property object and gets the properties we want in the jdbc.properties file in applicationContext
@Resource
private SysBackupAndRecoveryConfig sysBackupAndRecoveryConfig;
@Value("${jdbc_url}")
private String sqlUrl;
@Value("${jdbc_username}")
private String userName;
@Value("${jdbc_password}")
private String passWord;
5. According to the obtained database connection statement url, the database name, IP address and character code are resolved
/**
*Analyze the database connection statement to get the desired data
*
*/
public void setBackupInfo()
{
String sqlPath=sqlUrl;
String[] split = sqlPath.split(":");
sysBackupAndRecoveryConfig.setHost(split[2].substring(2));
String[] splits= split[3].split("/");
sysBackupAndRecoveryConfig.setPort(splits[0]);
sysBackupAndRecoveryConfig.setCharset(splits[1].split("\\?")[1].split("&")[0].split("=")[1]);
sysBackupAndRecoveryConfig.setDatabase(splits[1].split("\\?")[0]);
sysBackupAndRecoveryConfig.setUsername( userName);
sysBackupAndRecoveryConfig.setPassword(passWord);
}
6. Database backup.
The key code is as follows. There are other detailed codes on the Internet. mysqldump is used for backup
process = runtime.exec(" mysqldump -R --port="+sysBackupAndRecoveryConfig.getPort()+" -h" +sysBackupAndRecoveryConfig.getHost() +
" -p"+sysBackupAndRecoveryConfig.getPassword()+" -u" + sysBackupAndRecoveryConfig.getUsername()+ " --set-charset="+sysBackupAndRecoveryConfig.getCharset()+" " + sysBackupAndRecoveryConfig.getDatabase());
Complete code
/**
* backups
* @throws ParseException
* @throws NumberFormatException
*
*/
private FileOutputStream fileOutputStream;
private BufferedReader br ;
private InputStreamReader reader;
private InputStream inputStream;
@Override
public void dataBackup() {
String realPath=getProjectPath();
setBackupInfo();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMddHHmmss");
Date date=new Date();
String sqlDate= dateFormater.format(date);
long time = new Date().getTime();
File file = new File(realPath+"/"+time);
if (!file .exists() && !file .isDirectory())
{
file.mkdirs();
}
File realfile=new File(file,sqlDate+"_"+sysBackupAndRecoveryConfig.getDatabase()+".sql");
Runtime runtime = Runtime.getRuntime();
Process process;
try {
process = runtime.exec(" mysqldump -R --port="+sysBackupAndRecoveryConfig.getPort()+" -h" +sysBackupAndRecoveryConfig.getHost() +
" -p"+sysBackupAndRecoveryConfig.getPassword()+" -u" + sysBackupAndRecoveryConfig.getUsername()+ " --set-charset="+sysBackupAndRecoveryConfig.getCharset()+" " + sysBackupAndRecoveryConfig.getDatabase());
inputStream = process.getInputStream();
reader= new InputStreamReader(inputStream,"utf-8");
br = new BufferedReader(reader);
StringBuffer sb = new StringBuffer();
String s = null;
while((s = br.readLine()) != null){
sb.append(s+"\r\n");
}
s = sb.toString();
fileOutputStream = new FileOutputStream(realfile);
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
br.close();
reader.close();
inputStream.close();
} catch (IOException e1) {
delFolder(realfile.getAbsolutePath());
throw new RuntimeException("Backup failed");
}finally {
try {
if (null != fileOutputStream)
{
fileOutputStream.close();
}
if (null != br)
{
br.close();
}
if (null != reader)
{
reader.close();
}
if (null != inputStream)
{
inputStream.close();
}
} catch (IOException e) {
delFolder(realfile.getAbsolutePath());
throw new RuntimeException("Shutdown failure");
}
}
}
7. Database restore
The key code is as follows. There are other detailed codes on the Internet. Restore mysql
process = runtime.exec("mysql --default-character-set="+sysBackupAndRecoveryConfig.getCharset()+" -port"+sysBackupAndRecoveryConfig.getPort()+" -u"+sysBackupAndRecoveryConfig.getUsername()+" -p"+sysBackupAndRecoveryConfig.getPassword()+" "+sysBackupAndRecoveryConfig.getDatabase()+"");
Complete code
/**
* reduction
*
*/
private OutputStream outputStream ;
private OutputStreamWriter writer;
@Override
public void dataRecovery(String fileName,String backupDate) {
setBackupInfo();
Runtime runtime = Runtime.getRuntime();
String path=getProjectPath();
String realpath = getRepath(path,fileName,backupDate);
Process process;
//local
try {
process = runtime.exec("mysql --default-character-set="+sysBackupAndRecoveryConfig.getCharset()+" -port"+sysBackupAndRecoveryConfig.getPort()+" -u"+sysBackupAndRecoveryConfig.getUsername()+" -p"+sysBackupAndRecoveryConfig.getPassword()+" "+sysBackupAndRecoveryConfig.getDatabase()+"");
// process = runtime.exec("mysql --default-character-set=utf8 -port3306 -uroot -p9790 testtwn");
outputStream = process.getOutputStream();
br = new BufferedReader(new InputStreamReader(new FileInputStream(realpath)));
String str = null;
StringBuffer sb = new StringBuffer();
while((str = br.readLine()) != null){
sb.append(str+"\r\n");
}
str = sb.toString();
writer = new OutputStreamWriter(outputStream,"utf-8");
writer.write(str);
writer.flush();
outputStream.close();
br.close();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException("restore failed");
}finally {
try {
if (null != outputStream)
{
outputStream.close();
}
if (null != br)
{
br.close();
}
if (null != writer)
{
writer.close();
}
} catch (IOException e) {
throw new RuntimeException("Shutdown failure");
}
}
}
mysql database backup and restore are basically these. This is the first module that our project leader asked me to do. Later, I used the quartz timer to do automatic backup. When I have time, I can reorganize it. In addition, the partners who need complete code can find me. After I reorganize it, I can see if I can send it here.