Guest User

Untitled

a guest
Jul 3rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. $config = [
  4. 'host' => 'localhost',
  5. 'user' => 'root',
  6. 'pass' => '',
  7. 'db' => 'prestashop',
  8.  
  9. 'prefix' => 'ps_',
  10. 'tables' => [
  11. 'address',
  12. 'customer(.+|$)',
  13.  
  14. 'cart(.+|$)',
  15. 'message',
  16.  
  17. 'orders',
  18. 'order(.+)',
  19. 'paypal(.+)',
  20.  
  21. 'product',
  22. 'product_lang',
  23. 'specific_price'
  24. ],
  25.  
  26. 'name' => 'backup_%y%m%d_%H%i%s_%T.sql',
  27. 'path' => getcwd() .'/',
  28. 'gzip' => TRUE
  29. ];
  30.  
  31. $db = new mysqli($config['host'], $config['user'], $config['pass'], $config['db']);
  32.  
  33. if($db->connect_errno){
  34. error_log("ERROR connecting to DB: " .$db->connect_errno);
  35. exit(1);
  36. }
  37.  
  38. $tables = $db->query("SHOW TABLES");
  39. if($tables->num_rows == 0){
  40. error_log("No tables found.");
  41. exit(1);
  42. }
  43.  
  44. $backup_tables = array();
  45. $match = '/^' .$config['prefix'] .'(' .implode('|', $config['tables']) .')' .'$/';
  46.  
  47. while($table = $tables->fetch_row()){
  48. $table = current($table);
  49. if(preg_match($match, $table)){
  50. $backup_tables[] = $table;
  51. }
  52. }
  53.  
  54. if(count($backup_tables) == 0){
  55. echo("No tables found in pattern, not doing backup.");
  56. exit(0);
  57. }
  58.  
  59. echo "Preparing backup for " .count($backup_tables) ." tables...\n";
  60. $date_now = time();
  61.  
  62. foreach($backup_tables as $table){
  63. $file = $config['name'];
  64. $date_matches = "dDjlNSwzWFmMntLoYyaABgGhHisuveIOPZcr";
  65.  
  66. foreach(str_split($date_matches) as $m){
  67. $file = str_replace('%' .$m, date("$m", $date_now), $file);
  68. }
  69.  
  70. $file = str_replace("%T", $table, $file);
  71. echo "> " .$file ."\n";
  72.  
  73. $time = microtime(TRUE);
  74.  
  75. $cmd = "mysqldump"
  76. ." --host=" .$config['host']
  77. ." --user=" .$config['user']
  78. ." --password=" .$config['pass']
  79. ." " .$config['db']
  80. ." " .$table;
  81.  
  82. exec($cmd ." > " .$config['path'] .$file);
  83.  
  84. $time = floor((microtime(TRUE) - $time) * 1000);
  85. echo "Done in $time ms.";
  86.  
  87. if($config['gzip']){
  88. echo " Compressing... ";
  89. $time = microtime(TRUE);
  90.  
  91. exec("gzip " .$config['path'] .$file);
  92. $time = floor((microtime(TRUE) - $time) * 1000);
  93. echo "Done in $time ms.";
  94. }
  95.  
  96. echo "\n";
  97. }
  98. ?>
Add Comment
Please, Sign In to add comment