backup-tool.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. *
  4. * @global type $CONFIG
  5. * @return string filename of a new backup or false
  6. */
  7. function backup_tool_create_backup($options = array()) {
  8. /*
  9. * Create a new backup file
  10. */
  11. global $CONFIG;
  12. $dbuser = $CONFIG->dbuser; //get database user
  13. $dbpass = $CONFIG->dbpass; //get database password
  14. $dbname = $CONFIG->dbname; //get database name
  15. $dbhost = $CONFIG->dbhost;
  16. $datafolder = in_array('data', $options) ? elgg_get_data_path() : ''; //get path to sndata folder
  17. $rootfolder = in_array('site', $options) ? elgg_get_root_path() : ''; //get path to Elgg folder
  18. //get path to default backup dir specified in plugin settings
  19. $backup_dir = elgg_get_plugin_setting('backup_dir', 'backup-tool');
  20. $backip_file_name = elgg_get_plugin_setting("backup_name", 'backup-tool', 'backup') . '-' . date("Ymd");
  21. $backup_data_dir = $backup_dir.$backip_file_name."/";
  22. mkdir($backup_data_dir);
  23. $dump_path = '';
  24. //prepeare database dump
  25. if (in_array('db', $options)) {
  26. $backup_dump_dir = $backup_data_dir."dump/";
  27. mkdir($backup_dump_dir);
  28. $dump_name = $dbname . '.sql';
  29. $dump_path = $backup_dump_dir . $dump_name;
  30. $dump_command = "mysqldump --user={$dbuser} --password={$dbpass} --host={$dbhost} --databases {$dbname} > {$dump_path}";
  31. shell_exec($dump_command);
  32. sleep(1);
  33. //make a tar file for a dump
  34. //$cmd = "cd {$backup_dump_dir} && tar -cf {$backip_file_name}.tar {$dump_name}";
  35. //$result = shell_exec($cmd);
  36. //unlink($dump_path);
  37. }
  38. //prepare dataroot
  39. if (in_array('data',$options)){
  40. $backup_dataroot_dir = $backup_data_dir."dataroot/";
  41. mkdir($backup_dataroot_dir);
  42. $cmd = "cd {$datafolder} && cp . {$backup_dataroot_dir} -R";
  43. shell_exec($cmd);
  44. }
  45. //prepare siteroot
  46. if (in_array('site',$options)){
  47. $backup_siteroot_dir = $backup_data_dir."siteroot/";
  48. mkdir($backup_dataroot_dir);
  49. $cmd = "cd {$rootfolder} && cp . {$backup_siteroot_dir} -R";
  50. shell_exec($cmd);
  51. }
  52. //prepare tar file
  53. //$tar_name = $backip_file_name . '.tar';
  54. // $cmd = "tar -cf {$backup_data_dir}{$tar_name} {$datafolder} {$rootfolder}";
  55. $cmd = "cd {$backup_dir} && tar -cf {$backip_file_name}.tar -C {$backip_file_name} .";
  56. $result = shell_exec($cmd);
  57. //remove temp folder
  58. $remove_command = "rm " . $backup_data_dir ." -R";
  59. shell_exec($remove_command);
  60. //if dump with such name already exists then remove it first
  61. if (file_exists($backup_dir . $backip_file_name.".tar.gz")) {
  62. $remove_command = "rm " . $backup_dir . $backip_file_name . ".tar.gz";
  63. shell_exec($remove_command);
  64. }
  65. //compress
  66. $gzip_command = "gzip {$backup_dir}{$backip_file_name}.tar";
  67. shell_exec($gzip_command);
  68. if (file_exists($backup_dir . $backip_file_name . ".tar.gz")) {
  69. //create info file
  70. $inifile = fopen($backup_dir . $backip_file_name . ".tar.gz.ini", "w+");
  71. $options_string = serialize($options);
  72. fputs($inifile, $options_string, strlen($options_string));
  73. fclose($inifile);
  74. //return name of the new created backup file
  75. return $backip_file_name . "tar.gz";
  76. }
  77. return false;
  78. }
  79. function backup_tool_restore_backup ($options = array()){
  80. global $CONFIG;
  81. $dbuser = $CONFIG->dbuser; //get database user
  82. $dbpass = $CONFIG->dbpass; //get database password
  83. $dbname = $CONFIG->dbname; //get database name
  84. $dbhost = $CONFIG->dbhost;
  85. //get path to default backup dir specified in plugin settings
  86. $backup_dir = elgg_get_plugin_setting('backup_dir', 'backup-tool');
  87. $backip_file_name = $options['file_name'];
  88. $backup_data_dir = $backup_dir.str_replace(".tar.gz","",$backip_file_name)."/";
  89. $backup_file_path = $backup_dir.$backip_file_name;
  90. mkdir($backup_data_dir);
  91. //extract files from backup file into the new backup's dir
  92. $cmd = "cd $backup_data_dir && tar xfz $backup_file_path";
  93. exec($cmd);
  94. //restore dump
  95. $dump_dir = $backup_data_dir."dump/";
  96. $dump_file_path = $dump_dir.$dbname.".sql";
  97. $cmd = "mysql --user={$dbuser} --password={$dbpass} --host={$dbhost} {$dbname} < {$dump_file_path}";
  98. exec($cmd);
  99. //restore data folder
  100. $data_dir = $backup_data_dir."dataroot/";
  101. if (is_dir($data_dir)){
  102. $datafolder = elgg_get_data_path();
  103. $cmd = "cd $data_dir && cp -R . $datafolder";
  104. exec($cmd);
  105. }
  106. //restore site folder
  107. $site_dir = $backup_data_dir."siteroot/";
  108. if (is_dir($site_dir)){
  109. $sitefolder = elgg_get_root_path();
  110. $cmd = "cd $site_dir && cp -R . $sitefolder";
  111. exec($cmd);
  112. }
  113. //remove backup's dir
  114. $cmd = "rm $backup_data_dir -R";
  115. exec($cmd);
  116. return true;
  117. }
  118. function backup_tool_cleanup($offset) {
  119. elgg_load_library("backup_tool");
  120. $backup_dir = elgg_get_plugin_setting('backup_dir', 'backup-tool');
  121. $dir = opendir($backup_dir);
  122. //get size of each backup and comare it with offset
  123. while ($file = readdir($dir)) {
  124. if ($file != '.' && $file != '..') {
  125. $filename = $backup_dir . $file;
  126. if (is_file($filename)) {
  127. //if differences between current time and creation time is greater than offset then remove file
  128. $current_time = time();
  129. $creation_time = filemtime($filename);
  130. if ($current_time - $creation_time >= $offset) {
  131. unlink($filename);
  132. if (file_exists($filename . ".ini")) {
  133. unlink($filename . ".ini");
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. *
  142. * @param type $filename - file for uploading
  143. * @param type $ftpfortest - ftp settings for test
  144. */
  145. function backup_tool_upload_to_ftp($filename = NULL, $ftpfortest = false) {
  146. if (!$ftpfortest) {
  147. //get ftp settings
  148. $ftp = unserialize(elgg_get_plugin_setting('ftp', 'backup-tool'));
  149. } else {
  150. $ftp = $ftpfortest;
  151. }
  152. //Set up a connection
  153. $conn = ftp_connect($ftp['host']) or die("Can not connect to " . $ftp['host']);
  154. // Login
  155. if (ftp_login($conn, $ftp['user'], $ftp['password'])) {
  156. echo elgg_echo('backup-tool:ftp:established');
  157. if (ftp_chdir($conn, $ftp['dir'])) {
  158. if (!$ftpfortest) {
  159. //try to upload file
  160. //get path to default backup dir specified in plugin settings
  161. $backup_dir = elgg_get_plugin_setting('backup_dir', 'backup-tool');
  162. $file_path = $backup_dir . $filename;
  163. if (ftp_put($conn, $filename, $file_path, FTP_BINARY)) {
  164. echo "successfully uploaded $filename\n";
  165. } else {
  166. echo "There was a problem while uploading $filename\n";
  167. }
  168. }
  169. } else {
  170. echo elgg_echo('backup-tool:ftp:failchdir');
  171. }
  172. } else {
  173. echo elgg_echo('backup-tool:ftp:notestablished');
  174. }
  175. // close the connection
  176. ftp_close($conn);
  177. }