1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cornford\Backup\Engines; |
4
|
|
|
|
5
|
|
|
class BackupEngineSqlsrv extends BackupEngineAbstract |
6
|
|
|
{ |
7
|
|
|
private const ENGINE_NAME = 'sqlsrv'; |
8
|
|
|
private const ENGINE_EXTENSION = 'bak'; |
9
|
|
|
private const ENGINE_EXPORT_PROCESS = 'SqlCmd'; |
10
|
|
|
private const ENGINE_RESTORE_PROCESS = 'SqlCmd'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Get export process. |
14
|
|
|
* |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public function getExportProcess() |
18
|
|
|
{ |
19
|
|
|
return self::ENGINE_EXPORT_PROCESS; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get restore process. |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function getRestoreProcess() |
28
|
|
|
{ |
29
|
|
|
return self::ENGINE_RESTORE_PROCESS; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get database file extension. |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getFileExtension() |
38
|
|
|
{ |
39
|
|
|
return self::ENGINE_EXTENSION; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Export the database to a file path. |
44
|
|
|
* |
45
|
|
|
* @param string $filepath |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function export($filepath) |
50
|
|
|
{ |
51
|
|
|
$command = sprintf( |
52
|
|
|
'%s -E -S %s –Q "BACKUP DATABASE %s TO DISK=\'%s\'"', |
53
|
|
|
$this->getExportCommand(), |
54
|
|
|
escapeshellarg($this->getHostname()), |
55
|
|
|
escapeshellarg($this->getDatabase()), |
56
|
|
|
escapeshellarg($filepath) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return $this->getBackupProcess()->run($command, __FUNCTION__); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Restore the database from a file path. |
64
|
|
|
* |
65
|
|
|
* @param string $filepath |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function restore($filepath) |
70
|
|
|
{ |
71
|
|
|
$command = sprintf( |
72
|
|
|
'%s -E -S %s –Q "RESTORE DATABASE %s FROM DISK=\'%s\'"', |
73
|
|
|
$this->getRestoreCommand(), |
74
|
|
|
escapeshellarg($this->getHostname()), |
75
|
|
|
escapeshellarg($this->getDatabase()), |
76
|
|
|
escapeshellarg($filepath) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
return $this->getBackupProcess()->run($command, __FUNCTION__); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|