-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpg_table_recovery.pl
166 lines (138 loc) · 4.36 KB
/
pg_table_recovery.pl
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
#!/usr/bin/perl -w
use DBI;
$dbname = "dbname"; # Database name
$username = "dbuser"; # DB username
$password = "secretpassword"; # DB password
$dbhost = "localhost"; # DB hostname
$dbport = "5432"; # DB Port
$dboptions = "-e"; # DBI options
$dbtty = "ansi"; # DB Charset
$table = "tablename"; # Working table
$tableid = "id"; # Working table ID field name
$write = 0; # Passive mode switch
$verbose = 0; # Output additional diagnostic data
$qu = 0;
$timestamp_start = time();
$dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport;options=$dboptions;tty=$dbtty","$username","$password",
{PrintError => 0});
&check_range(0, &get_row_count($table), $table);
@table_fields = &get_field_names($table);
$timespan = time() - $timestamp_start;
$count = keys %offsets;
print "\nSCAN COMPLETE IN $timespan S\nfound $count corrupted rows. $qu database queries performed. \n\n";
foreach $ofst (keys %offsets) {
print "$offsets{$ofst} \n" if $verbose;
foreach $field (@table_fields) {
if ( &query_dbi($table,1,$ofst,$field) ) {
$ident = &get_row_id($table, $ofst);
print "id: $ident; field: $field at offset $ofst corruped.\n";
if ($write) {
$result = &update_dbi($table,$field,$ident);
print $result;
} else {
print "Diagnostic mode, 0 bytes written.\n";
}
} else {
}
}
}
print "\nSEQUENCE COMPLETE\n\n";
sub check_range {
local ($range_start, $range_end, $table) = @_ if @_;
print "Checking range: $range_start -- $range_end\n";
if ($range_start >= $range_end) {
local $errval = &query_dbi($table,'1',$range_start);
if ( $errval ) {
$offsets{$range_start} = "$errval; offset: $range_start";
print "Found error at offset $range_start\n" if $verbose;
} else {
}
} else {
$pre_median = ($range_end - $range_start) / 2 ;
if ( ($range_end-$range_start) / 2 != int( ($range_end-$range_start) / 2 ) ) {
$midrange = int($pre_median);
$tail = 1;
} else {
$midrange = $pre_median;
$tail = 0;
}
local $lower_median = $midrange;
local $lower_start = $range_start;
local $lower_end = $range_start + $lower_median;
$lower_median = 1 if $lower_median < 1;
local $upper_median = $midrange + $tail;
local $upper_start = $range_start + $upper_median;
local $upper_end = $range_end;
$upper_median = 1 if $upper_median < 1;
print "CHECKING LOWER: $lower_start ... $lower_end LIMIT:$lower_median \n" if $verbose;
if ( &query_dbi($table,$lower_median+1,$lower_start) ) {
print " ^^^^^^ triggered here \n\n" if $verbose;
&check_range($lower_start, $lower_end, $table);
}
print "CHECKING UPPER: $upper_start ... $upper_end LIMIT:$upper_median \n" if $verbose;
if ( &query_dbi($table,$upper_median+1,$upper_start) ) {
print " ^^^^^^ triggered here \n\n" if $verbose;
&check_range($upper_start, $upper_end, $table);
}
}
}
sub query_dbi {
local ($table,$limit,$offset,$field) = @_ if @_;
$qu++;
local $field = "*" unless $field;
local $query = "SELECT COUNT($field) FROM $table ORDER BY $tableid LIMIT $limit OFFSET $offset";
local $sth = $dbh->prepare($query);
local $rv = $sth->execute();
if (!defined $rv) {
return $dbh->errstr;
} else {
return ""
}
}
sub get_row_count {
local ($table) = @_ if @_;
$qu++;
local $query = "SELECT COUNT(*) FROM $table";
$sth = $dbh->prepare($query);
$rv = $sth->execute();
@row = $sth->fetchrow_array();
return $row[0];
}
sub get_field_names {
local ($table) = @_ if @_;
$qu++;
local $query = "SELECT column_name from information_schema.columns where table_name = '$table'";
$sth = $dbh->prepare($query);
$rv = $sth->execute();
while (@rows = $sth->fetchrow_array()) {;
push (@out, join('',@rows) );
}
return @out;
}
sub get_row_id {
local ($table,$offset) = @_ if @_;
$qu++;
local @ids;
local $query = "SELECT $tableid FROM $table ORDER BY $tableid LIMIT 1 OFFSET $offset";
$sth = $dbh->prepare($query);
$rv = $sth->execute();
while (@rws = $sth->fetchrow_array()) {;
push (@ids, join('',@rws) );
}
return $ids[0];
}
sub update_dbi {
local ($table,$field,$ident) = @_ if @_;
$qu++;
local $query = "UPDATE $table SET $field = '' WHERE $tableid = $ident";
$dbh->begin_work();
$rv = $dbh->do($query);
$dbh->commit();
if (!defined $rv) {
return "Update failed: " . $dbh->errstr . "\n";
} else {
return "Update Ok\n";
}
}
$sth->finish();
$dbh->disconnect();