class-backup-engine-database-imysqldump.php
2 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
<?php
namespace HM\BackUpWordPress;
use Ifsnop\Mysqldump as IMysqldump;
/**
* Perform a database backup using the mysqldump-php library
*
* @see https://github.com/ifsnop/mysqldump-php
*/
class IMysqldump_Database_Backup_Engine extends Database_Backup_Engine {
public function __construct() {
parent::__construct();
}
/**
* Perform the database backupwordpress
*
* @return bool True if the backup completed successfully, else false.
*/
public function backup() {
try {
$dump = new IMysqldump\Mysqldump( $this->get_dsn(), $this->get_user(), $this->get_password(), $this->get_dump_settings() );
$dump->start( $this->get_backup_filepath() );
} catch ( \Exception $e ) {
$this->error( __CLASS__, $e->getMessage() );
}
return $this->verify_backup();
}
/**
* Get the settings for the database bump.
*
* @return array The array of database dump settings.
*/
public function get_dump_settings() {
/**
* Allow additional settings to be added.
*
* @param string[] $settings The array of settings.
* @todo can these be standardised across all database backup engines
*/
return apply_filters( 'hmbkp_imysqldump_command', array(
'default-character-set' => $this->get_charset(),
'hex-blob' => true,
'single-transaction' => defined( 'HMBKP_MYSQLDUMP_SINGLE_TRANSACTION' ) && HMBKP_MYSQLDUMP_SINGLE_TRANSACTION,
) );
}
/**
* Correctly calculates the DSN string for the various mysql
* connection variations including simplt hostname, non-standard ports
* and socket connections.
*
* @return string The DSN connection string
*/
public function get_dsn() {
$dsn = 'mysql:host=' . $this->get_host() . ';dbname=' . $this->get_name();
if ( $this->get_host() && $this->get_port() ) {
$dsn = 'mysql:host=' . $this->get_host() . ';port=' . $this->get_port() . ';dbname=' . $this->get_name();
} elseif ( $this->get_socket() ) {
$dsn = 'mysql:unix_socket=' . $this->get_socket() . ';dbname=' . $this->get_name();
}
return $dsn;
}
}