-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuslims_db_binary_backup.php
104 lines (78 loc) · 2.21 KB
/
uslims_db_binary_backup.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
<?php
$self = __FILE__;
$notes = <<<__EOD
usage: $self {remote_config_file}
creates a binary backup of the database
must be run as root
__EOD;
if ( count( $argv ) < 1 || count( $argv ) > 2 ) {
echo $notes;
exit;
}
$config_file = "db_config.php";
if ( count( $argv ) == 2 ) {
$use_config_file = $argv[ 1 ];
} else {
$use_config_file = $config_file;
}
if ( !file_exists( $use_config_file ) ) {
fwrite( STDERR, "$self:
$use_config_file does not exist
to fix:
cp ${config_file}.template $use_config_file
and edit with appropriate values
")
;
exit(-1);
}
require "utility.php";
file_perms_must_be( $use_config_file );
require $use_config_file;
if ( !is_admin() ) {
error_exit( "must run as root" );
}
open_db();
# get datadir
$res = db_obj_result( $db_handle, "show global variables like 'datadir'" );
$datadir = sprintf( "%s", $res->{'Value'} );
echoline( "=" );
echo "datadir is $datadir\n";
# newdir “mariadb-binary-backup’
newfile_dir_init( "mariadb-binary-backup" );
# qyn service mariadb stop
get_yn_answer( "Stop mariadb services (make sure no processes are actively updating!)", true );
run_cmd( "service mariadb stop" );
# verify db stopped
echo "Verifying db had stopped, expect a PHP Warning\n";
$db_handle = mysqli_connect( $dbhost, $user, $passwd );
if ( $db_handle ) {
error_exit( "Can still connect to database, somehow it didn't stop?" );
}
echo "OK: db appears to be stopped as I can not connect\n";
# deep copy of datadir to newfile-/
echoline( "=" );
echo "making copy of database datadir $datadir\n";
$debug = 1;
run_cmd( "cp -rp $datadir/* $newfile_dir" );
$debug = 0;
# qyn service mariadb start
if ( get_yn_answer( "Restart mariadb services? (processes might further update the db)" ) ) {
run_cmd( "service mariadb start" );
# make sure db is open
echo "verify db is running\n";
open_db();
echo "OK: db is running\n";
} else {
echo "WARNING: db is not running\n";
}
echoline('=');
echo
"backups are in:\n"
. "$newfile_dir\n"
. "restore by:\n"
. "service mariadb stop\n"
. "rm -fr $datadir/*\n"
. "cp -rp $newfile_dir/* $datadir\n"
. "chown -R mysql:mysql $datadir\n"
. "service mariadb start\n"
;