Advertisement
Guest User

Untitled

a guest
Aug 4th, 2016
1,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * This script enables you to copy the structure and content of databases back and
  5. * forth between remote and local databases. Remote databases are accessed through
  6. * SSH tunnels.
  7. *
  8. * This script operates in two modes. The mode is passed in the first parameter
  9. * -- put: Copy a local database to a remote server
  10. * -- get: Copy a remote database to a local server
  11. *
  12. * When using a config.ini file to specify your database credentials the format is:
  13. *
  14. * [remote]
  15. * ssh = user@server.com
  16. * username = remoteDbUsername
  17. * password = remoteDbPassword
  18. * database = remoteDbName
  19. *
  20. * [local]
  21. * username = localDbUsername
  22. * password = localDbPassword
  23. * database = localDbName
  24. *
  25. *
  26. * Example usage: Replace your local database with a remote database
  27. * dbsync.php get
  28. *
  29. * Example usage: Replace a remote database with your local database
  30. * dbsync.php put
  31. *
  32. * Example usage using extrenal config file: Replace a remote database with your local database
  33. * dbsync.php put path/to/config.ini
  34. */
  35.  
  36. /**
  37. * If you have your .ssh/config file setup you can use this syntax
  38. * $sshServer = 'server';
  39. * If you are not using your .ssh/config file you should use this syntax
  40. * $sshServer = 'user@server.com
  41.  
  42. // SSH server connection string
  43. $sshServer = 'user@host.com';
  44.  
  45. // Remote database configuration
  46. $rDbUser = 'remoteDbUsername';
  47. $rDbPass = 'remoteDbPassword';
  48. $rDbName = 'remoteDbName';
  49.  
  50. // Local database configuration
  51. $lDbUser = 'localDbUsername';
  52. $lDbPass = 'localDbPassword';
  53. $lDbName = 'localDbName';
  54. */
  55.  
  56. /** End of configuration settings. Do not edit below this line **/
  57.  
  58. $mode = $argv[1];
  59.  
  60. if(isset($argv[2])) {
  61. $config = $argv[2];
  62. if(file_exists($config)) {
  63. $ini = parse_ini_file($config, true);
  64. $sshServer = $ini['remote']['ssh'];
  65. $rDbUser = $ini['remote']['username'];
  66. $rDbPass = $ini['remote']['password'];
  67. $rDbName = $ini['remote']['database'];
  68. $lDbUser = $ini['local']['username'];
  69. $lDbPass = $ini['local']['password'];
  70. $lDbName = $ini['local']['database'];
  71. }
  72. }
  73.  
  74. if($mode == 'get') {
  75. $source = "ssh $sshServer" . ' "' . "mysqldump -u $rDbUser --password=$rDbPass $rDbName" . '" ';
  76. $target = "mysql -u $lDbUser --password=$lDbPass --host=localhost -C $lDbName";
  77. $cmd = " $source | $target";
  78. echo "Replacing $lDbName on localhost with $rDbName from $sshServer\n\n";
  79. echo `$cmd`;
  80. }
  81. elseif($mode == 'put') {
  82. $source = "mysqldump -u $lDbUser --password=$lDbPass --host=localhost -C $lDbName";
  83. $target = "ssh $sshServer" . ' "' . "mysql -u $rDbUser --password=$rDbPass $rDbName" . '"';
  84. $cmd = $source . ' | ' . $target;
  85. echo "Replacing $rDbName on $sshServer with $lDbName on localhost\n\n";
  86. echo `$cmd`;
  87. }
  88. echo "\n\nTransfer complete.\n\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement