]> www.wagner.pp.ru Git - oss/restore.git/commitdiff
Added ability to send messages to instant messenger
authorVictor Wagner <vitus@wagner.pp.ru>
Mon, 23 Oct 2023 18:21:48 +0000 (21:21 +0300)
committerVictor Wagner <vitus@wagner.pp.ru>
Mon, 23 Oct 2023 18:21:48 +0000 (21:21 +0300)
backup
notify-messenger [new file with mode: 0755]

diff --git a/backup b/backup
index a6d01376f60205d6e2ec9279e59c940965a68272..d01a64cf2b40fc9479dddf949b57f042d8e28191 100755 (executable)
--- 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</usr/local/sbin/notify-messenger> 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 <vitus@wagner.pp.ru>
 
 =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 (executable)
index 0000000..ac83092
--- /dev/null
@@ -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 -