简体   繁体   中英

Download mysql gzipped backup with phpseclib to local system

i am using phpseclib on my local pc to download mysqldummp from a remote server this is my code:

set_include_path( './phpseclib');
require_once 'NET/SSH2.php';
require_once 'NET/SFTP.php';
require_once 'Crypt/RSA.php';
require_once 'Math/BigInteger.php';

$privatekey = file_get_contents('/path/to/.ssh/id_rsa');

$rsa = new Crypt_RSA();
$rsa->setPassword('');

if ($rsa->loadKey($privatekey, CRYPT_RSA_PRIVATE_FORMAT_PKCS1 ) === false) {
exit ("private key loading failed!");

$ssh = new Net_SSH2('example.com');
if (!$ssh->login('root', $rsa)) {
    exit('Login Failed');
}

  ini_set('zlib.output_compression', 'Off'); // tried removing this line too

header("Content-disposition: attachment; filename=sql.tar.gz");
header("Content-Type: text/javascript");
header("Content-Encoding: application/x-gzip");
echo $ssh->exec('mysqldump -uuser -ppass dbname tablename | gzip -9');

when i open this page in my browser on my localhost it is prompting to download file sql.tar.gz , but file is not extracting it seems it is corrupt. can you guys plz tell me if i am doing anything wrong

  1. I think you might have better luck with header('Content-Encoding: gzip'); then with the Content-Encoding you are using.

  2. When I tried to do mysqldump -uuser -ppass dbname tablename | gzip -9 mysqldump -uuser -ppass dbname tablename | gzip -9 I got the following:

     gzip: compressed data not written to a terminal. Use -f to force compression. For help, type: gzip -h mysqldump: Got errno 32 on write 

    If you remove all the headers and do an strlen() on $ssh->exec() 's output, what do you get? I'm wondering if what you're getting back is even being compressed at all. If not then maybe try the -f option that the command I ran suggested.

  3. Your code can be cleaned up a lot.

     set_include_path( './phpseclib'); require_once 'NET/SSH2.php'; require_once 'NET/SFTP.php'; require_once 'Crypt/RSA.php'; require_once 'Math/BigInteger.php'; $privatekey = file_get_contents('/path/to/.ssh/id_rsa'); $rsa = new Crypt_RSA(); $rsa->setPassword(''); if ($rsa->loadKey($privatekey, CRYPT_RSA_PRIVATE_FORMAT_PKCS1 ) === false) { 

    Including NET/SSH2.php is unnecessary since NET/SFTP.php includes it. Also, including Math/BigInteger.php is unnecessary since both NET/SSH2.php and Math/BigInteger.php include it.

    Calling $rsa->setPassword(''); is also unnecessary since (I'm 80% confident) empty passwords are assumed by default and $rsa->loadKey($privatekey, CRYPT_RSA_PRIVATE_FORMAT_PKCS1 ) can

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