sftpget_and_loaddump.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. *
  19. * Get a distant dump file and load it into a mysql database
  20. */
  21. $sapi_type = php_sapi_name();
  22. $script_file = basename(__FILE__);
  23. $path=dirname(__FILE__).'/';
  24. // Test if batch mode
  25. if (substr($sapi_type, 0, 3) == 'cgi') {
  26. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  27. exit;
  28. }
  29. // Global variables
  30. $error=0;
  31. $sourceserver=isset($argv[1])?$argv[1]:''; // user@server:/src/file
  32. $password=isset($argv[2])?$argv[2]:'';
  33. $dataserver=isset($argv[3])?$argv[3]:'';
  34. $database=isset($argv[4])?$argv[4]:'';
  35. $loginbase=isset($argv[5])?$argv[5]:'';
  36. $passwordbase=isset($argv[6])?$argv[6]:'';
  37. // Include Dolibarr environment
  38. $res=0;
  39. if (! $res && file_exists($path."../../master.inc.php")) $res=@include $path."../../master.inc.php";
  40. if (! $res && file_exists($path."../../htdocs/master.inc.php")) $res=@include $path."../../htdocs/master.inc.php";
  41. if (! $res && file_exists("../master.inc.php")) $res=@include "../master.inc.php";
  42. if (! $res && file_exists("../../master.inc.php")) $res=@include "../../master.inc.php";
  43. if (! $res && file_exists("../../../master.inc.php")) $res=@include "../../../master.inc.php";
  44. if (! $res && preg_match('/\/nltechno([^\/]*)\//', $_SERVER["PHP_SELF"], $reg)) $res=@include $path."../../../dolibarr".$reg[1]."/htdocs/master.inc.php"; // Used on dev env only
  45. if (! $res && preg_match('/\/nltechno([^\/]*)\//', $_SERVER["PHP_SELF"], $reg)) $res=@include "../../../dolibarr".$reg[1]."/htdocs/master.inc.php"; // Used on dev env only
  46. if (! $res) die("Failed to include master.inc.php file\n");
  47. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  48. /*
  49. * Main
  50. */
  51. $login='';
  52. $server='';
  53. if (preg_match('/^(.*)@(.*):(.*)$/', $sourceserver, $reg))
  54. {
  55. $login=$reg[1];
  56. $server=$reg[2];
  57. $sourcefile=$reg[3];
  58. $targetfile=basename($sourcefile);
  59. }
  60. if (empty($sourceserver) || empty($server) || empty($login) || empty($sourcefile) || empty($password) || empty($database) || empty($loginbase) || empty($passwordbase))
  61. {
  62. print "Usage: $script_file login@server:/src/file.(sql|gz|bz2) passssh databaseserver databasename loginbase passbase\n";
  63. print "Return code: 0 if success, <>0 if error\n";
  64. print "Warning, this script may take a long time.\n";
  65. exit(-1);
  66. }
  67. $targetdir='/tmp';
  68. print "Get dump file from server ".$server.", path ".$sourcefile.", connect with login ".$login." loaded into localhost\n";
  69. $sftpconnectstring=$sourceserver;
  70. print 'SFTP connect string : '.$sftpconnectstring."\n";
  71. //print 'SFTP password '.$password."\n";
  72. // SFTP connect
  73. if (! function_exists("ssh2_connect")) {
  74. dol_print_error('', 'ssh2_connect function does not exists'); exit(1);
  75. }
  76. $connection = ssh2_connect($server, 22);
  77. if ($connection)
  78. {
  79. if (! @ssh2_auth_password($connection, $login, $password))
  80. {
  81. dol_syslog("Could not authenticate with username ".$login." . and password ".preg_replace('/./', '*', $password), LOG_ERR);
  82. exit(-5);
  83. }
  84. else
  85. {
  86. //$stream = ssh2_exec($connection, '/usr/bin/php -i');
  87. /*
  88. print "Generate dump ".$filesys1.'.bz2'."\n";
  89. $stream = ssh2_exec($connection, "mysqldump -u debian-sys-maint -p4k9Blxl2snq4FHXY -h 127.0.0.1 --single-transaction -K --tables -c -e --hex-blob --default-character-set=utf8 saasplex | bzip2 -1 > ".$filesys1.'.bz2');
  90. stream_set_blocking($stream, true);
  91. // The command may not finish properly if the stream is not read to end
  92. $output = stream_get_contents($stream);
  93. */
  94. $sftp = ssh2_sftp($connection);
  95. print 'Get file '.$sourcefile.' into '.$targetdir.$targetfile."\n";
  96. ssh2_scp_recv($connection, $sourcefile, $targetdir.$targetfile);
  97. $fullcommand="cat ".$targetdir.$targetfile." | mysql -h".$databaseserver." -u".$loginbase." -p".$passwordbase." -D ".$database;
  98. if (preg_match('/\.bz2$/', $targetfile))
  99. {
  100. $fullcommand="bzip2 -c -d ".$targetdir.$targetfile." | mysql -h".$databaseserver." -u".$loginbase." -p".$passwordbase." -D ".$database;
  101. }
  102. if (preg_match('/\.gz$/', $targetfile))
  103. {
  104. $fullcommand="gzip -d ".$targetdir.$targetfile." | mysql -h".$databaseserver." -u".$loginbase." -p".$passwordbase." -D ".$database;
  105. }
  106. print "Load dump with ".$fullcommand."\n";
  107. $output=array();
  108. $return_var=0;
  109. print strftime("%Y%m%d-%H%M%S").' '.$fullcommand."\n";
  110. exec($fullcommand, $output, $return_var);
  111. foreach($output as $line) print $line."\n";
  112. //ssh2_sftp_unlink($sftp, $fileinstalllock);
  113. //print $output;
  114. }
  115. }
  116. else
  117. {
  118. print 'Failed to connect to ssh2 to '.$server;
  119. exit(-6);
  120. }
  121. exit(0);