This repository has been archived by the owner on Sep 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
syncr.php
executable file
·341 lines (285 loc) · 10.1 KB
/
syncr.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env php
<?php
/*
+----------------------------------------------------------------------+
| Syncr - Easy files and MySQL databases synchronization with PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2015 Junior Grossi |
| Authors: Junior Grossi <juniorgro@gmail.com> |
+----------------------------------------------------------------------+
*/
namespace syncr;
/**
* Check for command line options
*/
$options = getopt('', array('up', 'down', 'database', 'help'));
/**
* Check if --help option exists in the command
*/
if (isset($options['help'])) {
echo <<<MESSAGE
Synopsis:
php syncr [options]
Options:
--up Synchronize local files with remote server
--down Synchronize remote files with local server
--database Synchronize also the MySQL database
MESSAGE;
exit();
}
/**
* Check for the synchronization direction (up or down)
*/
if (!isset($options['up']) and !isset($options['down'])) {
echo <<<MESSAGE
+-----------------------------------------------------------+
| ! ERROR ! |
| You have to specify --up or --down options to start the |
| synchronization process. |
+-----------------------------------------------------------+
MESSAGE;
exit();
} else {
if (isset($options['up'])) {
$direction = 'up';
} elseif (isset($options['down'])) {
$direction = 'down';
}
}
if (isset($options['database'])) {
$sync_database = true;
} else $sync_database = false;
/**
* Check if the config file exists
*/
$file = 'syncr.json';
if (file_exists(__DIR__.'/'.$file)) {
$json = file_get_contents(__DIR__.'/'.$file);
$config = json_decode($json, true);
} else {
echo <<<MESSAGE
+-----------------------------------------------------------+
| ! ERROR ! |
| Missing the configuration file. |
| Ensure you have a syncr.json file with the appropriate |
| configurations variables. |
+-----------------------------------------------------------+
MESSAGE;
exit();
}
/**
* Check if the config file is valid
*/
if (!config_is_valid($config)) {
echo <<<MESSAGE
+-----------------------------------------------------------+
| ! ERROR ! |
| The configuration file does not have the required |
| variables. |
+-----------------------------------------------------------+
MESSAGE;
exit();
}
/**
* Check if all commands exist
*/
if (!commands_check()) {
echo <<<MESSAGE
+-----------------------------------------------------------+
| ! ERROR ! |
| Ensure you have all the following commands installed in |
| your machine: rsync, ssh, sshpass, mysqldump, gzip and |
| scp. |
+-----------------------------------------------------------+
MESSAGE;
exit();
}
/**
* Continue with normal process
*/
sync_files($config, $direction);
if ($sync_database) {
sync_database($config, $direction);
}
echo "---> Synchronization finished.\n";
/**
* Synchronization functions
*/
function sync_files($config, $direction = ' up')
{
$remote = $config['remote']['server'];
$local = $config['local']['server'];
$password = $remote['password'];
$ssh_port = (int) $remote['ssh_port'];
$from = trim($local['path'], '/').'/';
$username = $remote['username'];
$host = $remote['host'];
$remote_path = trim($remote['path'], '/').'/';
$remote_string = $username.'@'.$host.':'.$remote_path;
$exclude = exclude_files_command($config, $direction);
if ($direction == 'up') {
echo "---> Sending files to remote server...\n";
if ($password) {
$command = 'sshpass -p "%s" rsync -zavP -e "ssh -p %d" %s "%s" "%s"';
$command = sprintf($command, $password, $ssh_port, $exclude, $from, $remote_string);
} else {
$command = 'rsync -zavP -e "ssh -p %d" %s "%s" "%s"';
$command = sprintf($command, $ssh_port, $exclude, $from, $remote_string);
}
} elseif ($direction == 'down') {
echo "---> Getting files from remote server...\n";
if ($password) {
$command = 'sshpass -p "%s" rsync -zavP -e "ssh -p %d" %s "%s" "%s"';
$command = sprintf($command, $password, $ssh_port, $exclude, $remote_string, $from);
} else {
$command = 'rsync -zavP -e "ssh -p %d" %s "%s" "%s"';
$command = sprintf($command, $ssh_port, $exclude, $remote_string, $from);
}
}
shell_exec($command);
}
function sync_database($config, $direction = 'up')
{
if ($direction == 'up') {
sync_database_up($config);
} elseif ($direction == 'down') {
sync_database_down($config);
}
}
function sync_database_up($config)
{
$username = $config['local']['database']['username'];
$password = $config['local']['database']['password'];
$name = $config['local']['database']['name'];
$sshpass = '';
$filename = 'mysqldump_'.uniqid();
$command = 'mysqldump -u%s -p%s %s > %s.sql';
$command = sprintf($command, $username, $password, $name, $filename);
echo "---> Backing up local MySQL database...\n";
echo shell_exec($command);
$command = sprintf('gzip %s.sql', $filename);
echo shell_exec($command);
$username = $config['remote']['server']['username'];
$password = $config['remote']['server']['password'];
$host = $config['remote']['server']['host'];
$ssh_port = $config['remote']['server']['ssh_port'];
$command = 'scp -P $ssh_port ./%s.sql.gz %s@%s:%s.sql.gz';
$command = sprintf($command, $filename, $username, $host, $filename);
echo "---> Copying local SQL file to remote server...\n";
echo shell_exec($command);
echo shell_exec("rm $filename.sql.gz");
$db_username = $config['remote']['database']['username'];
$db_password = $config['remote']['database']['password'];
$db_name = $config['remote']['database']['name'];
if ($password) {
$sshpass = sprintf('sshpass -p "%s" ', $password);
}
echo "---> Importing database into remove server...\n";
$command = <<<COMMAND
{$sshpass}ssh -q -t -p $ssh_port $username@$host << ENDSSH
gzip -d $filename.sql.gz
mysql -u$db_username -p$db_password $db_name < $filename.sql
rm $filename.sql
ENDSSH
COMMAND;
exec($command);
}
function sync_database_down($config)
{
$username = $config['remote']['server']['username'];
$password = $config['remote']['server']['password'];
$host = $config['remote']['server']['host'];
$ssh_port = $config['remote']['server']['ssh_port'];
$sshpass = '';
$db_username = $config['remote']['database']['username'];
$db_password = $config['remote']['database']['password'];
$db_name = $config['remote']['database']['name'];
$filename = 'mysqldump_'.uniqid();
if ($password) {
$sshpass = sprintf('sshpass -p "%s" ', $password);
}
$command = <<<COMMAND
{$sshpass}ssh -q -p $ssh_port $username@$host << ENDSSH
mysqldump -u$db_username -p$db_password $db_name > $filename.sql
gzip $filename.sql
ENDSSH
COMMAND;
echo "---> Backing up remote MySQL database...\n";
exec($command);
$command = <<<COMMAND
{$sshpass}scp -P $ssh_port $username@$host:{$filename}.sql.gz ./
gzip -d {$filename}.sql.gz
COMMAND;
echo "---> Copying remote dump file to local server...\n";
echo shell_exec($command);
$db_username = $config['local']['database']['username'];
$db_password = $config['local']['database']['password'];
$db_name = $config['local']['database']['name'];
$command = "mysql -u$db_username -p$db_password $db_name < $filename.sql";
echo "---> Importing SQL dump file to local database...\n";
exec($command);
$command = <<<COMMAND
rm $filename.sql
{$sshpass}ssh -q -p $ssh_port $username@$host << ENDSSH
rm $filename.sql.gz
ENDSSH
COMMAND;
echo "---> Cleaning dump files on local and remote servers...\n";
exec($command);
}
function config_is_valid($config)
{
return isset($config['remote']) and
isset($config['remote']['server']) and
isset($config['remote']['server']['host']) and
isset($config['remote']['server']['username']) and
isset($config['remote']['server']['password']) and
isset($config['remote']['server']['ssh_port']) and
isset($config['remote']['server']['path']) and
isset($config['remote']['database']) and
isset($config['remote']['database']['name']) and
isset($config['remote']['database']['username']) and
isset($config['remote']['database']['password']) and
isset($config['local']) and
isset($config['local']['server']) and
isset($config['local']['server']['path']) and
isset($config['local']['database']) and
isset($config['local']['database']['name']) and
isset($config['local']['database']['username']) and
isset($config['local']['database']['password']);
}
function exclude_files_command($config, $direction)
{
if ($direction == 'up') {
if (isset($config['remote']['ignore_from_local'])) {
$files = $config['remote']['ignore_from_local'];
}
} elseif ($direction == 'down') {
if (isset($config['local']['ignore_from_remote'])) {
$files = $config['local']['ignore_from_remote'];
}
}
if (count($files) == 0) {
return '';
}
$cmd = '';
foreach ($files as $file) {
$cmd .= '--exclude '.$file.' ';
}
return trim($cmd);
}
function commands_check()
{
$commands = array('rsync', 'ssh', 'sshpass', 'mysqldump', 'gzip', 'scp');
foreach ($commands as $command) {
if (!command_exists($command)) {
return false;
}
}
return true;
}
function command_exists($command)
{
$return = shell_exec("which $command");
return !empty($return);
}