11 my @monikop_banner = (
12 " _/ _/ _/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/_/_/ ",
13 " _/_/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/ _/",
14 " _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/_/_/ ",
15 " _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ ",
16 "_/ _/ _/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ ",
20 # 0 = clean UI; 1 = lots of scrolling junk; anything else = both (pipe to file)
22 $debug = $ARGV[1] if $ARGV[1];
24 # Where to read local configuration:
25 my $monikop_config = '~/monikop/monikop.config';
26 $monikop_config = $ARGV[0] if $ARGV[0];
28 ########################################
30 ########################################
31 # Possible data sources, and by what directory name to represent them in
33 # When the latter is not unique, care must be taken that all pathnames in the
34 # respective sources are unique.
37 # Possible mount points of data destinations. Must be unique.
38 my @usable_mount_points;
40 # Common directory (under a mount point) to put new data in.
41 # Must agree with Pokinom's setting.
42 my $path_in_destination;
44 # Directories (under any mount point) of this name will be deleted
45 # Must agree with Pokinom's setting.
46 my $path_in_destination_backed_up;
48 # Directory name (under a mount point) while being deleted.
49 # Must agree with Pokinom's setting.
50 my $path_in_destination_being_deleted;
52 # Path and file name prefix of rsync's raw logs:
55 # Path and file name prefix of the list of successfully rsynced files:
58 # How to suffix the name of the duplicate of a safe file:
59 my $safe_file_backup_suffix;
61 # How to suffix the name of an unfinished safe file:
62 my $safe_file_unfinished_suffix;
64 # What to do (shutdown) when F3 has been pressed:
67 # What to do (reboot) when F6 has been pressed:
70 # Rsyncs time (in seconds) to wait for a response:
73 # Rsyncs directory (relative to destination) for partially transferred files.
74 # Must agree with Pokinom's setting.
75 my $rsync_partial_dir_name;
77 # Put actual values into the above.
78 eval `cat $monikop_config`;
80 # Time in seconds before rsync is restarted and user information is
82 my $coffee_break = 10;
84 # Places to store run-time information to share between threads:
85 my %speeds :shared
; # rsync output
86 my %progress_ratios :shared
; # rsync output
87 my %destination_usages :shared
; # i.e. used/unused
88 my %destination_usage_ratios :shared
;
89 my %destination_source_is_writing_to :shared
;
90 my %reachable :shared
;
92 sub debug_print
{ if ($debug) { print @_; } };
94 # Return the hash referenced by argument, which is sorted if accessed as an
99 foreach my $key (sort keys %hash_table) {
100 push @sorted_hash, $key, $hash_table{$key};
105 # Turn a path into a legal perl identifier:
106 sub make_key_from_path
{
108 ($path) =~ s/\/?(.*)\/?
/$1/g;
115 $source_roots{make_key_from_path
$_} = $_
118 my %source_dirs_in_destination;
120 $source_dirs_in_destination{make_key_from_path
$_} = $sources{$_}
123 # Crudely turn date string(s) into a number. Chronological order is preserved.
125 my $date = join '', @_;
126 $date =~ tr/ \/:-//d
;
130 # Return sorted intersection of arrays which are supposed to have unique
133 my @intersection = ();
136 foreach $element (@_) { $count{$element}++ }
137 foreach $element (keys %count) {
138 push @intersection, $element if $count{$element} > 1;
143 # Write @content to a file with name $filename or a name starting with
144 # $filename and ending with $safe_file_backup_suffix. Leave at least one such
145 # file, even if interrupted.
147 my ($filename, @content) = @_;
148 my $filename_a = $filename;
149 my $filename_b = $filename . $safe_file_backup_suffix;
150 my $filename_unfinished = $filename . $safe_file_unfinished_suffix;
151 local (*FILE_UNFINISHED
);
152 open FILE_UNFINISHED
, '>', $filename_unfinished
153 or die "[" . $$ . "] open $filename_unfinished failed: $!\n";
154 print FILE_UNFINISHED
@content;
155 close FILE_UNFINISHED
;
156 qx(cp
$filename_unfinished $filename_b);
157 qx(mv
$filename_unfinished $filename_a);
160 # Put contents of $filename into an array:
164 open FILE
, '<', $filename
165 or warn "[" . $$ . "] open $filename failed: $!\n";
171 # Read a file written by safe_write
174 my $filename_a = $filename;
175 my $filename_b = $filename . $safe_file_backup_suffix;
176 if (stat $filename_a) { my $filename = $filename_a }
177 elsif (stat $filename_b) { my $filename = $filename_b }
179 debug_print
"SAFE_READ: $filename";
183 my @destination_roots;
184 my %rsync_worker_thread;
189 my %rsync_dir_exec_form;
190 my %rsync_dir_err_form;
191 my %being_deleted_thread;
193 sub rsync_preparation_form
{
195 $speeds{$source} = "-";
198 ########## Capture rsync's status messages for use by UI
199 '$rsync_outfun{\'', $source, '\'} = sub {',
200 ' my ($outline, $outputchannel) = @_ ; ',
201 ' my ($speed) = $outline =~ /\d+\s+\d+%\s+(\S+)/; ',
202 ' my ($progress_ratio) = $outline =~ /.+to-check=(\d+\/\d+)\)$/; ',
203 ' if ($speed and $outputchannel eq \'out\') {',
204 ' $speeds{\'', $source, '\'} = $speed;',
206 ' $speeds{\'', $source, '\'} = "-";',
208 ' if ($progress_ratio and $outputchannel eq \'out\') {',
209 ' $progress_ratios{\'', $source, '\'} = $progress_ratio;',
213 ########## Run rsync: main worker
214 '$rsync{\'', $source, '\'} = File::Rsync->new; ',
215 ########## Return fodder for another eval
216 '$rsync_exec_form{\'', $source, '\'} = sub {',
217 ' my ($complete_destination) = @_;',
218 ' \'$rsync{\\\'', $source, '\\\'}->exec(',
220 ' src => \\\'', $source_roots{$source}, '/\\\', ',
221 ' dest => \\\'\' . $complete_destination . \'/\\\', ',
222 ' outfun => $rsync_outfun{\\\'', $source, '\\\'}, ',
223 ' progress => 1, debug => 0, verbose => 0, ',
224 ' filter => [\\\'merge,- ', $finished_prefix, $source, '\\\'], ',
226 ' \\\'--recursive\\\', \\\'--times\\\', ',
227 ' \\\'--partial-dir=', $rsync_partial_dir_name, '\\\', ',
228 ' \\\'--timeout=', $rsync_timeout, '\\\', ',
229 ' \\\'--prune-empty-dirs\\\', ',
230 ' \\\'--log-file-format=%i %b %l %M %n\\\', ',
231 join (', ', map { '\\\'--compare-dest=' . $_ . '/'
232 . $path_in_destination . '/'
233 . $source_dirs_in_destination{$source} . '/\\\'' }
234 ( @destination_roots )),
235 ' , \\\'--log-file=', $rsync_log_prefix, $source, '\\\'] ',
240 ########## Run rsync: get directory from source
241 '$rsync_dir{\'', $source, '\'} = File::Rsync->new; ',
242 ########## Return fodder for another eval: dir
243 '$rsync_dir_exec_form{\'', $source, '\'} = sub {',
244 ' \'$rsync_dir{\\\'', $source, '\\\'}->list(',
246 ' src => \\\'', $source_roots{$source}, '/\\\', ',
247 ' literal => [ \\\'--recursive\\\', ',
248 ' \\\'--timeout=', $rsync_timeout, '\\\'] ',
253 ########## Return fodder for another eval: error code from last rsync call
254 '$rsync_dir_err_form{\'', $source, '\'} = sub {',
255 ' \'$rsync_dir{\\\'', $source, '\\\'}->err();\' ',
260 sub act_on_keypress
{
261 my ($pressed_key) = @_;
262 if ($pressed_key eq 267) { qx($key_f3_action) }
263 elsif ($pressed_key eq 270) { qx($key_f6_action); }
266 # Run rsync for one $source, try all destinations:
267 sub rsync_someplace
{
268 my ($source, @destinations) = @_;
271 my $rsync_log_name = $rsync_log_prefix . $source;
272 my $finished_name = $finished_prefix . $source;
273 foreach (@destinations) {
274 $destination_source_is_writing_to{$source} = $_;
275 my $common_destination = $_ . '/' . $path_in_destination;
276 my $complete_destination = $common_destination . '/'
277 . $source_dirs_in_destination{$source};
278 qx(mkdir -p
$common_destination);
279 if ($?
) { die "Fatal: $common_destination is not writable."}
280 if (eval ($rsync_exec_form{$source} ($complete_destination))) {
281 debug_print
"EVAL RSYNC_EXEC_FORM (successful) $source, $complete_destination: $@ \n";
283 last; # unnecessary reruns would put empty dirs into otherwise unused destinations
285 debug_print
"EVAL RSYNC_EXEC_FORM (failed) $source, $complete_destination: $@ \n";
295 $display_thread->kill('TERM')->join;
296 die "Caught signal $_[0]";
300 # Preparations done; sleeves up!
302 # Make sure we have dirs to put our logs in:
304 my ($filename, $directory) = fileparse
$_;
305 qx(mkdir -p
$directory);
306 } ( $rsync_log_prefix, $finished_prefix );
308 # Find usable destinations:
309 my @raw_mount_points = grep (s/\S+ on (.*) type .*/$1/, qx/mount/);
310 chomp @raw_mount_points;
311 @destination_roots = intersection
@raw_mount_points, @usable_mount_points;
312 debug_print
"DESTINATION_ROOTS:\n";
313 debug_print
@destination_roots;
315 # Clean up destinations:
317 my $p_i_d = $_ . '/' . $path_in_destination;
318 my $p_i_d_backed_up = $_ . '/' . $path_in_destination_backed_up;
319 my $p_i_d_being_deleted = $_ . '/' . $path_in_destination_being_deleted;
320 if (-d
$p_i_d_backed_up and -d
$p_i_d_being_deleted) {
321 warn "[" . $$ . "] Both $p_i_d_backed_up and $ p_i_d_being_deleted exist. This does not normally happen. I'm deleting $p_i_d_being_deleted. Be patient.\n";
322 qx(rm
-rf
$p_i_d_being_deleted);
324 qx(mv
-f
$p_i_d_backed_up $p_i_d_being_deleted 2> /dev/null
);
325 $being_deleted_thread{$_} = async
{ qx(rm
-rf
$p_i_d_being_deleted); };
326 } @destination_roots;
328 if (scalar @destination_roots) {
329 # Set up and start things per source_root:
331 # rotate for crude load balancing:
332 push (@destination_roots, shift (@destination_roots));
333 $progress_ratios{$_} = "?"; # Initialize for UI
334 $rsync_worker_thread{$_} = async
{
336 my $rsync_log_name = $rsync_log_prefix . $_;
337 my $finished_name = $finished_prefix . $_;
338 debug_print
'rsync_preparation_form:' .
339 rsync_preparation_form
($_). "\n";
340 eval rsync_preparation_form
$_;
341 debug_print
"EVAL RSYNC_PREPARATION_FORM $_: $@ \n";
342 debug_print
'rsync_dir_exec_form $_:'.
343 $rsync_dir_exec_form{$_} () . "\n";
344 my @rsync_ls = eval $rsync_dir_exec_form{$_}();
345 eval $rsync_dir_err_form{$_}();
346 $reachable{$_} = eval $rsync_dir_err_form{$_}() ?
0 : 1;
347 debug_print
"REACHABLE: $reachable{$_}\n";
348 if ($reachable{$_}) {
349 my %old_finished = safe_read
$finished_name;
350 if (-f
$rsync_log_name) {
351 my @rsync_log = read_list
$rsync_log_name;
352 foreach (@rsync_log) {
353 my ($file_length, $modification_time, $filename) =
354 /[\d\/\s
:\
[\
]]+ [>c\
.][fd
]\S
{9} \d
+ (\d
+) ([\d\
/:-]+) (.*)$/;
356 $old_finished{$filename . "\n"} =
357 "### " . $modification_time . " " .
361 safe_write
$finished_name, sort_hash
%old_finished;
362 unlink $rsync_log_name unless $debug;
365 # Delete from %old_finished what has to be re-rsynced.
366 foreach (@rsync_ls) {
367 my ($ls_size, $ls_modification_date,
368 $ls_modification_time, $ls_filename) =
369 /[drwx-]+\s+(\d+) ([\d\/]+) ([\d
:]+) (.*)/;
371 exists $old_finished{$ls_filename . "\n"}) {
372 my ($finished_modification_date, $finished_size) =
373 $old_finished{$ls_filename . "\n"} =~
375 if ( ($finished_size eq $ls_size)
377 ($finished_modification_date)
379 ($ls_modification_date,
380 $ls_modification_time)) )
382 $finished{$ls_filename . "\n"} =
383 $old_finished{$ls_filename . "\n"};
387 safe_write
$finished_name, %finished;
388 if (rsync_someplace
$_, @destination_roots) {
389 $progress_ratios{$_} = '0'; # Clean staleness for UI
395 } keys %source_roots;
398 # Provide some reassuring user information:
399 my $destinations_monitor_thread = async
{
402 my $destination_root = $_;
403 my $destination_usage = 0;
405 my $source_root = $_;
406 my $complete_destination = $destination_root . '/'
407 . $path_in_destination . '/'
408 . $source_dirs_in_destination{$source_root};
409 my @dir = qx(ls
-A
$complete_destination/ 2> /dev
/null
);
410 $destination_usage = 1 if scalar @dir; # 0 = no new data
411 } keys %source_roots;
412 $destination_usages{$destination_root} = $destination_usage;
413 my @destination_usage_ratio =
414 grep s/\S+\s+\S+\s+\S+\s+\S+\s+(\d*)%\s+\S+/$1/, qx(df
-P
$_);
415 chomp @destination_usage_ratio;
416 ($destination_usage_ratios{$_}) = @destination_usage_ratio;
417 } @destination_roots;
423 # Let the workers toil.
426 # Let the workers toil; talk to the user.
428 $display_thread = async
{
430 endwin
(); # Leave a usable terminal.
434 my $redraw_window_count = 0;
439 my $window_left = newwin
(LINES
() -8, 29, 0, 0);
440 my $window_right = newwin
(LINES
() - 8, 50, 0, 29);
441 my $window_center = newwin
(5, 79, LINES
() - 8, 0);
442 my $window_bottom = newwin
(3, 79, LINES
() - 3, 0);
443 $window_bottom->keypad(1);
444 $window_bottom->nodelay(1);
446 init_pair
1, COLOR_MAGENTA
, COLOR_BLACK
;
447 init_pair
2, COLOR_RED
, COLOR_BLACK
;
448 init_pair
3, COLOR_CYAN
, COLOR_BLACK
;
449 init_pair
4, COLOR_YELLOW
, COLOR_BLACK
;
450 my $MAGENTA = COLOR_PAIR
(1);
451 my $RED = COLOR_PAIR
(2);
452 my $CYAN = COLOR_PAIR
(3);
453 my $YELLOW = COLOR_PAIR
(4);
456 $window_left->attron($CYAN);
457 $window_left->box(0, 0);
458 $window_left->addstr(0, 6, "Data Destinations");
459 $window_left->attroff($CYAN);
460 my $destinations_format = "%-18s%-6s%-3s";
461 $window_left->attron(A_BOLD
);
462 $window_left->addstr(1, 1,
463 sprintf($destinations_format,
464 "Removable", "Fresh", "Usg"));
465 $window_left->addstr(2, 1,
466 sprintf($destinations_format,
467 "Disk", "Data?", "%"));
468 $window_left->attroff(A_BOLD
);
469 my $destination_usage;
472 if ($destination_usages{$_}) {
473 $window_left->attron($RED);
474 $destination_usage = "yes";
476 $window_left->attron($CYAN);
477 $destination_usage = "no";
479 $window_left->addstr($line_number, 1,
480 sprintf($destinations_format,
482 substr($destination_usage, -6, 6),
483 substr($destination_usage_ratios{$_} ?
484 $destination_usage_ratios{$_} :
488 $window_left->attroff($RED);
489 $window_left->attroff($CYAN);
490 } sort @destination_roots;
492 $window_right->attron($MAGENTA);
493 $window_right->box(0,0);
494 $window_right->addstr(0, 19, "Data Sources");
495 $window_right->attroff($MAGENTA);
496 my $sources_format = "%-15s%-11s%-9s%-13s";
497 $window_right->attron(A_BOLD
);
498 $window_right->addstr(1, 1,
499 sprintf ($sources_format,
500 "Data", "", "To", "Writing"));
501 $window_right->addstr(2, 1,
502 sprintf ($sources_format,
503 "Source", "Speed", "Do", "To"));
504 $window_right->attroff(A_BOLD
);
506 $window_right->attron($MAGENTA);
509 my $current_destination = '?';
510 if (exists $destination_source_is_writing_to{$source}) {
511 $current_destination =
512 $destination_source_is_writing_to{$source};
514 if ($reachable{$source}) {
515 $window_right->addstr($line_number, 1,
516 sprintf($sources_format,
517 substr($source_roots{$source},
519 substr($speeds{$source}, 0, 11),
520 substr($progress_ratios{$source},
522 substr($current_destination,
526 $window_right->addstr($line_number, 1,
527 sprintf($sources_format, "", "", "", ""));
528 } sort (keys %source_roots);
529 $window_right->attroff($MAGENTA);
533 $window_center->addstr($line_number, 2, $_);
536 $window_center->move(0, 0);
538 $window_bottom->box(0,0);
539 $window_bottom->attron(A_BOLD
);
540 $window_bottom->addstr(1, 3, "[F3]: Turn off computer. [F6]: Restart computer.");
541 $window_bottom->attroff(A_BOLD
);
543 $window_left->noutrefresh();
544 $window_right->noutrefresh();
545 $window_bottom->noutrefresh();
546 $window_center->noutrefresh(); # Last window gets the cursor.
547 act_on_keypress
($window_bottom->getch());
549 if (++ $redraw_window_count > 5) {
550 $redraw_window_count = 0;
561 # Tidy up. (Except we don't reach this.)
563 $being_deleted_thread{$_}->join if $being_deleted_thread{$_};
564 } @destination_roots;
567 $rsync_worker_thread{$_}->join if $rsync_worker_thread{$_};
568 } keys %source_roots;
570 $destinations_monitor_thread->join if $destinations_monitor_thread;
572 $display_thread->join if $display_thread;