|
|
@@ -1,8 +1,12 @@
|
|
|
-package es.uv.saic.domain;
|
|
|
+package es.uv.saic.config;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
import java.sql.SQLException;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
@@ -13,6 +17,10 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import es.uv.saic.domain.Calendari;
|
|
|
+import es.uv.saic.domain.Email;
|
|
|
+import es.uv.saic.domain.InstanciaTasca;
|
|
|
+import es.uv.saic.domain.Usuari;
|
|
|
import es.uv.saic.service.CalendariService;
|
|
|
import es.uv.saic.service.EmailService;
|
|
|
import es.uv.saic.service.InstanciaService;
|
|
|
@@ -51,6 +59,13 @@ public class ScheduledTasks {
|
|
|
private String parserEnabled;
|
|
|
@Value("${saic.scheduler.expired.enabled}")
|
|
|
private String expiredEnabled;
|
|
|
+ @Value("${saic.backups.database.enabled}")
|
|
|
+ private boolean backupDatabaseEnabled;
|
|
|
+ @Value("${saic.backups.database.exec}")
|
|
|
+ private String backupDatabaseExec;
|
|
|
+ @Value("${saic.backups.database.filePath}")
|
|
|
+ private String backupDatabasePath;
|
|
|
+
|
|
|
|
|
|
@Scheduled(fixedDelay = 300000)
|
|
|
public void sendInstanceMails() {
|
|
|
@@ -98,4 +113,26 @@ public class ScheduledTasks {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Scheduled(cron="0 0 3 * * *")
|
|
|
+ public void doBackups() throws InterruptedException {
|
|
|
+ if(this.backupDatabaseEnabled) { // do database backups if enabled
|
|
|
+ try {
|
|
|
+ List<File> sorted = Arrays.asList(new File(backupDatabasePath).listFiles());
|
|
|
+ sorted.sort(Comparator.comparing(File::lastModified));
|
|
|
+ if(sorted.size() == 3) {
|
|
|
+ sorted.get(0).delete();
|
|
|
+ }
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ String fileName = "db_"+now.getYear()+"."+now.getDayOfMonth()+"."+now.getMonthValue()+"_"+now.getHour()+"."+now.getMinute()+"."+now.getSecond()+".sql";
|
|
|
+ ProcessBuilder pb = new ProcessBuilder("bash", "-c", backupDatabaseExec+" --output \""+backupDatabasePath+fileName+"\"");
|
|
|
+ //pb.inheritIO();
|
|
|
+ Process p = pb.start();
|
|
|
+ p.waitFor();
|
|
|
+ }
|
|
|
+ catch(IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|