-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.php
157 lines (109 loc) · 3.57 KB
/
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
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
<?php
session_start();
if(!isset($_SESSION['user_session'])){ //User_session
header("location:index.php");
}else{
backup_tables('localhost','root','','simplepharmacy');
}
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysqli_connect($host,$user,$pass,$name);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysqli_query($link,'SHOW TABLES');
while($row = mysqli_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysqli_query($link,'SELECT * FROM '.$table);
$num_fields = mysqli_num_fields($result);
@$return.= 'DROP TABLE '.$table.';';
$row2 = mysqli_fetch_row(mysqli_query($link,'SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysqli_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j < $num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("/\n/","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j < ($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
if(!file_exists("C:\SPMS_DB_Backup")){
mkdir("C:\SPMS_DB_Backup");
}
$date = date("dMY");
@$invoice_number = $_GET['invoice_number'];
if(!file_exists('C:\SPMS_DB_Backup\db-backup-'.$date.'-'.implode(',',$tables).'.sql')){
$handle = fopen('C:\SPMS_DB_Backup\db-backup-'.$date.'-'.implode(',',$tables).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
echo "
<input type='hidden' value=$invoice_number id='invoice_number'>
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript'>
if(window.confirm('Backup Successfully')){
var invoice_number=$('#invoice_number').val();
window.location.href='home.php?invoice_number='+invoice_number;
}
</script>";
}else{
echo "
<input type='hidden' value=$invoice_number id='invoice_number'>
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript'>
if(window.confirm('Already Backup')){
var invoice_number=$('#invoice_number').val();
window.location.href='home.php?invoice_number='+invoice_number;
}
</script>";
}
}
?>
<!-- <?php
// include("dbcon.php");
// $select_query = "SELECT * FROM stock order by id desc";
// $result = mysqlii_query($con,$select_query);
// echo "<table border='1'>";
// echo "<th>Id</th>";
// echo "<th>Medicine</th>";
// echo "<th>Category</th>";
// echo "<th>Registered Quantity</th>";
// echo "<th>Used Quantity</th>";
// echo "<th>Remain Quantity</th>";
// echo "<th>Registered Date</th>";
// echo "<th>Expired Date</th>";
// echo "<th>Remark</th>";
// echo "<th>Type</th>";
// echo "<tr>";
// while($row = mysqlii_fetch_array($result)){
// for($i=0;$i<10;$i++){
// echo "<td>".$row[$i]."</td>";
// }
// echo "</tr>";
// }
// $date = date('d-m-y');
// header("Content-Type: application/xls");
// header("Content-Disposition: attachment; filename=backup\stock($date).xls");
// echo "</table>";
?> --><!-- For more projects: Visit codeastro.com -->