ChatSyncCommand.php
3.6 KB
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
<?php
/**
* +-----------------------------------------------------------------------------------------------------------------------
* Command :通聊天记录到本地服务器 Command
* +-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Console\Commands
* @package App\Console\Commands
* @author Richer <yangzi1028@163.com>
* @date 2023年4月26日14:08:55
* @copyright 2022-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Console\Commands;
use App\Models\Traits\RouteTrait;
use App\Models\Traits\ThirdPartyTrait;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Collective\Remote\RemoteFacade as SSH;
use Symfony\Component\Process\Process; // TODO: require this somehow?
class ChatSyncCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:sync {connection?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync a remote Laravel install\'s database';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$connection = $this->getConnection();
$this->line('Logging into '.$connection);
// grab the dump data
$sqlDump = '';
SSH::into($connection)->run([
'cd '.$this->getRemoteDirectory($connection),
'php artisan db:dump',
],
function($line) use (&$sqlDump){
$sqlDump .= $line;
});
$config = config('database.connections.mysql');
$command = $this->getMysqlImportCommand($config, $sqlDump);
//echo $command; die();
$process = new Process($command);
//$process->setTimeout(600); // 10 minutes, should be more?
$process->run();
if ($process->isSuccessful())
{
$this->line($process->getOutput());
}
else
{
$this->line($process->getErrorOutput());
}
}
/**
* Gets connection name from command argument or remote config default
*
* @return string
*/
protected function getConnection()
{
$argumentConnection = $this->argument('connection');
if(!$argumentConnection){
return config('remote.default');
}
}
/**
* Gets directory of Laravel install on remote machine
*
* @param string $connection
* @return string
*/
protected function getRemoteDirectory($connection)
{
return config('remote.connections.'.$connection.'.path');
}
/**
* Get import command
*
* @return string
*/
public function getMysqlImportCommand($config, $sqlDump)
{
$command = sprintf('mysql --user=%s --password=%s %s < %s',
escapeshellarg($config['username']),
escapeshellarg($config['password']),
escapeshellarg($config['database']),
$sqlDump
);
// $command = sprintf('mysql --user=%s --password=%s %s < %s',
// escapeshellarg($config['username']),
// escapeshellarg($config['password']),
// escapeshellarg($config['database']),
// $sqlDump
// );
return $command;
}
}