Skip to content

Commit

Permalink
backup via REST API. close #1949
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Dec 6, 2023
1 parent b3135a0 commit 2e885c8
Show file tree
Hide file tree
Showing 16 changed files with 3,904 additions and 25 deletions.
Binary file modified j-lawyer-client/lib/j-lawyer-cloud/j-lawyer-cloud.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ private void cmdAdHocBackupActionPerformed(java.awt.event.ActionEvent evt) {//GE
String password=p.getPassword();

try {
URL backupUrl = new java.net.URL(protocol + "://" + server + ":" + port + "/j-lawyer-server-war/autostart?action=backup.adhoc");
URL backupUrl = new java.net.URL(protocol + "://" + server + ":" + port + "/j-lawyer-server-war/autostart?action=backup.adhoc&jobid=" + System.currentTimeMillis());

URLConnection urlConnection = backupUrl.openConnection();
String userpass = principalId + ":" + password;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ You should also get your employer (if you work as a programmer) or school,
import org.jlawyer.io.rest.v6.DataBucketEndpointV6;
import org.jlawyer.io.rest.v6.SecurityEndpointV6;
import org.jlawyer.io.rest.v6.TemplatesEndpointV6;
import org.jlawyer.io.rest.v7.AdministrationEndpointV7;
import org.jlawyer.io.rest.v7.CasesEndpointV7;
import org.jlawyer.io.rest.v7.ConfigurationEndpointV7;
import org.jlawyer.io.rest.v7.MessagingEndpointV7;
Expand Down Expand Up @@ -707,6 +708,7 @@ public Set<Class<?>> getClasses()
s.add(ConfigurationEndpointV7.class);
s.add(CasesEndpointV7.class);
s.add(MessagingEndpointV7.class);
s.add(AdministrationEndpointV7.class);
return s;
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file modified j-lawyer-server/j-lawyer-server-ejb/lib/j-lawyer-cloud.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,11 @@ You should also get your employer (if you work as a programmer) or school,
import com.jdimension.jlawyer.persistence.FaxQueueBean;
import com.jdimension.jlawyer.persistence.ServerSettingsBean;
import com.jdimension.jlawyer.persistence.ServerSettingsBeanFacadeLocal;
import com.jdimension.jlawyer.pojo.JobStatus;
import com.jdimension.jlawyer.server.constants.MonitoringConstants;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import javax.annotation.security.PermitAll;
Expand All @@ -703,11 +705,12 @@ public class SingletonService implements SingletonServiceRemote, SingletonServic
private static final Logger log = Logger.getLogger(SingletonService.class.getName());

private int systemStatus = MonitoringConstants.LEVEL_NORMAL;
private HashMap<File, Date> observedFileNames = new HashMap<File, Date>();
private HashMap<File, Date> observedFileNames = new HashMap<>();
private FaxQueueBean failedFax = null;
protected EpostQueueBean failedLetter = null;
private ArrayList<FaxQueueBean> faxQueue = new ArrayList<>();
protected ArrayList<EpostQueueBean> epostQueue = new ArrayList<>();
private HashMap<String, JobStatus> jobStatus = new HashMap<>();

private long latestInstantMessageReceived=-1;
private long latestInstantMessageStatusUpdated=-1;
Expand Down Expand Up @@ -891,4 +894,48 @@ public void setEpostQueue(ArrayList<EpostQueueBean> epostQueue) {
this.epostQueue = epostQueue;
}

@Override
@PermitAll
public JobStatus getJobStatus(String jobId) {
this.purgeOldJobs();
return this.jobStatus.get(jobId);
}

@Override
@PermitAll
public void updateJobStatus(JobStatus jobStatus) {
this.purgeOldJobs();
if(jobStatus.getId()!=null)
this.jobStatus.put(jobStatus.getId(), jobStatus);
}

/*
* purges any jobs that are older than two days
*/
private void purgeOldJobs() {
ArrayList<String> removedKeys=new ArrayList<>();
for(String jobId: this.jobStatus.keySet()) {
JobStatus s=this.jobStatus.get(jobId);
if(s!=null) {
Date d=s.getLastUpdated();
if(d==null) {
removedKeys.add(jobId);
} else {
if((System.currentTimeMillis()-d.getTime())>2l*24l*60l*60l*1000l)
removedKeys.add(jobId);
}

}
}
for(String id: removedKeys) {
this.jobStatus.remove(id);
}
}

@Override
@RolesAllowed(value = {"loginRole"})
public Collection<JobStatus> listJobs() {
return this.jobStatus.values();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,10 @@ You should also get your employer (if you work as a programmer) or school,

import com.jdimension.jlawyer.persistence.EpostQueueBean;
import com.jdimension.jlawyer.persistence.FaxQueueBean;
import com.jdimension.jlawyer.pojo.JobStatus;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import javax.ejb.Local;
Expand Down Expand Up @@ -726,6 +728,12 @@ public interface SingletonServiceLocal {

long getLatestInstantMessageStatusUpdated();
void setLatestInstantMessageStatusUpdated(long latestInstantMessageStatusUpdated);

JobStatus getJobStatus(String jobId);

void updateJobStatus(JobStatus jobStatus);

Collection<JobStatus> listJobs();


}
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,12 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
out.println("<h1>Servlet AutoStartServlet at " + request.getContextPath() + "</h1>");

String action=request.getParameter("action");
String jobId=request.getParameter("jobid");
if(action!=null) {
if("backup.adhoc".equalsIgnoreCase(action)) {
out.println("starting ad-hoc backup...<br/>");
TransientTimer timer=TransientTimer.getInstance();
timer.scheduleAdHocBackup();
timer.scheduleAdHocBackup(jobId);
out.println("ad-hoc backup has been scheduled.<br/>");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,12 @@
import com.jdimension.jlawyer.persistence.ArchiveFileBean;
import com.jdimension.jlawyer.persistence.ServerSettingsBean;
import com.jdimension.jlawyer.persistence.ServerSettingsBeanFacadeLocal;
import com.jdimension.jlawyer.pojo.JobStatus;
import com.jdimension.jlawyer.server.utils.ServerFileUtils;
import com.jdimension.jlawyer.server.utils.ServerInformation;
import com.jdimension.jlawyer.services.ArchiveFileServiceLocal;
import com.jdimension.jlawyer.services.CalendarServiceLocal;
import com.jdimension.jlawyer.services.SingletonServiceLocal;
import com.jdimension.jlawyer.services.SystemManagementLocal;
import com.jdimension.jlawyer.storage.VirtualFile;
import com.jdimension.jlawyer.sync.FolderSync;
Expand All @@ -697,21 +699,44 @@ public class IterativeBackupTask extends java.util.TimerTask implements Cancella
private SimpleDateFormat dfMailTime = new SimpleDateFormat("HH:mm:ss");
private DecimalFormat mbFormat = new DecimalFormat("0.0");
private boolean adHoc = false;
private String jobId=null;

public IterativeBackupTask() {
this.adHoc = false;
this.jobId="" + System.currentTimeMillis();
}

public IterativeBackupTask(boolean adhoc) {
public IterativeBackupTask(boolean adhoc, String jobId) {
this.adHoc = adhoc;
this.jobId=jobId;
}

private void updateJobStatus(String status, float percentage, String details) {
if (this.jobId != null) {
try {

InitialContext ic = new InitialContext();
SingletonServiceLocal sng = (SingletonServiceLocal) ic.lookup("java:global/j-lawyer-server/j-lawyer-server-ejb/SingletonService!com.jdimension.jlawyer.services.SingletonServiceLocal");
JobStatus js = new JobStatus();
js.setJobType(JobStatus.TYPE_BACKUP);
js.setLastUpdated(new Date());
js.setDetails(details);
js.setStatus(status);
js.setPercentage(percentage);
js.setId(this.jobId);
sng.updateJobStatus(js);
} catch (Exception ex) {
log.error("Could not update job status", ex);
}
}
}

@Override
public void run() {

log.info("backup task is starting");
isRunning=true;

Date backupStart = new Date();
Date syncStart = new Date();
Date syncEnd = new Date();
Expand Down Expand Up @@ -782,6 +807,7 @@ public void run() {
if (dbUser == null || "".equals(dbUser)) {
log.warn("missing backup configuration - skipping backup!");
isRunning=false;
this.updateJobStatus(JobStatus.STATUS_FAILED, 0, "Missing backup configuration");
return;
}

Expand Down Expand Up @@ -878,6 +904,8 @@ public void run() {
log.error("Error getting ServerSettingsBean", t);
}

this.updateJobStatus(JobStatus.STATUS_INPROGRESS, 0, "preparing backup...");

log.info("getting directories...");
File directoryToZip = new File(System.getProperty("jlawyer.server.basedirectory").trim());
String backupDir = directoryToZip.getParentFile().getPath() + System.getProperty("file.separator") + "backups";
Expand All @@ -892,6 +920,7 @@ public void run() {
try {
log.info("starting backup");
backupResult=ibe.execute();
this.updateJobStatus(JobStatus.STATUS_INPROGRESS, 30, "Backup created.");
} catch (Throwable t) {
backupSuccess=false;
backupResult=new BackupResult();
Expand All @@ -914,6 +943,7 @@ public void run() {
exportStart = new Date();
if (exportEnabled) {
log.info("Starting export to " + exportLocation);
this.updateJobStatus(JobStatus.STATUS_INPROGRESS, 30, "Exporting to " + exportLocation);
InitialContext ic = new InitialContext();
ArchiveFileServiceLocal caseSvc = (ArchiveFileServiceLocal) ic.lookup("java:global/j-lawyer-server/j-lawyer-server-ejb/ArchiveFileService!com.jdimension.jlawyer.services.ArchiveFileServiceLocal");
CalendarServiceLocal calendarSvc = (CalendarServiceLocal) ic.lookup("java:global/j-lawyer-server/j-lawyer-server-ejb/CalendarService!com.jdimension.jlawyer.services.CalendarServiceLocal");
Expand Down Expand Up @@ -947,6 +977,7 @@ public void run() {
}
export.exportReviews();
log.info("Export finished");
this.updateJobStatus(JobStatus.STATUS_INPROGRESS, 60, "Export finished");
}

} catch (Throwable ex) {
Expand All @@ -960,6 +991,7 @@ public void run() {
try {
if (syncLocation.length() > 0) {
log.info("Starting sync to " + BackupSyncTask.removePasswordFromUrl(syncLocation));
this.updateJobStatus(JobStatus.STATUS_INPROGRESS, 65, "Syncing to " + BackupSyncTask.removePasswordFromUrl(syncLocation));
syncStart = new Date();

try {
Expand Down Expand Up @@ -1028,6 +1060,12 @@ public void run() {
body.append("\r\n");
body.append("Hinweis: Speichern Sie die Sicherungsdatei regelmäßig auf einem anderen Medium, idealerweise in anderen Räumlichkeiten (für den Fall eines Diebstahls oder Brandes).");

if(!backupSuccess || !syncSuccess || !exportSuccess) {
this.updateJobStatus(JobStatus.STATUS_FAILED, 100, "Failed - check logs for details");
} else {
this.updateJobStatus(JobStatus.STATUS_FINISHED, 100, "Finished");
}

if(exportSuccess && syncSuccess && backupSuccess && notifySuccess) {
sysMan.statusMail(subject, body.toString());
} else if((!exportSuccess || !syncSuccess || !backupSuccess) && notifyFailure) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,9 @@ public void start() {
}
}

public void scheduleAdHocBackup() {
public void scheduleAdHocBackup(String jobId) {
if (timerBackup != null) {
timerBackup.schedule(new IterativeBackupTask(true), 3000l);
timerBackup.schedule(new IterativeBackupTask(true, jobId), 3000l);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@
public class IterativeBackupExecutor {

private static final Logger log = Logger.getLogger(IterativeBackupExecutor.class.getName());

// these directories will be zipped entirely with each backup run
private final String[] fullBackupDirs = new String[]{"emailtemplates", "mastertemplates", "faxqueue", "templates", "letterheads"};
// these directories will be zipped on a per subdirectory basis and only if there are changes
Expand All @@ -695,7 +695,7 @@ public class IterativeBackupExecutor {
private String encryptionPassword = "";
private String dataDirectory = null;
private String backupDirectory = null;

public IterativeBackupExecutor(String dataDirectory, String backupDirectory, String dbUser, String dbPassword, String dbPort, String encryptionPassword) {
this.dataDirectory = dataDirectory;
this.backupDirectory = backupDirectory;
Expand All @@ -706,9 +706,8 @@ public IterativeBackupExecutor(String dataDirectory, String backupDirectory, Str
}

public BackupResult execute() throws Exception {

log.info("backup executor has been launched");

log.info("backup executor has been launched");
BackupResult backupResult = new BackupResult();

boolean encrypt = false;
Expand Down Expand Up @@ -789,12 +788,12 @@ public BackupResult execute() throws Exception {
try {
for (String fullBackupDir : fullBackupDirs) {
List<File> fileList = new ArrayList<>();
File checkAndCreate=new File(this.dataDirectory + File.separator + fullBackupDir);

File checkAndCreate = new File(this.dataDirectory + File.separator + fullBackupDir);
checkAndCreate.mkdirs();
log.info("Creating zip file for full backup dir " + this.dataDirectory + File.separator + fullBackupDir);
getAllFiles(new File(this.dataDirectory + File.separator + fullBackupDir), fileList);

targetDir = new File(this.backupDirectory + File.separator + fullBackupDir);
targetDir.mkdirs();
this.clearDirectory(targetDir);
Expand All @@ -806,8 +805,9 @@ public BackupResult execute() throws Exception {
}
}


for (String itBackupDir : iterativeBackupDirs) {

// remove files in backup dir that are no longer in the source
File currentBackupDir = new File(this.backupDirectory + File.separator + itBackupDir);
log.debug("Creating zip file for iterative backup dir " + this.backupDirectory + File.separator + itBackupDir);
Expand Down Expand Up @@ -911,8 +911,8 @@ private static File dumpDatabase(String user, String password, String port, Stri

String osName = System.getProperty("os.name").toLowerCase();
String path = "";
String backupFilePath=backupDir + System.getProperty("file.separator") + "jlawyerdb-dump.sql";
String backupFilePath = backupDir + System.getProperty("file.separator") + "jlawyerdb-dump.sql";

if (osName.contains("win")) {

} else if (osName.contains("linux")) {
Expand Down Expand Up @@ -949,29 +949,29 @@ private static File dumpDatabase(String user, String password, String port, Stri
Process process = null;

File f = new File(backupFilePath);
int exitCode=0;
int exitCode = 0;
try {

if (f.exists()) {
f.delete();
}

process = shell.exec(cmd);
exitCode=process.waitFor();
exitCode = process.waitFor();

f.setLastModified(System.currentTimeMillis());

} catch (Exception ex) {
//res = false;
log.error(ex);

}
if(exitCode!=0) {

if (exitCode != 0) {
log.error("mysqldump returned with exit code " + exitCode);
throw new Exception("Datenbank-Dump fehlgeschlagen - Rückgabewert " + exitCode);
}

return f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,6 @@ You should also get your employer (if you work as a programmer) or school,
import com.jdimension.jlawyer.timer.executors.IterativeBackupExecutor;
import java.io.File;
import junit.framework.Assert;
import net.lingala.zip4j.core.ZipFile;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
Expand Down

0 comments on commit 2e885c8

Please sign in to comment.