-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmirror.pl
executable file
·168 lines (133 loc) · 4.06 KB
/
mirror.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
166
167
168
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use Getopt::Long;
my $scheme = "http://";
my $source_host_port = "your_backend_system:8080";
my $target_host_port = "your_frontend_system:8080";
my $user = "admin";
my $password = "your_very_secret_password";
my $suri = $scheme . $source_host_port . "/exist/rest/db";
my $turi = $scheme . $target_host_port . "/exist/rest/db";
# These must be edited
my %editions = (
"nwgw" => "/data-nwgw",
"cnw" => "/data-cnw",
"schw" => "/data-schw",
"hartw" => "/data-hartw"
);
my $source = "/public";
my $target = "";
my $edition = "";
while( ($edition,$target) = each(%editions) ) {
my $since = "2000-01-01T00:00:01";
my $before = "2099-12-31T24:00:00";
#####
#
# Collecting data on source files The database treats dates gracefully, and so
# does the deprecated version of this script. However I'm not prepared to
# resolve the dependencies needed to install DateTime on our servers. RedHat
# has its limitations.
#
my $source_files = $suri . "/document-info.xq?" .
join ("&",
(
"c=$edition",
"db=$source",
"since=$since",
"before=$before"
)
);
my $target_files = $turi . "/document-info.xq?" .
join ("&",
(
"c=$edition",
"db=$target"
)
);
my %sources = &get_data($source_files);
my %targets = &get_data($target_files);
my @filelist = keys(%sources);
print "about to process $#filelist files\n";
foreach my $file (@filelist) {
print STDERR "doing $file \n";
my $req = new HTTP::Request();
my $target_url = $turi . $target .'/' . $file;
my $source_url = $suri . $source .'/' . $file;
©_file($source_url,$target_url);
}
@filelist = keys(%targets);
foreach my $file (@filelist) {
if($sources{$file}) {
# print STDERR "The file $file is in the source database\n";
} else {
my $target_url = $turi . $target .'/' . $file;
print STDERR "The file $file is in the target database only should be removed\n";
&delete_file($target_url);
}
}
}
sub get_data {
my $files = shift ;
my %data = ();
my $ua = LWP::UserAgent->new;
$ua->agent("crud-client/0.1 ");
$ua->credentials($target_host_port, "exist" , $user, $password );
my $req = new HTTP::Request();
$req->uri($files);
$req->method("GET");
my $res = $ua->request($req);
if ($res->is_success) {
print STDERR "Success getting $files list " . $res->status_line . "\n";
my @fileitems = split("\n",$res->content());
print STDERR "About to parse $#fileitems files\n";
foreach my $line (@fileitems) {
my ($file,$date) = split /\s+/,$line;
$data{$file} = $date;
}
}
return %data;
}
sub delete_file {
my $url = shift;
print STDERR "delete_file called\n";
my $ua = LWP::UserAgent->new;
$ua->credentials($target_host_port, "exist" , $user, $password );
my $delreq = new HTTP::Request();
$delreq->method("DELETE");
$delreq->uri($url);
my $delres = $ua->request($delreq);
if($delres->is_success) {
print STDERR "Successfully removed file $url\n";
}
}
sub copy_file {
my $source_url = shift;
my $target_url = shift;
print STDERR "copy_file called\n";
my $ua = LWP::UserAgent->new;
my $getreq = new HTTP::Request();
$getreq->method("GET");
$getreq->uri($source_url);
my $getres = $ua->request($getreq);
if($getres->is_success) {
$ua->credentials($target_host_port, "exist" , $user, $password );
print STDERR "got content from $source_url " . $getres->code . "\n";
my $content = $getres->content();
my $putreq = new HTTP::Request();
$putreq->method("PUT");
$putreq->content($content);
$putreq->uri($target_url);
my $putres = $ua->request($putreq);
if($putres->is_success) {
print STDERR "managed to put content to $target_url " .
$putres->code . "\n";
} else {
print STDERR "failed to put content to $target_url " .
$putres->code . "\n";
}
} else {
print STDERR "failed to get content from $source_url "
. $getres->code . "\n";
}
}