/
backupdb.php
147 lines (126 loc) · 3.03 KB
/
backupdb.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
<?php
/**
* MyBB 1.8
* Copyright 2014 MyBB Group, All Rights Reserved
*
* Website: http://www.mybb.com
* License: http://www.mybb.com/about/license
*
*/
function task_backupdb($task)
{
global $db, $config, $lang, $plugins;
static $contents;
@set_time_limit(0);
if(!defined('MYBB_ADMIN_DIR'))
{
if(!isset($config['admin_dir']))
{
$config['admin_dir'] = "admin";
}
define('MYBB_ADMIN_DIR', MYBB_ROOT.$config['admin_dir'].'/');
}
// Check if folder is writable, before allowing submission
if(!is_writable(MYBB_ADMIN_DIR."/backups"))
{
add_task_log($task, $lang->task_backup_cannot_write_backup);
}
else
{
$db->set_table_prefix('');
$file = MYBB_ADMIN_DIR.'backups/backup_'.date("_Ymd_His_").random_str(16);
if(function_exists('gzopen'))
{
$fp = gzopen($file.'.incomplete.sql.gz', 'w9');
}
else
{
$fp = fopen($file.'.incomplete.sql', 'w');
}
$tables = $db->list_tables($config['database']['database'], $config['database']['table_prefix']);
$time = date('dS F Y \a\t H:i', TIME_NOW);
$contents = "-- MyBB Database Backup\n-- Generated: {$time}\n-- -------------------------------------\n\n";
if(is_object($plugins))
{
$args = array(
'task' => &$task,
'tables' => &$tables,
);
$plugins->run_hooks('task_backupdb', $args);
}
foreach($tables as $table)
{
$field_list = array();
$fields_array = $db->show_fields_from($table);
foreach($fields_array as $field)
{
$field_list[] = $field['Field'];
}
$fields = "`".implode("`,`", $field_list)."`";
$structure = $db->show_create_table($table).";\n";
$contents .= $structure;
clear_overflow($fp, $contents);
if($db->engine == 'mysqli')
{
$query = mysqli_query($db->read_link, "SELECT * FROM {$db->table_prefix}{$table}", MYSQLI_USE_RESULT);
}
else
{
$query = $db->simple_select($table);
}
while($row = $db->fetch_array($query))
{
$insert = "INSERT INTO {$table} ($fields) VALUES (";
$comma = '';
foreach($field_list as $field)
{
if(!isset($row[$field]) || is_null($row[$field]))
{
$insert .= $comma."NULL";
}
else if($db->engine == 'mysqli')
{
$insert .= $comma."'".mysqli_real_escape_string($db->read_link, $row[$field])."'";
}
else
{
$insert .= $comma."'".$db->escape_string($row[$field])."'";
}
$comma = ',';
}
$insert .= ");\n";
$contents .= $insert;
clear_overflow($fp, $contents);
}
$db->free_result($query);
}
$db->set_table_prefix(TABLE_PREFIX);
if(function_exists('gzopen'))
{
gzwrite($fp, $contents);
gzclose($fp);
rename($file.'.incomplete.sql.gz', $file.'.sql.gz');
}
else
{
fwrite($fp, $contents);
fclose($fp);
rename($file.'.incomplete.sql', $file.'.sql');
}
add_task_log($task, $lang->task_backup_ran);
}
}
// Allows us to refresh cache to prevent over flowing
function clear_overflow($fp, &$contents)
{
global $mybb;
if(function_exists('gzopen'))
{
gzwrite($fp, $contents);
}
else
{
fwrite($fp, $contents);
}
$contents = '';
}