0

データベースのバックアップを取ろうとしていますが、同じバックアップ ファイルを復元できません。これがバックアップコードです。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.swing.JDialog;
import javax.swing.JFileChooser;

class Backupsql extends JDialog {

    public boolean backupDB(String dbUser, String dbPass, String dbName) {
        String sourcePath = null;
        boolean result = false;
        JFileChooser fc = new JFileChooser();
        int i = fc.showSaveDialog(Backupsql.this);
        File file = fc.getSelectedFile();
        sourcePath = file.getPath();
        String filename = file.getName();
        sourcePath = sourcePath + ".sql";

        String executeCmd =
            "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump -u "
            + dbUser + " -p" + dbPass + " " + dbName + " -r " + sourcePath;

        Process runtimeProcess;
        try {
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();
            if (processComplete == 0) {
                System.out.println("Backup created successfully");
                compressFile(sourcePath);
                result = true;
            } else {
                System.out.println("Could not create the backup");
            }
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error" + e);
        }
        return result;
    }

    public static void compressFile(String srcFile) {
        byte[] buffer = new byte[1024];
        try {
            String destPath = srcFile.replace(".sql", ".zip");
            FileOutputStream fos = new FileOutputStream(destPath);
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = new ZipEntry(srcFile);
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream(srcFile);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            in.close();
            zos.closeEntry();
            zos.close();
            System.out.println("Done");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

これが私が使用している復元コマンドです。

String[] executeCmd = new String[]{
    "C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql.exe", "--user="
    + user, "--password=" + password, "-e", " source " + Unzippath};

私のコードは正しいですか?いくつかの提案を得ることができますか?

4

1 に答える 1

1

MySQL Backup/Restoreを調べてください。次の SQL は、JDBC で簡単に実行でき、より一般的です。また、完全バックアップの処理目的としています。

LOAD DATA INFILE '...'
于 2013-01-24T11:01:16.233 に答える