-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgam-db-backup-functions.php
230 lines (185 loc) · 5.49 KB
/
gam-db-backup-functions.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Backup and download database now
* @access public
* @return void
*/
function db_backup_now()
{
try
{
$host = DB_HOST;
$name = DB_NAME;
$user = DB_USER;
$pass = DB_PASSWORD;
$tables = '*';
$path_info = wp_upload_dir();
$mainDir=$path_info['basedir'].'/gam-db-backup';
if (!file_exists($mainDir))
wp_mkdir_p($mainDir);
$sqlfile =$mainDir . '/'.DB_NAME.'.sql';
if (file_exists(sqlfile))
unlink($sqlfile);
$filename=DB_NAME.'.sql';
$handle = fopen($path_info['basedir'].'/gam-db-backup/'.$filename,'w+');
$con = mysql_connect($host,$user,$pass);
mysql_select_db($name,$con);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$return = "";
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
//$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = mysql_real_escape_string($row[$j]);
$return .= (isset($row[$j])) ? '"'.$row[$j].'"' : '""';
if ($j<($num_fields-1)) {
$return .= ',';
}
}
$return.= ");\n";
}
$return.="\n";
}
fwrite($handle,$return);
fclose($handle);
// Uploading File to upload directory
$upload_path = array(
'filename' => ($filename),
'dir' => ($path_info['basedir'].'/gam-db-backup/'.$filename),
'url' => ($path_info['baseurl'].'/gam-db-backup/'.$filename),
'size' => 0
);
$upload_path['size']=filesize($upload_path['dir']);
$fullDownloadPath=$path_info['baseurl'].'/gam-db-backup/'.$filename;
echo json_encode(array('Success'=>true, 'FullDownloadPath'=>$fullDownloadPath));
wp_die();
}
catch(Exception $e)
{
$GLOBALS['errors'][] = $e;
}
}
/**
* Save selected schedule selected option either daily, weekly or monthly.
* It will save in database as value of gam_db_backup_schedules_options field.
* gam_db_backup_schedules_options is common id of the all radio buttons (daily, weekly, monthly).
* @access public
* @return void
*/
function save_selected_schedule_option()
{
if ( ! empty($_POST['selected_schedule_option']) )
{
update_option('gam_db_backup_schedules_options',$_POST['selected_schedule_option']);
echo json_encode(array('Success'=>true, 'Message'=>__('Your schedule settings successfully saved!', 'gam-db-backup' ) ));
wp_die();
}
}
/**
* Download database based on schedules like once daily, once weekly or once monthly.
* For Once daily : database will store at wp-contents/uploads/gam-db-backup/daily/
* For Once weekly : database will store at wp-contents/uploads/gam-db-backup/weekly/
* For Once monthly : database will store at wp-contents/uploads/gam-db-backup/monthly/
* @access public
* @return void
*/
function db_backup_schedules()
{
try
{
$host = DB_HOST;
$name = DB_NAME;
$user = DB_USER;
$pass = DB_PASSWORD;
$tables = '*';
$path_info = wp_upload_dir();
$mainDir=$path_info['basedir'].'/gam-db-backup';
if (!file_exists($mainDir))
wp_mkdir_p($mainDir);
$selected_schedule_option = get_option('gam_db_backup_schedules_options');
if ( empty($selected_schedule_option) || $selected_schedule_option=='none' )
return;
$selected_schedule_option_dir =$mainDir . '/'.$selected_schedule_option;
if (!file_exists($selected_schedule_option_dir))
wp_mkdir_p($selected_schedule_option_dir);
$filename=date("F j, Y, g:i a").'_'. DB_NAME .'.sql';
$handle = fopen($selected_schedule_option_dir.'/'.$filename,'w+');
$con = mysql_connect($host,$user,$pass);
mysql_select_db($name,$con);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$return = "";
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = mysql_real_escape_string($row[$j]);
$return .= (isset($row[$j])) ? '"'.$row[$j].'"' : '""';
if ($j<($num_fields-1))
{
$return .= ',';
}
}
$return.= ");\n";
}
$return.="\n";
}
fwrite($handle,$return);
fclose($handle);
// Uploading File to upload directory
$upload_path = array(
'filename' => ($filename),
'dir' => ($selected_schedule_option_dir.'/'.$filename),
'url' => ($selected_schedule_option_dir.'/'.$filename),
'size' => 0
);
$upload_path['size']=filesize($upload_path['dir']);
}
catch(Exception $e)
{
$GLOBALS['errors'][] = $e;
}
}
?>