sftpget_and_loaddump.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 3 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")) {
  40. $res = @include $path."../../master.inc.php";
  41. }
  42. if (!$res && file_exists($path."../../htdocs/master.inc.php")) {
  43. $res = @include $path."../../htdocs/master.inc.php";
  44. }
  45. if (!$res && file_exists("../master.inc.php")) {
  46. $res = @include "../master.inc.php";
  47. }
  48. if (!$res && file_exists("../../master.inc.php")) {
  49. $res = @include "../../master.inc.php";
  50. }
  51. if (!$res && file_exists("../../../master.inc.php")) {
  52. $res = @include "../../../master.inc.php";
  53. }
  54. if (!$res && preg_match('/\/nltechno([^\/]*)\//', $_SERVER["PHP_SELF"], $reg)) {
  55. $res = @include $path."../../../dolibarr".$reg[1]."/htdocs/master.inc.php"; // Used on dev env only
  56. }
  57. if (!$res && preg_match('/\/nltechno([^\/]*)\//', $_SERVER["PHP_SELF"], $reg)) {
  58. $res = @include "../../../dolibarr".$reg[1]."/htdocs/master.inc.php"; // Used on dev env only
  59. }
  60. if (!$res) {
  61. die("Failed to include master.inc.php file\n");
  62. }
  63. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  64. /*
  65. * Main
  66. */
  67. $login = '';
  68. $server = '';
  69. if (preg_match('/^(.*)@(.*):(.*)$/', $sourceserver, $reg)) {
  70. $login = $reg[1];
  71. $server = $reg[2];
  72. $sourcefile = $reg[3];
  73. $targetfile = basename($sourcefile);
  74. }
  75. if (empty($sourceserver) || empty($server) || empty($login) || empty($sourcefile) || empty($password) || empty($database) || empty($loginbase) || empty($passwordbase)) {
  76. print "Usage: $script_file login@server:/src/file.(sql|gz|bz2) passssh databaseserver databasename loginbase passbase\n";
  77. print "Return code: 0 if success, <>0 if error\n";
  78. print "Warning, this script may take a long time.\n";
  79. exit(-1);
  80. }
  81. $targetdir = '/tmp';
  82. print "Get dump file from server ".$server.", path ".$sourcefile.", connect with login ".$login." loaded into localhost\n";
  83. $sftpconnectstring = $sourceserver;
  84. print 'SFTP connect string : '.$sftpconnectstring."\n";
  85. //print 'SFTP password '.$password."\n";
  86. // SFTP connect
  87. if (!function_exists("ssh2_connect")) {
  88. dol_print_error('', 'ssh2_connect function does not exists');
  89. exit(1);
  90. }
  91. $connection = ssh2_connect($server, 22);
  92. if ($connection) {
  93. if (!@ssh2_auth_password($connection, $login, $password)) {
  94. dol_syslog("Could not authenticate with username ".$login." . and password ".preg_replace('/./', '*', $password), LOG_ERR);
  95. exit(-5);
  96. } else {
  97. //$stream = ssh2_exec($connection, '/usr/bin/php -i');
  98. /*
  99. print "Generate dump ".$filesys1.'.bz2'."\n";
  100. $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');
  101. stream_set_blocking($stream, true);
  102. // The command may not finish properly if the stream is not read to end
  103. $output = stream_get_contents($stream);
  104. */
  105. $sftp = ssh2_sftp($connection);
  106. print 'Get file '.$sourcefile.' into '.$targetdir.$targetfile."\n";
  107. ssh2_scp_recv($connection, $sourcefile, $targetdir.$targetfile);
  108. $fullcommand = "cat ".$targetdir.$targetfile." | mysql -h".$databaseserver." -u".$loginbase." -p".$passwordbase." -D ".$database;
  109. if (preg_match('/\.bz2$/', $targetfile)) {
  110. $fullcommand = "bzip2 -c -d ".$targetdir.$targetfile." | mysql -h".$databaseserver." -u".$loginbase." -p".$passwordbase." -D ".$database;
  111. }
  112. if (preg_match('/\.gz$/', $targetfile)) {
  113. $fullcommand = "gzip -d ".$targetdir.$targetfile." | mysql -h".$databaseserver." -u".$loginbase." -p".$passwordbase." -D ".$database;
  114. }
  115. print "Load dump with ".$fullcommand."\n";
  116. $output = array();
  117. $return_var = 0;
  118. print strftime("%Y%m%d-%H%M%S").' '.$fullcommand."\n";
  119. exec($fullcommand, $output, $return_var);
  120. foreach ($output as $line) {
  121. print $line."\n";
  122. }
  123. //ssh2_sftp_unlink($sftp, $fileinstalllock);
  124. //print $output;
  125. }
  126. } else {
  127. print 'Failed to connect to ssh2 to '.$server;
  128. exit(-6);
  129. }
  130. exit(0);