-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdbsync.php
56 lines (48 loc) · 1.6 KB
/
dbsync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/php
<?php
/**
* If you have your .ssh/config file setup you can use this syntax
* $sshServer = 'server';
* If you are not using your .ssh/config file you should use this syntax
* $sshServer = '[email protected]
// SSH server connection string
$sshServer = '';
// Remote database configuration
$rDbUser = '';
$rDbPass = '';
$rDbName = '';
// Local database configuration
$lDbUser = '';
$lDbPass = '';
$lDbName = '';
*/
/** End of configuration settings. Do not edit below this line **/
$mode = $argv[1];
if(isset($argv[2])) {
$config = $argv[2];
if(file_exists($config)) {
$ini = parse_ini_file($config, true);
$sshServer = $ini['remote']['ssh'];
$rDbUser = $ini['remote']['username'];
$rDbPass = $ini['remote']['password'];
$rDbName = $ini['remote']['database'];
$lDbUser = $ini['local']['username'];
$lDbPass = $ini['local']['password'];
$lDbName = $ini['local']['database'];
}
}
if($mode == 'get') {
$source = "ssh $sshServer" . ' "' . "mysqldump -u $rDbUser --password=$rDbPass $rDbName" . '" ';
$target = "mysql -u $lDbUser --password=$lDbPass --host=localhost -C $lDbName";
$cmd = " $source | $target";
echo "Replacing $lDbName on localhost with $rDbName from $sshServer\n\n";
echo `$cmd`;
}
elseif($mode == 'put') {
$source = "mysqldump -u $lDbUser --password=$lDbPass --host=localhost -C $lDbName";
$target = "ssh $sshServer" . ' "' . "mysql -u $rDbUser --password=$rDbPass $rDbName" . '"';
$cmd = $source . ' | ' . $target;
echo "Replacing $rDbName on $sshServer with $lDbName on localhost\n\n";
echo `$cmd`;
}
echo "\n\nTransfer complete.\n\n";