RT#37908: Convert existing email-sending code to use common interface [removals and...
[freeside.git] / FS / FS / Cron / backup.pm
index cfc8e36..a192ca9 100644 (file)
@@ -6,7 +6,7 @@ use Exporter;
 use File::Copy;
 use Date::Format;
 use FS::UID qw(driver_name datasrc);
-use FS::Misc qw( send_email );
+use FS::Log
 
 @ISA = qw( Exporter );
 @EXPORT_OK = qw( backup );
@@ -20,7 +20,7 @@ sub backup {
   my $filename = time2str('%Y%m%d%H%M%S',time);
 
   datasrc =~ /dbname=([\w\.]+)$/
-    or backup_email_and_die($conf,$filename,"unparsable datasrc ". datasrc);
+    or backup_log_and_die($filename,"unparsable datasrc ". datasrc);
   my $database = $1;
 
   my $ext;
@@ -31,70 +31,61 @@ sub backup {
     system("mysqldump $database >/var/tmp/$database.sql");
     $ext = 'sql';
   } else {
-    backup_email_and_die($conf,$filename,"database dumps not yet supported for ". driver_name);
+    backup_log_and_die($filename,"database dumps not yet supported for ". driver_name);
   }
   chmod 0600, "/var/tmp/$database.$ext";
 
   if ( $conf->config('dump-pgpid') ) {
     eval 'use GnuPG;';
-    backup_email_and_die($conf,$filename,$@) if $@;
+    backup_log_and_die($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 backup_email_and_die($conf,$filename,$!);
+      or backup_log_and_die($filename,$!);
     chmod 0600, "/var/tmp/$database.gpg";
     $ext = 'gpg';
   }
 
   if ( $localdest ) {
     copy("/var/tmp/$database.$ext", "$localdest/$filename.$ext")
-      or backup_email_and_die($conf,$filename,$!);
+      or backup_log_and_die($filename,$!);
     chmod 0600, "$localdest/$filename.$ext";
   }
 
   if ( $scpdest ) {
     eval "use Net::SCP qw(scp);";
-    backup_email_and_die($conf,$filename,$@) if $@;
+    backup_log_and_die($filename,$@) if $@;
     scp("/var/tmp/$database.$ext", "$scpdest/$filename.$ext");
   }
 
-  unlink "/var/tmp/$database.$ext" or backup_email_and_die($conf,$filename,$!); #or just warn?
+  unlink "/var/tmp/$database.$ext" or backup_log_and_die($filename,$!); #or just warn?
 
-  backup_email($conf,$filename);
+  backup_log($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;
+#runs backup_log and dies with same error message
+sub backup_log_and_die {
+  my ($filename,$error) = @_;
+  $error = "backup_log_and_die called without error message" unless $error;
+  backup_log($filename,$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;
+#logs result
+sub backup_log {
+  my ($filename,$error) = @_;
+  my $result = $error ? "FAILED: $error" : 'succeeded';
+  my $message = "backup $filename $result\n";
+  my $log = FS::Log->new('Cron::backup');
+  if ($error) {
+    $log->error($message);
+  } else {
+    $log->info($message);
+  }
   return;
 }