-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysqlBackupRestore.php
111 lines (98 loc) · 2.76 KB
/
MysqlBackupRestore.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
104
105
106
107
108
109
110
111
<?php
/**
* Make Sure the DB Dump has:
* DROP TABLE IF EXISTS `attempts`;
* CREATE TABLE `attempts` (
* otherwise, it may fall in error
* For Windows, mysql.exe full path may require
*/
//run backup from cPanel Cron job:
// mysql -h localhost -u [myDbUser] -p[MyPassword] [myDB] < /path/db_data.sql >/dev/null 2>&1
$host = "localhost";
$file = __DIR__ . '/backup/my_dump_data.sql';
$user = "user";
$pass = 'password'; /// empty password will cause issue.
$db = "my_dump_data";
/*
*
*/
//$cmd = "mysql -h {$host} -u {$user} -p{$pass} {$db} < $file";
//echo exec($cmd);
//echo "DONE";
//echo '<br />';
//echo $cmd;
$dbMan = new MysqlBackupRestore($user,$pass,$host);
$dbMan->backup($db);
$dbMan->restore($db,$file);
/**
* php class to backup and restore mysql database
*
* LICENSE: GNU GENERAL PUBLIC LICENSE
* Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*
* @category Class
* @package MakeWebSmart\MysqlBackupRestore
* @author Azraf <c.azraf@gmail.com>
* @copyright 1997-2005 The PHP Group
* @version 1.0
*/
/**
* This is a "Docblock Comment," also known as a "docblock." The class'
* docblock, below, contains a complete description of how to write these.
*/
class MysqlBackupRestore{
private $_user;
private $_pass;
private $_path;
private $_host = null;
private $_port = null;
public function __construct($user,$pass,$host='localhost',$port=false)
{
$this->_user = $user;
$this->_pass = $pass;
$this->_host = $host;
$this->_path = __DIR__ . '/backup/';
if($port){
$this->_port = $port;
}
}
public function backup($db,$path=false,$addTime=false)
{
if(!$path){
$path = $this->_path;
}
if(is_array($db)){
foreach($db as $v)
{
$this->_doBackup($v,$path,$addTime);
}
}else {
$this->_doBackup($db,$path,$addTime);
}
}
private function _doBackup($db,$path,$timer=false)
{
$time = ($timer) ? date("Y-m-d-H") : '';
$cmd = "mysqldump --routines -h {$this->_host} -u {$this->_user} -p{$this->_pass} {$db} > " . $path . "{$db}_{$time}.sql";
exec($cmd);
}
public function restore($db,$file)
{
if(is_array($db)){
foreach($db as $v)
{
$this->_doRestore($v,$file);
}
}else {
$this->_doRestore($db,$file);
}
}
private function _doRestore($db,$file)
{
$cmd = "mysql -h {$this->_host} -u {$this->_user} -p{$this->_pass} {$db} < $file";
exec($cmd);
}
}
?>