This program takes the backup of a relational database PostGres using java code. It can be easily modified for other databases. Just need to supply the respective commands.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.omg.CORBA.portable.InputStream;
public class dataBaseMigration {
public static void main(String[] args) throws Exception {
System.out.println("*********** Main Function *********** ");
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd \"C:\\Program Files\\PostgreSQL\\9.4\\bin\" && pg_dump --format=c
--username \"postgres\" DataBase_name> \"E:\\pgBackup\\db_name.backup\"");
//passing the location for pg_dump, database to be backed and file name for backup
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
System.out.println(line);
}
}
Note :
1. You need to get the windows command line and then run the command for taking the back. This command can be changed for other databases.
Another Way to call pg_dump from Java Program :
PropertiesUtil propeties = new PropertiesUtil("myPropertyfile.properties");
String db_backup_file = propeties.getPropertyValue("dbbackup_file");
String password = propeties.getPropertyValue("password");
ProcessBuilder builder = new ProcessBuilder("C:\\Program Files\\PostgreSQL\\9.4\\bin\\pg_dump.exe",
"-v","-h", "localhost","-f",db_backup_file, "-U","postgres","DataBaseName");
builder.environment().put("PGPASSWORD",password);
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
System.out.println(line);
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.omg.CORBA.portable.InputStream;
public class dataBaseMigration {
public static void main(String[] args) throws Exception {
System.out.println("*********** Main Function *********** ");
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd \"C:\\Program Files\\PostgreSQL\\9.4\\bin\" && pg_dump --format=c
--username \"postgres\" DataBase_name> \"E:\\pgBackup\\db_name.backup\"");
//passing the location for pg_dump, database to be backed and file name for backup
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
System.out.println(line);
}
}
Note :
1. You need to get the windows command line and then run the command for taking the back. This command can be changed for other databases.
Another Way to call pg_dump from Java Program :
PropertiesUtil propeties = new PropertiesUtil("myPropertyfile.properties");
String db_backup_file = propeties.getPropertyValue("dbbackup_file");
String password = propeties.getPropertyValue("password");
ProcessBuilder builder = new ProcessBuilder("C:\\Program Files\\PostgreSQL\\9.4\\bin\\pg_dump.exe",
"-v","-h", "localhost","-f",db_backup_file, "-U","postgres","DataBaseName");
builder.environment().put("PGPASSWORD",password);
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
System.out.println(line);
}
No comments:
Post a Comment