[freeside-commits] branch FREESIDE_3_BRANCH updated. e7c1b3f96941e30898bf0e8b4dd25abc1859757c

Jonathan Prykop jonathan at 420.am
Fri Jul 3 16:47:53 PDT 2015


The branch, FREESIDE_3_BRANCH has been updated
       via  e7c1b3f96941e30898bf0e8b4dd25abc1859757c (commit)
      from  e525acf0e959d93f54723eca1a5c0b0e9aab31a3 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit e7c1b3f96941e30898bf0e8b4dd25abc1859757c
Author: Jonathan Prykop <jonathan at freeside.biz>
Date:   Tue Jun 23 00:42:06 2015 -0500

    RT#29895: Send email when backup is completed

diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm
index 4c6b1e4..c5c0e46 100644
--- a/FS/FS/Conf.pm
+++ b/FS/FS/Conf.pm
@@ -779,6 +779,11 @@ sub reason_type_options {
   }
 }
 
+my $validate_email = sub { $_[0] =~
+                             /^[^@]+\@[[:alnum:]-]+(\.[[:alnum:]-]+)+$/
+                             ? '' : 'Invalid email address';
+                         };
+
 #Billing (81 items)
 #Invoicing (50 items)
 #UI (69 items)
@@ -1271,10 +1276,7 @@ sub reason_type_options {
     'description' => 'Return address on email invoices (address only, see invoice_from_name)',
     'type'        => 'text',
     'per_agent'   => 1,
-    'validate'    => sub { $_[0] =~
-                             /^[^@]+\@[[:alnum:]-]+(\.[[:alnum:]-]+)+$/
-                             ? '' : 'Invalid email address';
-                         }
+    'validate'    => $validate_email,
   },
 
   {
@@ -2782,6 +2784,14 @@ and customer address. Include units.',
   },
 
   {
+    'key'         => 'dump-email_to',
+    'section'     => '',
+    'description' => "Optional email address to send success/failure message for database dumps.",
+    'type'        => 'text',
+    'validate'    => $validate_email,
+  },
+
+  {
     'key'         => 'users-allow_comp',
     'section'     => 'deprecated',
     'description' => '<b>DEPRECATED</b>, enable the <i>Complimentary customer</i> access right instead.  Was: Usernames (Freeside users, created with <a href="../docs/man/bin/freeside-adduser.html">freeside-adduser</a>) which can create complimentary customers, one per line.  If no usernames are entered, all users can create complimentary accounts.',
diff --git a/FS/FS/Cron/backup.pm b/FS/FS/Cron/backup.pm
index 5feca26..cfc8e36 100644
--- a/FS/FS/Cron/backup.pm
+++ b/FS/FS/Cron/backup.pm
@@ -6,6 +6,7 @@ use Exporter;
 use File::Copy;
 use Date::Format;
 use FS::UID qw(driver_name datasrc);
+use FS::Misc qw( send_email );
 
 @ISA = qw( Exporter );
 @EXPORT_OK = qw( backup );
@@ -18,7 +19,8 @@ sub backup {
 
   my $filename = time2str('%Y%m%d%H%M%S',time);
 
-  datasrc =~ /dbname=([\w\.]+)$/ or die "unparsable datasrc ". datasrc;
+  datasrc =~ /dbname=([\w\.]+)$/
+    or backup_email_and_die($conf,$filename,"unparsable datasrc ". datasrc);
   my $database = $1;
 
   my $ext;
@@ -29,36 +31,71 @@ sub backup {
     system("mysqldump $database >/var/tmp/$database.sql");
     $ext = 'sql';
   } else {
-    die "database dumps not yet supported for ". driver_name;
+    backup_email_and_die($conf,$filename,"database dumps not yet supported for ". driver_name);
   }
   chmod 0600, "/var/tmp/$database.$ext";
 
   if ( $conf->config('dump-pgpid') ) {
     eval 'use GnuPG;';
-    die $@ if $@;
+    backup_email_and_die($conf,$filename,$@) if $@;
     my $gpg = new GnuPG;
     $gpg->encrypt( plaintext => "/var/tmp/$database.$ext",
                    output    => "/var/tmp/$database.gpg",
                    recipient => $conf->config('dump-pgpid'),
                  );
-    unlink "/var/tmp/$database.$ext" or die $!;
+    unlink "/var/tmp/$database.$ext"
+      or backup_email_and_die($conf,$filename,$!);
     chmod 0600, "/var/tmp/$database.gpg";
     $ext = 'gpg';
   }
 
   if ( $localdest ) {
-    copy("/var/tmp/$database.$ext", "$localdest/$filename.$ext") or die $!;
+    copy("/var/tmp/$database.$ext", "$localdest/$filename.$ext")
+      or backup_email_and_die($conf,$filename,$!);
     chmod 0600, "$localdest/$filename.$ext";
   }
 
   if ( $scpdest ) {
     eval "use Net::SCP qw(scp);";
-    die $@ if $@;
+    backup_email_and_die($conf,$filename,$@) if $@;
     scp("/var/tmp/$database.$ext", "$scpdest/$filename.$ext");
   }
 
-  unlink "/var/tmp/$database.$ext" or die $!;
+  unlink "/var/tmp/$database.$ext" or backup_email_and_die($conf,$filename,$!); #or just warn?
 
+  backup_email($conf,$filename);
+
+}
+
+#runs backup_email and dies with same error message
+sub backup_email_and_die {
+  my ($conf,$filename,$error) = @_;
+  backup_email($conf,$filename,$error);
+  warn "backup_email_and_die called without error message" unless $error;
+  die $error;
+}
+
+#checks if email should be sent, sends it
+sub backup_email {
+  my ($conf,$filename,$error) = @_;
+  my $to = $conf->config('dump-email_to');
+  return unless $to;
+  my $result = $error ? 'FAILED' : 'succeeded';
+  my $email_error = send_email(
+    'from'    => $conf->config('invoice_from'), #or whatever, don't think it matters
+    'to'      => $to,
+    'subject' => 'FREESIDE NOTIFICATION: Backup ' . $result,
+    'body'    => [ 
+      "This is an automatic message from your Freeside installation.\n",
+      "Freeside backup $filename $result",
+      ($error ? " with the following error:\n\n" : "\n"),
+      ($error || ''),      
+      "\n",
+    ],
+    'msgtype' => 'admin',
+  );
+  warn $email_error if $email_error;
+  return;
 }
 
 1;

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/Conf.pm        |   18 ++++++++++++++----
 FS/FS/Cron/backup.pm |   51 +++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 58 insertions(+), 11 deletions(-)




More information about the freeside-commits mailing list