diff options
-rw-r--r-- | FS/FS/Conf.pm | 11 | ||||
-rw-r--r-- | FS/FS/Cron/backup.pm | 75 | ||||
-rwxr-xr-x | FS/bin/freeside-daily | 4 | ||||
-rw-r--r-- | bin/freeside-backup | 6 |
4 files changed, 61 insertions, 35 deletions
diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index a6feffe5c..7c7c6ed72 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -312,7 +312,7 @@ sub delete { my($name, $agentnum) = @_; if ( my $cv = FS::Record::qsearchs('conf', {name => $name, agentnum => $agentnum}) ) { - warn "[FS::Conf] DELETE $name\n"; + warn "[FS::Conf] DELETE $name\n" if $DEBUG; my $oldAutoCommit = $FS::UID::AutoCommit; local $FS::UID::AutoCommit = 0; @@ -2201,9 +2201,16 @@ and customer address. Include units.', }, { + 'key' => 'dump-localdest', + 'section' => '', + 'description' => 'Destination for local database dumps (full path)', + 'type' => 'text', + }, + + { 'key' => 'dump-scpdest', 'section' => '', - 'description' => 'destination for scp database dumps: user@host:/path', + 'description' => 'Destination for scp database dumps: user@host:/path', 'type' => 'text', }, diff --git a/FS/FS/Cron/backup.pm b/FS/FS/Cron/backup.pm index 3c0d63bdd..5feca2636 100644 --- a/FS/FS/Cron/backup.pm +++ b/FS/FS/Cron/backup.pm @@ -3,43 +3,62 @@ package FS::Cron::backup; use strict; use vars qw( @ISA @EXPORT_OK ); use Exporter; +use File::Copy; use Date::Format; use FS::UID qw(driver_name datasrc); @ISA = qw( Exporter ); -@EXPORT_OK = qw( backup_scp ); +@EXPORT_OK = qw( backup ); -sub backup_scp { +sub backup { my $conf = new FS::Conf; - my $dest = $conf->config('dump-scpdest'); - if ( $dest ) { - $dest .= time2str('/%Y%m%d%H%M%S',time); - datasrc =~ /dbname=([\w\.]+)$/ or die "unparsable datasrc ". datasrc; - my $database = $1; + my $localdest = $conf->config('dump-localdest'); + my $scpdest = $conf->config('dump-scpdest'); + return unless $localdest || $scpdest; + + my $filename = time2str('%Y%m%d%H%M%S',time); + + datasrc =~ /dbname=([\w\.]+)$/ or die "unparsable datasrc ". datasrc; + my $database = $1; + + my $ext; + if ( driver_name eq 'Pg' ) { + system("pg_dump -Fc $database >/var/tmp/$database.Pg"); + $ext = 'Pg'; + } elsif ( driver_name eq 'mysql' ) { + system("mysqldump $database >/var/tmp/$database.sql"); + $ext = 'sql'; + } else { + die "database dumps not yet supported for ". driver_name; + } + chmod 0600, "/var/tmp/$database.$ext"; + + if ( $conf->config('dump-pgpid') ) { + eval 'use GnuPG;'; + die $@ 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 $!; + chmod 0600, "/var/tmp/$database.gpg"; + $ext = 'gpg'; + } + + if ( $localdest ) { + copy("/var/tmp/$database.$ext", "$localdest/$filename.$ext") or die $!; + chmod 0600, "$localdest/$filename.$ext"; + } + + if ( $scpdest ) { eval "use Net::SCP qw(scp);"; die $@ if $@; - if ( driver_name eq 'Pg' ) { - system("pg_dump -Fc $database >/var/tmp/$database.Pg") - } else { - die "database dumps not yet supported for ". driver_name; - } - if ( $conf->config('dump-pgpid') ) { - eval 'use GnuPG;'; - die $@ if $@; - my $gpg = new GnuPG; - $gpg->encrypt( plaintext => "/var/tmp/$database.Pg", - output => "/var/tmp/$database.gpg", - recipient => $conf->config('dump-pgpid'), - ); - chmod 0600, '/var/tmp/$database.gpg'; - scp("/var/tmp/$database.gpg", "$dest.gpg"); - unlink "/var/tmp/$database.gpg" or die $!; - } else { - chmod 0600, '/var/tmp/$database.Pg'; - scp("/var/tmp/$database.Pg", "$dest.Pg"); - } - unlink "/var/tmp/$database.Pg" or die $!; + scp("/var/tmp/$database.$ext", "$scpdest/$filename.$ext"); } + + unlink "/var/tmp/$database.$ext" or die $!; + } 1; diff --git a/FS/bin/freeside-daily b/FS/bin/freeside-daily index 2c9dd8b1b..ac0a82391 100755 --- a/FS/bin/freeside-daily +++ b/FS/bin/freeside-daily @@ -51,8 +51,8 @@ if ( $opt{u} ) { } #you can skip this just by not having the config -use FS::Cron::backup qw(backup_scp); -backup_scp(); +use FS::Cron::backup qw(backup); +backup(); #same use FS::Cron::rt_tasks qw(rt_escalate); diff --git a/bin/freeside-backup b/bin/freeside-backup index 97a4899e6..25e74a451 100644 --- a/bin/freeside-backup +++ b/bin/freeside-backup @@ -9,8 +9,8 @@ my $user = shift or die &usage; adminsuidsetup $user; #you can skip this just by not having the config -use FS::Cron::backup qw(backup_scp); -backup_scp(); +use FS::Cron::backup qw(backup); +backup(); sub usage { die "Usage:\n\n freeside-backup user\n"; @@ -30,7 +30,7 @@ freeside-backup - Runs a backup =head1 DESCRIPTION -Runs a backup. See the dump-scpdest configuration option. +Runs a backup. See the dump-scpdest and dump-localdest configuration options. =head1 BUGS |