-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpruebas2.php
214 lines (197 loc) · 6.61 KB
/
pruebas2.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
<?php
// scrypt for backup and restore postgres database
function dl_file($file){
if (!is_file($file)) { die("<b>404 File not found!</b>"); }
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
$ctype="application/force-download";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $ctype");
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($file);
exit;
}
$action = $_POST["actionButton"];
$ficheiro=$_FILES["path"]["tmp_name"];
switch ($action) {
case "Import":
$dbname = "SIDICI"; //database name
$dbconn = pg_pconnect("host=ssh.mservers.ovh port=5432 dbname=$dbname
user=Jrodriguez password=Amovil.2022$"); //connectionstring
if (!$dbconn) {
echo "Can't connect.\n";
exit;
}
$back = fopen($ficheiro,"r");
$contents = fread($back, filesize($ficheiro));
$res = pg_query(utf8_encode($contents));
echo pg_result_error($res);
echo "Upload Ok";
fclose($back);
break;
case "Export":
$dbname = "SIDICI"; //database name
$dbconn = pg_pconnect("host=ssh.mservers.ovh port=5432 dbname=$dbname
user=Jrodriguez password=Amovil.2022$"); //connectionstring
if (!$dbconn) {
echo "Can't connect.\n";
exit;
}
$back = fopen("$dbname.sql","w");
$str="";
$str .= "\n--\n";
$str .= "-- Generar Sequencias\n";
$res0 = pg_query("select c.relname FROM pg_class c WHERE c.relkind = 'S';");
while($row = pg_fetch_row($res0))
{
$str .= "DROP SEQUENCE IF EXISTS ".$row[0]." CASCADE;\n";
$str .= "CREATE SEQUENCE ".$row[0].";\n";
}
$res = pg_query(" select relname as tablename
from pg_class where relkind in ('r')
and relname not like 'pg_%' and relname not like 'sql_%' order by tablename");
while($row = pg_fetch_row($res))
{
$table = $row[0];
$str .= "\n--\n";
$str .= "-- Estrutura da tabela '$table'";
$str .= "\n--\n";
$str .= "\nDROP TABLE IF EXISTS $table CASCADE;";
$str .= "\nCREATE TABLE $table (";
$res2 = pg_query("
select * from(
SELECT attnum,attname , typname , atttypmod-4 , attnotnull
,atthasdef ,adsrc AS def
FROM pg_attribute, pg_class, pg_type, pg_attrdef WHERE
pg_class.oid=attrelid
AND pg_type.oid=atttypid AND attnum>0 AND pg_class.oid=adrelid AND
adnum=attnum
AND atthasdef='t' AND lower(relname)='$table' UNION
SELECT attnum,attname , typname , atttypmod-4 , attnotnull ,
atthasdef ,'' AS def
FROM pg_attribute, pg_class, pg_type WHERE pg_class.oid=attrelid
AND pg_type.oid=atttypid AND attnum>0 AND atthasdef='f' AND
lower(relname)='$table') t order by t.attnum
");
while($r = pg_fetch_row($res2))
{
$str .= "\n" . $r[1]. " " . $r[2];
if ($r[2]=="varchar")
{
$str .= "(".$r[3] .")";
}
if ($r[4]=="t")
{
$str .= " NOT NULL";
}
if ($r[5]=="t")
{
$str .= " DEFAULT ".$r[6];
}
$str .= ",";
}
$str=rtrim($str, ",");
$str .= "\n);\n";
$str .= "\n--\n";
$str .= "-- Creating data for '$table'";
$str .= "\n--\n\n";
$res3 = pg_query("SELECT * FROM $table");
while($r = pg_fetch_row($res3))
{
$sql = "INSERT INTO $table VALUES ('";
$sql .= utf8_decode(implode("','",$r));
$sql .= "');";
if(is_null($str))$str = "";
$str = str_replace("''","NULL",$str);
$str .= $sql;
$str .= "\n";
}
$res1 = pg_query("SELECT pg_index.indisprimary,
pg_catalog.pg_get_indexdef(pg_index.indexrelid)
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,
pg_catalog.pg_index AS pg_index
WHERE c.relname = '$table'
AND c.oid = pg_index.indrelid
AND pg_index.indexrelid = c2.oid
AND pg_index.indisprimary");
while($r = pg_fetch_row($res1))
{
$str .= "\n\n--\n";
$str .= "-- Creating index for '$table'";
$str .= "\n--\n\n";
$t = str_replace("CREATE UNIQUE INDEX", "", $r[1]);
$t = str_replace("USING btree", "|", $t);
// Next Line Can be improved!!!
$t = str_replace("ON", "|", $t);
$Temparray = explode("|", $t);
$str .= "ALTER TABLE ONLY ". $Temparray[1] . " ADD CONSTRAINT " .
$Temparray[0] . " PRIMARY KEY " . $Temparray[2] .";\n";
}
}
$res = pg_query("select max(idadmin)+1 id from admin;");
while($r = pg_fetch_row($res))
{
$str .= "\n\n-- Actualizando secuencia admin\n\n";
$str .= "ALTER SEQUENCE admin_idadmin_seq RESTART WITH ".$r[0].";\n";
}
$res = pg_query("select max(idmovimiento)+1 id from movimientos;");
while($r = pg_fetch_row($res))
{
$str .= "\n\n-- Actualizando secuencia movimientos\n\n";
$str .= "ALTER SEQUENCE movimientos_idmovimiento_seq RESTART WITH ".$r[0].";\n";
}
$res = pg_query("select max(id_novedad)+1 id from novedades;");
while($r = pg_fetch_row($res))
{
$str .= "\n\n-- Actualizando secuencia novedad\n\n";
$str .= "ALTER SEQUENCE novedades_id_novedad_seq RESTART WITH ".$r[0].";\n";
}
$res = pg_query(" SELECT
cl.relname AS tabela,ct.conname,
pg_get_constraintdef(ct.oid)
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class cl ON (a.attrelid = cl.oid AND cl.relkind = 'r')
JOIN pg_catalog.pg_namespace n ON (n.oid = cl.relnamespace)
JOIN pg_catalog.pg_constraint ct ON (a.attrelid = ct.conrelid AND
ct.confrelid != 0 AND ct.conkey[1] = a.attnum)
JOIN pg_catalog.pg_class clf ON (ct.confrelid = clf.oid AND
clf.relkind = 'r')
JOIN pg_catalog.pg_namespace nf ON (nf.oid = clf.relnamespace)
JOIN pg_catalog.pg_attribute af ON (af.attrelid = ct.confrelid AND
af.attnum = ct.confkey[1]) order by cl.relname ");
while($row = pg_fetch_row($res))
{
$str .= "\n\n--\n";
$str .= "-- Creating relacionships for '".$row[0]."'";
$str .= "\n--\n\n";
$str .= "ALTER TABLE ONLY ".$row[0] . " ADD CONSTRAINT " . $row[1] .
" " . $row[2] . ";";
}
fwrite($back,$str);
fclose($back);
dl_file("$dbname.sql");
break;
}
?>
<html>
<head>
</head>
<body>
<form id="dataForm" name="dataForm" method="post"
enctype="multipart/form-data" action="">
<input type="file" name="path" id="path" style="width:300px"/>
<input type="submit" value="Import" name="actionButton"
id="actionButton" >
<input type="submit" value="Export" name="actionButton"
id="actionButton" >
</form>
</body>
</html>