I want to backup my database by creating a zip file with qprocess in the QT program, in the code below it does it as a sql file. How can I make a backup inside the zip file?
QProcess dump(this);
QStringlist args;
QString path="C:/Users/ali/Desktop/dbfile/db.sql";
args<<"-uroot"<<"-proot"<<"denemesql";
dump.setStandardOutputFile(path);
dump.start("mysqldump.exe",args);
if(!dump.waitForStarted(1000))
{
qDebug()<<dump.errorString();
}
dump.waitForFinished(-1);
can you help me?
CodePudding user response:
The easy way is perform it in two steps: dump and then zip
bool dump1() {
QString path = "C:/Users/ali/Desktop/dbfile/db.sql";
QString zipPath = "C:/Users/ali/Desktop/dbfile/db.zip";
QProcess dump;
dump.setProgram("mysqldump.exe");
dump.setArguments({"-uroot", "-proot", "denemesql"});
dump.setStandardOutputFile(path);
dump.start();
if (!dump.waitForStarted()) {
qDebug() << dump.error();
return false;
}
if (!dump.waitForFinished()) {
qDebug() << dump.error();
return false;
}
QProcess zip;
zip.setProgram("C:\\Program Files\\7-Zip\\7z.exe");
zip.setArguments({"a", zipPath, path});
zip.start();
if (!zip.waitForStarted()) {
qDebug() << dump.error();
return false;
}
if (!zip.waitForFinished()) {
qDebug() << dump.error();
return false;
}
QFile(path).remove();
return true;
}
You can avoid creating temporary file piping ouput of dump directly to 7z,but you will be limited to gzip format.
bool dump2() {
QString gzipPath = "C:/Users/ali/Desktop/dbfile/db.gz";
QString fileName = "db.sql";
QProcess dump;
dump.setProgram("mysqldump.exe");
dump.setArguments({"-uroot", "-proot", "denemesql"});
QProcess zip;
zip.setProgram("C:\\Program Files\\7-Zip\\7z.exe");
zip.setArguments({"a", gzipPath, "-si" fileName});
dump.setStandardOutputProcess(&zip);
dump.start();
zip.start();
if (!dump.waitForStarted()) {
qDebug() << dump.error();
return false;
}
if (!zip.waitForStarted()) {
qDebug() << zip.error();
return false;
}
if (!dump.waitForFinished()) {
qDebug() << dump.error();
return false;
}
if (!zip.waitForFinished()) {
qDebug() << zip.error();
return false;
}
return true;
}
The hard way is to use zlib, you can add it to your project and use
zipOpen
zipOpenNewFileInZip
zipWriteInFileInZip
zipCloseFileInZip
zipClose
functions. This requires linking library and including headers and sources from zlib\contrib\minizip
.