From: Victor Wagner Date: Mon, 23 Oct 2023 18:21:48 +0000 (+0300) Subject: Added ability to send messages to instant messenger X-Git-Url: http://www.wagner.pp.ru/gitweb/?p=oss%2Frestore.git;a=commitdiff_plain;h=0ac08edde44c5705446e76c26ee80cbf7ec919fe Added ability to send messages to instant messenger --- diff --git a/backup b/backup index a6d0137..d01a64c 100755 --- a/backup +++ b/backup @@ -27,16 +27,23 @@ performed. If all backups were successful, unmounts all partitions of device where C<$snapshot_root> is located. +If there exists B invokes it and +sends messaage about backup completion status to its stdin + =head1 FILES /etc/rsnapshot.conf +/usr/local/sbin/notify-messenger + =head1 AUTHOR Victor Wagner =cut +use Sys::Hostname; + # Read rsnapshot conf, find out snapshot_root and retain. open $conf, "<","/etc/rsnapshot.conf" or die "/etc/rsnapshot.conf:$!\n"; my @levels=(); @@ -86,6 +93,7 @@ while (@levels) { close $stamp; } run_rsnapshot($level); +notify("Backup completed successfully\n"); # Размонтируем файловую систему, содержащую snapshot_root my @lines = `df $snapshot_root`; my @line = split(/\s+/,pop @lines); @@ -99,17 +107,34 @@ while (<$mount>) { } } close $mount; +my @baddevs=(); for $device (@to_umount) { print STDERR "umount $device\n"; - system("umount",$device); + if (system("umount",$device) !=0) { + push @baddevs, $device; + } } +if (@baddevs) { + notify("Cannot unmount device(s) ".join(", ",@baddevs).".\n") +} sub run_rsnapshot { my $level = shift; print STDERR "running rsnapshot $level\n"; my $status= system("rsnapshot",$level) >> 8; if ($status) { - print STDERR "rnsapshot $level finished with code $status\n"; + notify("rsapshot $level finished with code $status\n"); exit $status; } } +sub notify { + my $msg = shift; + print STDERR $msg; + my $notifier = "/usr/local/sbin/notify-messenger"; + if ( -x $notifier) { + $hostname = Sys::Hostname::hostname(); + open my $pipe,"|",$notifier; + print $pipe "$hostname: $msg"; + close $pipe; + } +} diff --git a/notify-messenger b/notify-messenger new file mode 100755 index 0000000..ac83092 --- /dev/null +++ b/notify-messenger @@ -0,0 +1,8 @@ +#!/bin/sh +# This and example of script called from backup script to notify +# sysadmin about backup status +# This version sends message to default matrix room configured in root's +# ~/.config/matrix-commander/credentials.json +# You can use XMPP or some proprietary messenger such as telegram +# instead +matrix-commander -m -