I
I
IBM7012014-05-24 19:10:26
MySQL
IBM701, 2014-05-24 19:10:26

How to backup MySQL database from a remote server in Java

Good day, there is a MySQL database on a remote server, such as almost all web hosts provide. Accordingly, from it there is only a host, the name of the database, login and password. The task is to make a backup of this database using Java. Using JDBC, connecting to it is not a problem, but here's how to make a backup (backup.sql) and save it to the local computer?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kozhin, 2014-05-25
@IBM701

Since MySQL has a standard mysqldump tool for this purpose, in your case with Java there are two options.
1) Connect to the database and do SHOW TABLES
2) For each table, do SHOW CREATE TABLE table_name to get a SQL query to create the same table.
3) For each table, do a SELECT to get data, from which you then form INSERT queries.
4) Save it all to a file - and here it is your backup.
1) Run the mysqldump utility , if installed on the server where you are running the Java program, using Runtime.getRuntime().exec- www.mkyong.com/java/how-to-execute-shell-command-f...
If you had a similar task in PHP, then I would recommend using this wonderful utility: https://github.com/clouddueling/mysqldump-php

A
a_f0x, 2014-05-26
@a_f0x

1) Connect to the database and do SHOW TABLES
2) For each table, do SHOW CREATE TABLE table_name to get a SQL query to create the same table.
3) For each table, do a SELECT to get the data from which you then form INSERT queries.
4) Save it all to a file - and here it is your backup.

And what about connections?
with foreign keys?
if there are views?
if temporary tables?
stored procedures?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question