springMVC implements mysql/MariaDB scheduled task backup database

1. Add in the SpringMVC configuration file

xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task  
http://www.springframework.org/schema/task/spring-task-3.2.xsd

Configure task scanning

<task:annotation-driven />

Configure scan task location

<!-- scan task -->
    <context:component-scan base-package="com.jzwl.test.module.**.controller" />

 

Demo:

@Component("scheduledTaskManager")
@Controller
public class SolrTaskController{
//MariaDB timing database MariaDB installation path, mysql also applies C://Program Files//MariaDB10.3//bin/ 
public static String sqlurl1 = "C://Program Files//MariaDB10.3//bin//mysqldump -u root -proot -E test";
//If there is no function in the database, no need to comment below 

/* public static String sqlurl2 = "C://Program Files//MariaDB10.3//bin//mysqldump -u root -proot -R test"; */
public static String path = "f:/test.sql"; //Backup file storage location


@Scheduled(cron = "0 0 2 * * ?") //
public static void backup() throws IOException {
Runtime runtime = Runtime.getRuntime();
//-u is followed by username, -p It is best not to have spaces after the password -p, -family is the name of the database R event E data
Process process = runtime.exec(sqlurl1);
    InputStream inputStream = process.getInputStream();//得到输入流,写成.sql文件
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(reader);
String s = null;
StringBuffer sb = new StringBuffer();
while((s = br.readLine()) != null){
sb.append(s+"\r\n");
}

   //If there is no function in the database, no need to comment below 

/*
    Process process2 = runtime.exec(sqlurl2);
InputStream inputStream2 = process2.getInputStream();//得到输入流,写成.sql文件
InputStreamReader reader1 = new InputStreamReader(inputStream2);
BufferedReader br1 = new BufferedReader(reader1);
String s1 = null;
while((s1 = br1.readLine()) != null){
sb.append(s1+"\r\n");
}*/
    s= sb.toString();
File file = new File(path);
file.getParentFile().mkdirs();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
br.close();
reader.close();
inputStream.close();
System.out.println("数据库备份成功");
}
}

Precautions:

1. Spring's @Scheduled annotation needs to be written on the implementation method

2. The task method of the timer cannot have a return value (if there is a return value, spring will tell you that there is an error when initializing, and you need to set a certain value of proxytargetclass to true)

3. The implementation class must have component annotation @Component

 

Extension: setting of timing time

  Such as: "0/5 * * * * ?"

  The full format of CronTrigger configuration is: [seconds] [minutes] [hours] [days] [months] [weeks] [years]

serial number illustrate Is it required allowed values Wildcards allowed
1 Second Yes 0-59 , - * /
2 Minute Yes 0-59 , - * /
3 Hour Yes 0-23 , - * /
4 day Yes 1-31 , - * ? / L W
5 moon Yes 1-12 or JAN-DEC , - * /
6 week Yes 1-7 or SUN-SAT , - * ? / L W
7 year no empty or 1970-2099 , - * /

 

Wildcard description:

* means all values. For example: setting "*" on the field of minutes means it will be triggered every minute.


? means no value is specified. The use case is that you don't need to care about the value of the current setting of this field.

For example: to trigger an operation on the 10th of each month, but don't care about the day of the week, so the field that needs the week position is set to "?" Specifically set to 0 0 0 10 * ?


- Indicates the interval. For example, setting "10-12" on the hour means that it will be triggered at 10, 11, and 12 o'clock.


, means specifying multiple values, for example, setting "MON,WED,FRI" on the week field means triggering on Monday, Wednesday and Friday


/ for incremental triggering. If "5/15" is set on the second, it starts from 5 seconds and triggers every 15 seconds (5, 20, 35, 50). Set '1/3' on the month field to start on the 1st of each month and trigger every three days.


L means the last. On the day field setting, it means the last day of the current month (according to the current month, if it is February, it will also depend on whether it is a leap year [leap]), and on the week field it means Saturday, which is equivalent to "7" or "SAT". If you add a number before "L", it means the last of the data. For example, setting the format "6L" on the week field means "the last Friday of this month"


W represents the closest working day (Monday to Friday) to the specified date. For example, if you set "15W" on the day field, it means that the trigger will be triggered on the working day closest to the 15th of each month. If the 15th happens to be a Saturday, it will trigger on the nearest Friday (14th), if the 15th is a weekday, it will trigger on the nearest next Monday (16th). If the 15th happens to be on a working day (Monday to Weekly) 5), it will be triggered on that day. If the specified format is "1W", it means that it will be triggered on the nearest working day after the 1st of each month. If the 1st falls on a Saturday, it will trigger on the following Monday, the 3rd. (Note, only specific numbers can be set before "W", and interval "-" is not allowed).


# Serial number (indicates the day of the month), for example, setting "6#3" on the week field means the third Saturday of the month. Note that if "#5" is specified, there is no week in the fifth week Sixth, the configuration will not be triggered (it's perfect for Mother's Day and Father's Day);

Tip:
'L' and 'W' can be used in combination. If "LW" is set on the day field, it means that it will be triggered on the last working day of the month;
the setting of the week field is case-insensitive if English letters are used, that is, MON is the same as mon;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348565&siteId=291194637
Recommended