简体   繁体   中英

php mysqldump and zip 7z

I have this line of php backing up my databases, work's like a charm (Windows 7):

$exec_str = "{$real_path}Bin\mysqldump -h {$mysql_db_host} -u {$mysql_db_user} -p{$mysql_db_pasw} {$db_name} > {$backupdir}\\{$db_name}.sql";

But i would rather prefer to have the .sql files compressed with 7zip (as 7z)

This one will not work (path to 7za.exe is correct) Instead it generates empty 7z files:

$exec_str = "{$real_path}Bin\mysqldump -h {$mysql_db_host} -u {$mysql_db_user} -p{$mysql_db_pasw} {$db_name} | {$real_path}7za a > {$backupdir}\\{$db_name}.sql.7z";

What am i doing wrong?

I successfully used 7z to compress the output from mysqldump (Windows Server 2008 R2), but I used command line batch files and automated tasks instead. Also, I just used 7z.exe, not 7za.

Note the following commands are executed from the Windows command terminal, ie Start->Run-> cmd

I created a batch file for the automated task to run with the contents:

mysqldump -u<mysqlUserName> -p<mysqlPassword> <schemaName>|7z.exe a -si<nameInZip> <zippedPathAndName.7z> 

This creates a .7z file of the mysqldump. The [-si] switch specifies to take input from StdIn. The "nameInZip" is just what you want to appear in the zip file; I made mine "schemaName".sql.

NOTE: this assumes that the mysqldump and 7z executable directories are in the %PATH% Environment variable. Otherwise use the full path to the binaries, eg c:\\program files\\7-zip\\7z.exe instead of just 7z.exe

Then I created an automated task:

schtasks /create /sc DAILY /tn <yourTaskName> /ru System /tr <pathToYourBatchFile> /st 20:45

This creates an automated task that runs your batch file daily at 20:45. Note the "/ru System" switch which ensures that the command is run by SYSTEM and will run whether you're logged on or not.

See here for a good reference for schtasks command.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM