Created
April 2, 2015 07:03
-
-
Save rohitkothari/0f2cb2ef132a21d05ac1 to your computer and use it in GitHub Desktop.
Download a backup of MySQL schema and data from AWS RDS to local machine using mysqldump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
public class BackupAWSRDS { | |
public static void main(String[] args) { | |
BackupAWSRDS bckupRds = new BackupAWSRDS(); | |
// Make sure you replace the parameters in below String with actual RDS_IP_ADDRESS, RDS_USERNAME, RDS_PASSWORD, DB | |
String createBackupCommand = "/usr/local/mysql/bin/mysqldump -hRDS_IP_ADDRESS -uRDS_USERNAME -pRDS_PASSWORD --port=3306 --single-transaction --routines --triggers --databases DB -r /tmp/mysql-dump.sql"; | |
// This creates a MySQL dump file at the specified location - currently at /tmp/mysql-dump.sql - Feel free to change this location | |
String createBackupOutput = bckupRds.executeCreateBackupCommand(createBackupCommand); | |
System.out.println(createBackupOutput); | |
} | |
private String executeCreateBackupCommand(String command) { | |
StringBuffer output = new StringBuffer(); | |
Process p; | |
try { | |
p = Runtime.getRuntime().exec(command); | |
int exitval=p.waitFor(); | |
System.out.println("Backup exitval: "+exitval); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); | |
String line = ""; | |
while ((line = reader.readLine())!= null) { | |
output.append(line + "\n"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return output.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment