How can I backup and restore date from mysql database

I want to know how to back up from mysql database and restore it. I want to use it in my java application.

mysql> mysql -u root -p 123  -h hostname club <dumpfile.sql;

but he has this error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -u root -p mehdi  -h hostname club < dumpfile.sql' at line 1   
+2
source share
4 answers
import java.io.IOException;  
/**  
*  
* @author wakam.cha.hanba@gmail.com  
*/  

public class DatabaseManager {  

public static boolean backup(String mysqldumpPath, String backupPath) {
    boolean status = false;
    String username = "name";
    String password = "pword";
    String database = "database_name";


    String command = "/" + mysqldumpPath + "/mysqldump -u " + username + " -p" + password + " " + database + " -r " + backupPath;
    try {
        Process runtimeProcess = Runtime.getRuntime().exec(command);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("DatabaseManager.backup: Backup Successfull");
            status = true;
        } else {
            System.out.println("DatabaseManager.backup: Backup Failure!");
        }
    } catch (IOException ioe) {
        System.out.println("Exception IO");
        ioe.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
    return status;
}
public static boolean restore(String filePath){
    boolean status = false;
    String username = "name";
    String password = "pword";
    String[] command = new String[]{"mysql", "database_name", "-u" + username, "-p" + password, "-e", " source "+filePath };

    try {
        Process runtimeProcess = Runtime.getRuntime().exec(command);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("DatabaseManager.restore: Restore Successfull");
            status = true;
        } else {
            System.out.println("DatabaseManager.restore: Restore Failure!");
        }
    } catch (IOException ioe) {
        System.out.println("Exception IO");
        ioe.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
    return status;
}

//for testing
public static void main(String args[]){
    String backupName = "D:/DatabaseBackup/backupHvs.sql";
    DatabaseManager.restore(backupName);
}

}
+3
source

mysql -u root -p 123 -h hostname club < dumpfile.sql must be run on the command line in SO and NOT in the mysql console.

EDIT: in Java, you can invoke the SO process to do this, or use some library to back up / restore. Try running dbunit to complete these steps for the database. I used dbunit with sleep mode and worked fine.

+2
source

Linux :

mysqldump --add-drop-table -u<username> -p<password> <databasename> > dumpfile.sql

:

mysql -u<username> -p<password> <databasename> < dumpfile.sql
+1
    try {
        Runtime.getRuntime().exec("/C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump -uroot -p123456 computer -r D:\\zerso\\backup.sql");

        JOptionPane.showMessageDialog(null, "  make   back  up");
    } catch (IOException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "  not   back  up");
    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
0

Source: https://habr.com/ru/post/1777021/


All Articles