1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\BackupHandlers\Database\Databases; |
4
|
|
|
|
5
|
|
|
use Spatie\Backup\Console; |
6
|
|
|
|
7
|
|
|
class PgSQLDatabase implements DatabaseInterface |
8
|
|
|
{ |
9
|
|
|
protected $console; |
10
|
|
|
protected $database; |
11
|
|
|
protected $schema; |
12
|
|
|
protected $username; |
13
|
|
|
protected $password; |
14
|
|
|
protected $host; |
15
|
|
|
protected $port; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Console $console |
19
|
|
|
* @param $database |
20
|
|
|
* @param string $schema |
21
|
|
|
* @param string $username |
22
|
|
|
* @param $password |
23
|
|
|
* @param string string $host |
24
|
|
|
* @param int $port |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Console $console, $database, $schema, $username, $password, $host, $port) |
27
|
|
|
{ |
28
|
|
|
$this->console = $console; |
29
|
|
|
$this->database = $database; |
30
|
|
|
$this->schema = $schema; |
31
|
|
|
$this->username = $username; |
32
|
|
|
$this->password = $password; |
33
|
|
|
$this->host = $host; |
34
|
|
|
$this->port = $port; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a database dump. |
39
|
|
|
* |
40
|
|
|
* @param $destinationFile |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function dump($destinationFile) |
45
|
|
|
{ |
46
|
|
|
$command = sprintf('export PGHOST && %spg_dump ' . (!$this->useCopy() ? '--inserts' : '') . ' --schema=%s %s > %s', |
47
|
|
|
$this->getDumpCommandPath(), |
48
|
|
|
escapeshellarg($this->schema), |
49
|
|
|
escapeshellarg($this->database), |
50
|
|
|
escapeshellarg($destinationFile) |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$env = [ |
54
|
|
|
'PGHOST' => $this->host, |
55
|
|
|
'PGUSER' => $this->username, |
56
|
|
|
'PGPASSWORD' => $this->password, |
57
|
|
|
'PGPORT' => $this->port, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
return $this->console->run($command, config('laravel-backup.pgsql.timeoutInSeconds'), $env); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the file extension of a dump file (sql, ...). |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getFileExtension() |
69
|
|
|
{ |
70
|
|
|
return 'sql'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the path to the pgsql_dump. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
protected function getDumpCommandPath() |
79
|
|
|
{ |
80
|
|
|
$path = config('laravel-backup.pgsql.dump_command_path') |
81
|
|
|
|
82
|
|
|
if ($path != '') { |
|
|
|
|
83
|
|
|
$path = str_finish($path, '/'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $path; |
87
|
|
|
|
88
|
|
|
return $path; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Determine if COPY should be used instead of INSERT. |
93
|
|
|
*/ |
94
|
|
|
protected function useCopy() |
95
|
|
|
{ |
96
|
|
|
return config('laravel-backup.pgsql.use_copy'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|