import org.apache.commons.vfs.FileObject;
-/** Backups a MySQL database using mysqldump */
+/** Backups a MySQL database using mysqldump. */
public class MySqlBackup extends OsCallBackup {
private String mysqldumpLocation = "/usr/bin/mysqldump";
private String dbName;
public MySqlBackup() {
- super();
}
public MySqlBackup(String dbUser, String dbPassword, String dbName) {
- super(dbName + ".sql");
this.dbUser = dbUser;
this.dbPassword = dbPassword;
this.dbName = dbName;
+ init();
}
@Override
public void init() {
if (getName() == null)
- setName(dbName + ".sql");
+ setName(dbName + ".mysql");
super.init();
}
private Map<String, String> variables = new HashMap<String, String>();
private Executor executor = new DefaultExecutor();
+ private Map<String, String> environment = new HashMap<String, String>();
+
public OsCallBackup() {
}
ExecuteStreamHandler streamHandler = new PumpStreamHandler(
targetContent.getOutputStream(), errBos);
executor.setStreamHandler(streamHandler);
- executor.execute(commandLine);
+ executor.execute(commandLine, environment);
} catch (ExecuteException e) {
byte[] err = errBos.toByteArray();
String errStr = new String(err);
return command;
}
+ /**
+ * A reference to the environment variables that will be passed to the
+ * process. Empty by default.
+ */
+ protected Map<String, String> getEnvironment() {
+ return environment;
+ }
+
protected Map<String, String> getVariables() {
return variables;
}
--- /dev/null
+package org.argeo.server.backup;
+
+import org.apache.commons.vfs.FileObject;
+
+/** Backups a PostgreSQL database using pg_dump. */
+public class PostgreSqlBackup extends OsCallBackup {
+ /**
+ * PostgreSQL password environment variable (see
+ * http://stackoverflow.com/questions
+ * /2893954/how-to-pass-in-password-to-pg-dump)
+ */
+ protected final static String PGPASSWORD = "PGPASSWORD";
+
+ private String pgDumpLocation = "/usr/bin/pg_dump";
+
+ private String dbUser;
+ private String dbPassword;
+ private String dbName;
+
+ public PostgreSqlBackup() {
+ super();
+ }
+
+ public PostgreSqlBackup(String dbUser, String dbPassword, String dbName) {
+ this.dbUser = dbUser;
+ this.dbPassword = dbPassword;
+ this.dbName = dbName;
+ init();
+ }
+
+ @Override
+ public void init() {
+ // disable compression since pg_dump is used with -Fc option
+ setCompression(null);
+
+ if (getName() == null)
+ setName(dbName + ".pgdump");
+ super.init();
+ }
+
+ @Override
+ public void writeBackup(FileObject targetFo) {
+ if (getCommand() == null) {
+ getEnvironment().put(PGPASSWORD, dbPassword);
+ setCommand(pgDumpLocation + " -Fc" + " -U ${dbUser} ${dbName}");
+ }
+ getVariables().put("dbUser", dbUser);
+ getVariables().put("dbPassword", dbPassword);
+ getVariables().put("dbName", dbName);
+
+ super.writeBackup(targetFo);
+ }
+
+ public void setDbUser(String dbUser) {
+ this.dbUser = dbUser;
+ }
+
+ public void setDbPassword(String dbPassword) {
+ this.dbPassword = dbPassword;
+ }
+
+ public void setDbName(String dbName) {
+ this.dbName = dbName;
+ }
+
+ public void setPgDumpLocation(String mysqldumpLocation) {
+ this.pgDumpLocation = mysqldumpLocation;
+ }
+
+}
SortedMap<DateTime, FileObject> toDelete = new TreeMap<DateTime, FileObject>();
int backupCount = 0;
- // scan backups an list those which should be deleted
+ // make sure base dir exists
+ baseFo.createFolder();
+
+ // scan backups and list those which should be deleted
for (FileObject backupFo : baseFo.getChildren()) {
String backupName = backupFo.getName().getBaseName();
Date backupDate = dateFormat.parse(backupName);
--- /dev/null
+package org.argeo.server.backup;
+
+import java.io.File;
+
+import org.apache.commons.vfs.FileObject;
+
+/** Backups a Subversion repository using svnadmin. */
+public class SvnBackup extends OsCallBackup {
+ private String svnadminLocation = "/usr/bin/svnadmin";
+
+ private String repoLocation;
+ private String repoName;
+
+ public SvnBackup() {
+ }
+
+ public SvnBackup(String repoLocation) {
+ this.repoLocation = repoLocation;
+ init();
+ }
+
+ @Override
+ public void init() {
+ // use directory as repo name
+ if (repoName == null)
+ repoName = new File(repoLocation).getName();
+
+ if (getName() == null)
+ setName(repoName + ".svndump");
+ super.init();
+ }
+
+ @Override
+ public void writeBackup(FileObject targetFo) {
+ if (getCommand() == null) {
+ setCommand(svnadminLocation + " dump " + " ${repoLocation}");
+ }
+ getVariables().put("repoLocation", repoLocation);
+
+ super.writeBackup(targetFo);
+ }
+
+ public void setRepoLocation(String repoLocation) {
+ this.repoLocation = repoLocation;
+ }
+
+ public void setRepoName(String repoName) {
+ this.repoName = repoName;
+ }
+
+ public void setSvnadminLocation(String mysqldumpLocation) {
+ this.svnadminLocation = mysqldumpLocation;
+ }
+
+}
MySqlBackup mySqlBackup = new MySqlBackup("root", "", "test");
atomicBackups.add(mySqlBackup);
+ PostgreSqlBackup postgreSqlBackup = new PostgreSqlBackup(
+ "argeo", "argeo", "gis_template");
+ atomicBackups.add(postgreSqlBackup);
+ SvnBackup svnBackup = new SvnBackup(
+ "/home/mbaudier/tmp/testsvnrepo");
+ atomicBackups.add(svnBackup);
systemBackup.setAtomicBackups(atomicBackups);