File: //proc/self/root/scripts.20110531.215904.25158/cpbackup
#!/usr/bin/perl
# cpanel - cpbackup Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use strict;
use Cwd ();
use Cpanel::LoadFile ();
use Cpanel::Sys::OS ();
use Cpanel::Hostname ();
use Cpanel::FindBin ();
use Cpanel::OSSys ();
use Cpanel::Cpu ();
use Cpanel::Logger ();
use Cpanel::FileUtils::TouchFile ();
use Cpanel::Config::Backup ();
use Cpanel::Config::LoadConfig ();
use Cpanel::Config::FlushConfig ();
use Cpanel::Config::LoadCpConf ();
use Cpanel::Config::Users ();
use Cpanel::MysqlUtils ();
use Cpanel::SafeRun::Errors ();
use Cpanel::SimpleSync ();
use Cpanel::Tar ();
use Cpanel::Gzip::Config ();
my $logger = Cpanel::Logger->new();
$ENV{'PATH'} .= ":/sbin";
our $logdir = '/usr/local/cpanel/logs/cpbackup';
my $force = 0;
if ( $#ARGV != -1 && $ARGV[0] eq '--force' ) {
$force = 1;
shift @ARGV;
}
my %CPCONF = Cpanel::Config::LoadCpConf::loadcpconf();
my $previous_pid;
if ( -e '/var/cpanel/backuprunning' ) {
$previous_pid = Cpanel::LoadFile::loadfile('/var/cpanel/backuprunning');
chomp $previous_pid;
}
if ( $previous_pid && kill( 0, $previous_pid ) > 0 ) {
if ( -t STDIN ) {
print "[cpbackup] Backup process currently running. pid: $previous_pid\n";
if ( opendir my $logdir_dh, $logdir ) {
my @logs = readdir $logdir_dh;
closedir $logdir_dh;
@logs = grep( /^\d+\.log$/, sort @logs );
my $log = pop @logs;
print "[cpbackup] Backup log file: $logdir/$log\n";
}
}
exit 1;
}
my $gzip_cfg = Cpanel::Gzip::Config::load_gzipcfg();
my $gzip_bin = $gzip_cfg->{'bin'};
if ( !$gzip_bin ) {
Cpanel::Logger::logger(
{
'message' => 'Unable to locate suitable gzip binary',
'service' => 'cpbackup',
'level' => 'die',
'output' => 2,
'backtrace' => 0,
}
);
}
my $rsync_bin = Cpanel::FindBin::findbin('rsync');
if ( !$rsync_bin ) {
Cpanel::Logger::logger(
{
'message' => 'Unable to locate suitable rsync binary',
'service' => 'cpbackup',
'level' => 'die',
'output' => 2,
'backtrace' => 0,
}
);
}
my $tarcfg = Cpanel::Tar::load_tarcfg();
my ( $status, $message ) = Cpanel::Tar::checkperm();
exit 1 if !$status; # No need to show message here, handled in checkperm routine
my $tar_bin = $tarcfg->{'bin'};
my $mount_bin = Cpanel::FindBin::findbin('mount');
if ( !$mount_bin ) {
Cpanel::Logger::logger(
{
'message' => 'Unable to locate suitable mount binary',
'service' => 'cpbackup',
'level' => 'die',
'output' => 2,
'backtrace' => 0,
}
);
}
my $umount_bin = Cpanel::FindBin::findbin('umount');
if ( !$umount_bin ) {
Cpanel::Logger::logger(
{
'message' => 'Unable to locate suitable umount binary',
'service' => 'cpbackup',
'level' => 'die',
'output' => 2,
'backtrace' => 0,
}
);
}
my $losetup_bin = Cpanel::FindBin::findbin('losetup');
my $loopdev;
my $now = time;
my $system = ( Cpanel::OSSys::uname() )[0];
my $host = Cpanel::Hostname::gethostname();
my $os = Cpanel::Sys::OS::getos();
my $rsyncopts = '-rlptD';
#recusive, copy symlinks as symlinks, preserve permissions,
#preserve times, preserve devices
$ENV{'CPBACKUP'} = 1;
if ( !$CPCONF{'skipnotifyacctbackupfailure'} ) {
$ENV{'CPBACKUP_NOTIFY_FAIL'} = 1;
}
my %CONF = Cpanel::Config::Backup::load();
my $pkgacct =
( exists $CONF{'USEBINARYPKGACCT'} && $CONF{'USEBINARYPKGACCT'} eq 'yes' && -x '/usr/local/cpanel/bin/pkgacct' )
? '/usr/local/cpanel/bin/pkgacct'
: '/scripts/pkgacct';
if ( $#ARGV != -1 && $ARGV[0] eq '--allow-override' ) {
shift @ARGV;
if ( -e '/var/cpanel/lib/Whostmgr/Pkgacct/pkgacct' && -x _ ) {
$pkgacct = '/var/cpanel/lib/Whostmgr/Pkgacct/pkgacct';
}
}
my $has_link_dest = 0;
if ( $CONF{'LINKDEST'} && ( $CONF{'LINKDEST'} eq 'yes' || $CONF{'LINKDEST'} eq '1' ) ) {
my $rsync_help = Cpanel::SafeRun::Errors::saferunallerrors( $rsync_bin, '--help' );
$has_link_dest = ( $rsync_help =~ /link-dest/ ? 1 : 0 );
}
if ($has_link_dest) {
print "[cpbackup] EXPERIMENTAL Hard Linking enabled (link-dest)\n";
}
if ( ( !exists $CONF{'PREBACKUP'} || $CONF{'PREBACKUP'} eq '-1' || $CONF{'PREBACKUP'} eq '' ) && -e '/scripts/precpbackup' && -x _ ) {
if ( !exists $CONF{'PREBACKUP'} ) {
my $message = <<"EOM";
The backup process has detected the existence of /scripts/precpbackup. In earlier versions of cpbackup,
this script would be executed prior to any checks or verifications and would be executed regardless of
whether the backups were up to date.
If you are certain that you would like to retain this deprecated behavior, then please edit the configuration
file /etc/cpbackup.conf and add or replace the setting for "PREBACKUP" so that the value is "-1". The following
shell command executed on the system will perform this task:
echo "PREBACKUP -1" >> /etc/cpbackup.conf
Until the configuration file is updated, the legacy behavior will be retained and you will continue to receive
this message once per day each time the backup process is executed.
Please visit https://$host:2087/scripts/backupset to adjust your backup configuration.
EOM
if ( my $pid = fork() ) {
waitpid( $pid, 0 );
}
else {
require Cpanel::Notify;
Cpanel::Notify::notification(
'application' => 'cpbackup',
'status' => 'prebackup deprecation',
'priority' => 2,
'interval' => 60 * 60 * 24, # Once per day
'subject' => qq{Deprecation notice for /scripts/precpbackup on $host},
'message' => $message,
'msgtype' => ''
);
exit(0);
}
}
print "[cpbackup] Executing user defined pre backup script (/scripts/precpbackup) [LEGACY].\n";
system '/scripts/precpbackup';
$CONF{'PREBACKUP'} = 0; # Prevent from running again
}
if ( exists $CONF{'GZIPRSYNCOPTS'} ) {
$ENV{'GZIP'} .= $CONF{'GZIPRSYNCOPTS'};
}
else {
$CONF{'GZIPRSYNCOPTS'} = $gzip_cfg->{'rsyncable'} ? '--rsyncable' : '';
$ENV{'GZIP'} .= $CONF{'GZIPRSYNCOPTS'};
Cpanel::Config::Backup::save( \%CONF );
}
$ENV{'pkgacct-cpbackup'} = 1;
Cpanel::OSSys::nice(18); # needs to be one higher for cpuwatch
#BACKUPDIR /backup
#BACKUPINT daily
#BACKUPENABLE yes
#BACKUPCHECK yes
#BACKUPMOUNT no
#BACKUPINC no
#BACKUPFILES yes
#BACKUPACCTS
#DIEIFNOTMOUNTED no
#BACKUPDAYS 0,1,2,3,4,5,6
#BACKUPLOGS no
#MYSQLBACKUP accounts
#BACKUPTYPE normal,ftp
if ( $> == 0 ) {
$ENV{'USER'} = 'root';
$ENV{'HOME'} = '/root';
}
my $mountkeyword = 'remount';
my $cpunum = Cpanel::Cpu::getcpucount();
if ( $system =~ m/freebsd/i ) {
$mountkeyword = 'update';
}
my %SKIPUSERS;
if ( open my $userskip_fh, '<', '/etc/cpbackup-userskip.conf' ) {
while (<$userskip_fh>) {
chomp;
$SKIPUSERS{$_} = 1;
}
close $userskip_fh;
}
my @FILES = qw(
/etc/exim.conf
/etc/exim.conf.local
/etc/exim.conf.localopts
/etc/namedb/named.conf
/etc/rc.conf
/etc/named.conf
/etc/proftpd.conf
/etc/localdomains
/etc/httpd/conf/httpd.conf
/usr/local/apache/conf/httpd.conf
/etc/group
/etc/shadow
/etc/master.passwd
/etc/passwd
/etc/fstab
/root/.my.cnf
/etc/ips
/etc/ips.remotemail
/etc/ips.remotedns
/etc/reservedips
/etc/reservedipreasons
/etc/quota.conf
/etc/wwwacct.conf
/etc/remotedomains
/etc/rndc.conf
/etc/secondarymx
/etc/my.cnf
/usr/local/cpanel/3rdparty/interchange/interchange.cfg
);
my @DIRS = qw(
/etc/namedb
/var/lib/rpm
/var/lib/named/chroot/var/named/master
/etc/valiases
/etc/proftpd
/var/named
/etc/vfilters
/var/cpanel
/var/spool/cron
/var/cron/tabs
/var/spool/fcron
/usr/local/frontpage
/var/log/bandwidth
/etc/vdomainaliases
/usr/share/ssl
/etc/ssl
/var/ssl
/usr/local/cpanel/3rdparty/mailman
);
#BACKUPLOGS no
#MYSQLBACKUP accounts
if ( $CONF{'MYSQLBACKUP'} eq "both" || $CONF{'MYSQLBACKUP'} eq 'dir' ) {
my $mysqldatadir = Cpanel::MysqlUtils::getmysqldir() || '/var/lib/mysql';
while ( -l $mysqldatadir ) {
$mysqldatadir = readlink($mysqldatadir);
}
push @DIRS, $mysqldatadir;
}
if ( $CONF{'BACKUPENABLE'} ne "yes" ) {
if ( -t \*STDOUT ) {
print STDERR "[cpbackup] Backup Not Enabled (This can be adjusted in WHM => Backup => Configure Backup)\n";
}
exit 1;
}
if ( $CONF{'BACKUPENABLE'} ne "restoreonly" && $CONF{'BACKUPENABLE'} ne "yes" ) {
print STDERR "[cpbackup] Backup Not Enabled (restore only) (This can be adjusted in WHM => Backup => Configure Backup)\n";
exit 1;
}
if ( $CONF{'BACKUPCHECK'} ne "yes" ) {
print STDERR "[cpbackup] Backup must be re-enabled in your WHM (This can be adjusted in WHM => Backup => Configure Backup)\n";
exit 1;
}
if ( $CONF{'BACKUPDAYS'} ne "" ) {
my @BACKUPDAYS = split( /\,/, $CONF{'BACKUPDAYS'} );
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime($now);
my $backupthisday = 0;
foreach (@BACKUPDAYS) {
if ( $wday eq $_ ) {
$backupthisday = 1;
}
}
if ( !$backupthisday && !$force ) {
print STDERR "[cpbackup] Backups are not scheduled to run today (This can be adjusted in WHM => Backup => Configure Backup)\n";
exit 1;
}
}
my $basedir = $CONF{'BACKUPDIR'};
if ( $CONF{'BACKUPTYPE'} eq "ftp" ) {
my $ftpuser = $CONF{'BACKUPFTPUSER'};
my $ftppass = $CONF{'BACKUPFTPPASS'};
if ( !defined($ftpuser) || $ftpuser eq '' ) {
my $status = 'FTP user for backups is not defined.';
my $subject = $status;
exit_stderr_log_and_notify( $status, $subject );
}
elsif ( !defined($ftppass) || $ftppass eq '' ) {
my $status = "FTP password for $ftpuser is not defined.";
my $subject = $status;
exit_stderr_log_and_notify( $status, $subject );
}
my $net_ftp_ok = 0;
eval { require Net::FTP; $net_ftp_ok = 1; };
if ( !$net_ftp_ok ) {
my $status = 'loading Net::FTP failed';
my $subject = qq{Remote FTP backup failed because Net::FTP could not be loaded on $host};
exit_stderr_log_and_notify( $status, $subject );
}
my $ftppassive = ( $CONF{'BACKUPFTPPASSIVE'} eq 'yes' );
my $ftphost = $CONF{'BACKUPFTPHOST'};
my $ftp = Net::FTP->new(
$ftphost,
Debug => 1,
Passive => $ftppassive
);
unless ($ftp) {
my $status = "Unable to connect to remote FTP server ($ftphost).";
my $subject = qq{Remote FTP backup on $host failed because unable to connect to remote FTP server ($ftphost)};
exit_stderr_log_and_notify( $status, $subject );
}
if ( !$ftp->login( $ftpuser, $ftppass ) ) {
$ftp->quit();
my $status = 'Unable to login to remote FTP server.';
my $subject = $status;
exit_stderr_log_and_notify( $status, $subject );
}
$ftp->quit();
mkdir( "/home/cpbackuptmp", 0711 );
chmod( 0711, "/home/cpbackuptmp" );
$basedir = "/home/cpbackuptmp";
$CONF{'BACKUPINC'} = "no";
$CONF{'BACKUPFILES'} = "no";
$CONF{'BACKUPACCTS'} = "yes";
$CONF{'BACKUPMOUNT'} = "no";
$CONF{'DIEIFNOTMOUNTED'} = "no";
}
my $basemount = $basedir;
if ( $CONF{'BACKUPMOUNT'} eq 'yes' ) {
mount_backup_disk();
}
die_if_not_mounted_check();
if ( !$basedir ) {
if ( !-t STDIN ) {
require Cpanel::iContact;
Cpanel::iContact::icontact(
'application' => 'cpbackup',
'level' => 1,
'subject' => qq{Backup not completed on $host},
'message' => qq{The backup was not able to be completed because the backup directory is not specified.},
'msgtype' => ''
);
}
print STDERR "[cpbackup] Backup failed! Base directory for backups not set!\n";
exit 1;
}
chmod( 0711, $basedir );
$basedir .= '/cpbackup';
mkdir( $basedir, 0711 );
chmod( 0711, $basedir );
# Create log dir and rotate log files
setup_and_clean_logs();
if ( my $pid = fork() ) {
if ( -t STDIN ) {
print "[cpbackup] Process started in background.\n";
print '[cpbackup] Log file: ' . $logdir . '/' . $now . '.log' . "\n";
}
exit 0;
}
else {
sysopen( BCKLOG, $logdir . '/' . $now . '.log', &Fcntl::O_WRONLY | &Fcntl::O_TRUNC | &Fcntl::O_CREAT, 0600 ) || die "Could not open backup log: $logdir" . '/' . $now . '.log';
open( STDOUT, '>&BCKLOG' );
open( STDERR, '>&BCKLOG' );
open( STDIN, '<', '/dev/null' );
Cpanel::OSSys::setsid();
# Do not close fds here as this will cause an fd leak when run from crontab
}
$ENV{'CPBACKUP_LOGFILE'} = $logdir . '/' . $now . '.log';
my $original_child_pid = $$;
$SIG{'TERM'} = $SIG{__DIE__} = sub {
if ( $$ == $original_child_pid ) {
unlink '/var/cpanel/backuprunning';
}
};
open( my $bck_lock, '>', '/var/cpanel/backuprunning' );
print $bck_lock $$;
close($bck_lock);
my $localtime = localtime($now);
print "[cpbackup] Started at $localtime\n";
if ( $CONF{'PREBACKUP'} && -e '/scripts/precpbackup' && -x _ ) {
print "[cpbackup] Executing user defined pre backup script (/scripts/precpbackup).\n";
system '/scripts/precpbackup';
}
#
# If precpbackup unmounts the disk, mount it again
#
if ( $CONF{'BACKUPMOUNT'} eq 'yes' && !( backup_disk_is_mounted() )[0] ) {
mount_backup_disk();
die_if_not_mounted_check();
}
my %BACKUP_TTLS = (
'monthly' => 29,
'weekly' => 6,
'daily' => .5
);
my ( @backups_to_run, @backups_to_skip ); # We are using an array becuse the order of the backups matter
# when we push them in below the first backup will be the one we run
# anything after the first are just retention backups which may or may be due
# to be copied from the backup we just made.
#
# Pick the first backup based on the BACKUPINT setting
#
if ( exists $BACKUP_TTLS{ $CONF{'BACKUPINT'} } ) {
if ( $force || !-e $basedir . '/' . $CONF{'BACKUPINT'} || isolderthen( $BACKUP_TTLS{ $CONF{'BACKUPINT'} }, $basedir . '/' . $CONF{'BACKUPINT'} ) ) {
push @backups_to_run, $CONF{'BACKUPINT'};
}
else {
push @backups_to_skip, $CONF{'BACKUPINT'};
}
}
#
# If we are doing daily backups we have the option to retain weekly backups
#
if ( $CONF{'BACKUPINT'} eq "daily" && $CONF{'BACKUPRETWEEKLY'} ) {
if ( !-e "${basedir}/weekly" || isolderthen( $BACKUP_TTLS{'weekly'}, "${basedir}/weekly" ) ) {
push @backups_to_run, 'weekly';
}
else {
push @backups_to_skip, 'weekly';
}
}
#
# If we are doing daily or weekly backups we have the option to retain monthly backups
#
if ( ( $CONF{'BACKUPINT'} eq "weekly" || $CONF{'BACKUPINT'} eq "daily" ) && $CONF{'BACKUPRETMONTHLY'} ) {
if ( !-e "${basedir}/monthly" || isolderthen( $BACKUP_TTLS{'monthly'}, "${basedir}/monthly" ) ) {
push @backups_to_run, 'monthly';
}
else {
push @backups_to_skip, 'monthly';
}
}
#
# Only send the message if there are backups to run
#
my $send_message = ( @backups_to_run ? 1 : 0 );
if (@backups_to_run) {
print "[cpbackup] The following backups will be updated: " . join( ',', @backups_to_run ) . "\n";
my $current_backup = $backups_to_run[0];
mkdir( $basedir . '/' . $current_backup, 0711 ) if ( !-e $basedir . '/' . $current_backup );
chmod( 0711, $basedir . '/' . $current_backup );
backupaccts( $basedir . '/' . $current_backup, \@backups_to_run ); # we include the list of backups to run so we can avoid
# having to do the backup process for each backup when ftping
update_last_run_time( $basedir . '/' . $current_backup );
#
# Make a copy of the current_backup (BACKUPINT setting) into the next type we need to retain
#
shift(@backups_to_run); # the first one is the kind of backup we are running
# The next are the ones we need to keep (they are only pushed in if we need to make a copy)
while ( my $retention_backup = shift @backups_to_run ) { # We are shifting them out as they are done
# so we know they have been completed
next if ( !$retention_backup );
print "[cpbackup] Copying $current_backup backups to $retention_backup backups for retention\n";
cpusystem( $rsync_bin, $rsyncopts, "--delete", ( $has_link_dest ? ( '--link-dest=../' . $current_backup ) : () ), "$basedir/" . $current_backup . '/', "$basedir/" . $retention_backup );
# case 42741: no need to update the file again as it will be copied from the $current_backup
#update_last_run_time( $basedir . '/' . $retention_backup );
last; # Only do one per run so at least we have one day where monthly and weekly backups are not the same at the end of the month
}
if (@backups_to_run) {
print "[cpbackup] The following backups will be updated next time cpbackup runs: " . join( ',', @backups_to_run ) . "\n"; # we do not want to cobber the monthly backups
# at the end of the month so we will wait till the next run
}
}
if (@backups_to_skip) {
print "[cpbackup] The following backups were already up to date: " . join( ',', @backups_to_skip ) . "\n";
}
if ( -e '/scripts/postcpbackup' && -x _ && ( !exists $CONF{'POSTBACKUP'} || $CONF{'POSTBACKUP'} ) ) {
print "[cpbackup] Executing user defined post backup script (/scripts/postcpbackup).\n";
system '/scripts/postcpbackup';
}
if ( $CONF{'BACKUPMOUNT'} eq 'yes' ) {
unmount_backup_disk();
}
unlink '/var/cpanel/backuprunning';
my $complete_localtime = localtime();
print "[cpbackup] Completed at $complete_localtime\n";
if ($send_message) {
require Cpanel::Notify;
Cpanel::Notify::notification(
'application' => 'cpbackup',
'status' => '',
'priority' => 2,
'interval' => 60,
'subject' => qq{[cpbackup] Backup complete on $host},
'attach_files' => [ $logdir . '/' . $now . '.log' ],
'message' => qq{Please see the attached log file.},
'msgtype' => ''
);
}
sub isolderthen {
my ( $days, $file ) = @_;
return 1 if !-e $file;
my $last_run_time = ( stat($file) )[9];
# We want the trust the earliest time so we do not miss out on a backup. Its better to backup again
# if we are unsure. We now create a cpbackupstatus.cfg if it is missing
if ( -e $file . '/cpbackupstatus.cfg' ) {
my $backup_status_ref = Cpanel::Config::LoadConfig::loadConfig( $file . '/cpbackupstatus.cfg', {}, '=' );
# Always take the oldest date
if ( exists $backup_status_ref->{'last_run_time'} && $backup_status_ref->{'last_run_time'} && $backup_status_ref->{'last_run_time'} < $last_run_time ) {
$last_run_time = $backup_status_ref->{'last_run_time'};
}
}
else {
my $backup_status_ref = { 'last_run_time' => $last_run_time };
Cpanel::Config::FlushConfig::flushConfig( $file . '/cpbackupstatus.cfg', $backup_status_ref, '=' );
}
my $days_old = ( time() - ($last_run_time) ) / ( 60 * 60 * 24 ); #9 =mtime
print "[cpbackup] backup point $file is " . sprintf( '%0.2f', $days_old ) . " days old\n";
return ( ( $days_old < 0 || $days_old > $days ) ? 1 : 0 ); #case 19450:: now time warp safe
}
sub update_last_run_time {
my $dir = shift;
my $time = shift || time();
my $backup_status_ref;
if ( -e $dir . '/cpbackupstatus.cfg' ) {
$backup_status_ref = Cpanel::Config::LoadConfig::loadConfig( $dir . '/cpbackupstatus.cfg', {}, '=' );
}
$backup_status_ref->{'last_run_time'} = $time;
Cpanel::Config::FlushConfig::flushConfig( $dir . '/cpbackupstatus.cfg', $backup_status_ref, '=' );
}
sub backupaccts {
my $target = shift;
my $all_targets = shift;
Cpanel::FileUtils::TouchFile::touchfile($target);
#if another cpbackup starts just go bye bye
if ( !( $CONF{'BACKUPFILES'} eq "no" ) ) {
print "[cpbackup] Running dir & file backup with target : $target\n";
if ( !-e "$target/files" ) {
mkdir( "$target/files", 0700 );
}
if ( !-e "$target/dirs" ) {
mkdir( "$target/dirs", 0700 );
}
chmod(0700,"$target/files","$target/dirs");
foreach my $file (@FILES) {
next if ( !-e $file );
my $rawfile = $file;
$rawfile =~ s/\//_/g;
if ( $CONF{'BACKUPINC'} eq "yes" ) {
Cpanel::SimpleSync::syncfile( $file, "$target/files/$rawfile", 0, 1 );
}
else {
Cpanel::SimpleSync::syncfile( $file, "$target/files/$rawfile", 0, 1 );
cpusystem( $gzip_bin, "-f", "$target/files/$rawfile" );
}
}
foreach my $dir (@DIRS) {
next if ( !-e $dir );
my $rawdir = $dir;
$rawdir =~ s/\//_/g;
my @EXCLUDES = ($dir eq '/var/cpanel' ?
("--exclude=lastrun/*","--exclude=bwusagecache/*","--exclude=serviceauth/*","--exclude=dnsrequests_db/*","--exclude=configs.cache/*") :
("--exclude=*/proc/*"));
# Added exclude for /proc for chroot bind setups to prevent error messages
if ( $CONF{'BACKUPINC'} eq "yes" ) {
cpusystem( $rsync_bin, $rsyncopts, @EXCLUDES, '--delete', "$dir/", "$target/dirs/$rawdir" );
}
else {
cpusystem( $tar_bin, 'cfzp', "$target/dirs/$rawdir.tar.gz", @EXCLUDES, $dir );
chmod( 0600, "$target/dirs/$rawdir.tar.gz" );
}
}
}
print "[cpbackup] Running account backup with target : $target\n";
#BACKUPLOGS no
#MYSQLBACKUP accounts
$ENV{'pkgacct-logs'} = ( $CONF{'BACKUPLOGS'} eq "yes" ? 'yes' : 'no' );
$ENV{'pkgacct-mysql'} = ( ( $CONF{'MYSQLBACKUP'} eq "dir" || $CONF{'MYSQLBACKUP'} eq "no" ) ? 'no' : 'yes' );
$ENV{'pkgacct-psql'} = ( $CONF{'PSQLBACKUP'} eq "no" ? 'no' : 'yes' );
if ( $CONF{'BACKUPACCTS'} ne 'no' ) {
my @cpusers = Cpanel::Config::Users::getcpusers();
foreach my $user (@cpusers) {
next if ( $SKIPUSERS{$user} );
if ( $CONF{'BACKUPINC'} ne 'yes' ) {
if ( -d "$target/${user}" ) {
#rm_bin was empty so this never worked .. Not enabling it because someone
#may have been counting on this -jnk 10/20/08
#cpusystem( $rm_bin, '-rf', $target . '/' . $user );
}
}
{
local $ENV{'INCBACKUP'} = 1 if $CONF{'BACKUPINC'} eq "yes";
cpusystem( $pkgacct, $user, $target, 'backup', ( $CONF{'COMPRESSACCTS'} eq 'no' ? 'nocompress' : () ) );
}
if ( $CONF{'BACKUPTYPE'} eq 'ftp' ) {
my $target_file = ( $CONF{'COMPRESSACCTS'} eq 'no' ? "$user.tar" : "$user.tar.gz" );
foreach my $remote_target (@$all_targets) { # If we are going to update multiple targets we send the file multiple times
# previously we would build the backup once for every target. Now we just upload it once
# instead as we know all the targets before we get here now
ftpsend( $remote_target, $target . '/' . $target_file, $target_file );
}
unlink( $target . '/' . $target_file );
}
}
}
}
sub ftpsend {
my ( $targetdir, $targetfile, $remotefile ) = @_;
my $ftpuser = $CONF{'BACKUPFTPUSER'};
my $ftppass = $CONF{'BACKUPFTPPASS'};
my $ftproot = $CONF{'BACKUPFTPDIR'};
my $ftphost = $CONF{'BACKUPFTPHOST'};
my $ftppassive = ( $CONF{'BACKUPFTPPASSIVE'} eq 'yes' ? 1 : 0 );
my @FD = split( /\//, $targetdir );
my $backtype = $FD[$#FD];
my $ftp = Net::FTP->new(
$ftphost,
Debug => 1,
Passive => $ftppassive
);
unless ($ftp) {
my $msg = 'Unable to connect to remote FTP server.';
print "$msg\n";
Cpanel::Logger::logger(
{
'message' => $msg,
'service' => 'cpbackup',
'level' => 'die',
'output' => 2,
'backtrace' => 0,
}
);
return;
}
if ( !$ftp->login( $ftpuser, $ftppass ) ) {
$ftp->quit();
my $msg = 'Unable to login to remote FTP server.';
print "$msg\n";
Cpanel::Logger::logger(
{
'message' => $msg,
'service' => 'cpbackup',
'level' => 'die',
'output' => 2,
'backtrace' => 0,
}
);
return;
}
$ftp->binary();
if ( $ftproot eq "" ) {
$ftp->mkdir("cpbackup");
$ftp->mkdir("cpbackup/${backtype}");
$ftp->cwd("cpbackup/${backtype}");
}
else {
$ftp->mkdir($ftproot);
$ftp->mkdir("${ftproot}/${backtype}");
$ftp->cwd("${ftproot}/${backtype}");
}
$ftp->put( $targetfile, $remotefile );
$ftp->quit;
}
sub cpusystem {
if ( my $cpupid = fork() ) {
local $SIG{'TERM'} = $SIG{__DIE__} = sub {
if ( $$ == $original_child_pid ) {
unlink '/var/cpanel/backuprunning';
}
if ($cpupid) {
kill( 8, $cpupid );
sleep(2);
kill( 9, $cpupid );
}
exit(1);
};
waitpid( $cpupid, 0 );
}
else {
if ( -e "$CPCONF{'root'}/bin/cpuwatch" ) {
exec( "$CPCONF{'root'}/bin/cpuwatch", "$cpunum.0", @_ );
}
elsif ( -e "$CPCONF{'root'}/bin/logrunner" ) {
exec( "$CPCONF{'root'}/bin/logrunner", "$cpunum.0", @_ );
}
else {
exec(@_);
}
exit 1;
}
return;
}
sub setup_and_clean_logs {
if ( !-d $logdir ) {
unlink $logdir;
mkdir $logdir, 0700;
}
else {
chmod 0700, $logdir;
}
if ( opendir my $logdir_dh, $logdir ) {
while ( my $file = readdir($logdir_dh) ) {
if ( -f $logdir . '/' . $file && ( stat(_) )[9] < ( $now - ( 86400 * 10 ) ) ) {
unlink $logdir . '/' . $file;
}
}
closedir $logdir_dh;
}
else {
die "Unable to read directory $logdir: $!";
}
}
sub backup_disk_is_mounted {
my $ismounted = 0;
my @MOUNT = Cpanel::SafeRun::Errors::saferunnoerror($mount_bin);
my $mountline;
foreach (@MOUNT) {
chomp();
my $point;
( undef, undef, $point, undef ) = split( / /, $_ );
if ( $point eq $basemount ) {
$mountline = $_;
$ismounted = 1;
last;
}
}
return ( $ismounted, $mountline );
}
sub die_if_not_mounted_check {
if ( $CONF{'DIEIFNOTMOUNTED'} eq "yes" ) {
my ( $ismounted, $mountline ) = backup_disk_is_mounted();
if ( !$ismounted ) {
if ( !-t STDIN ) {
require Cpanel::iContact;
Cpanel::iContact::icontact(
'application' => 'cpbackup',
'level' => 1,
'subject' => qq{Backup not completed on $host},
'message' => qq{The backup was not able to be completed because $basemount could not be mounted.},
'msgtype' => ''
);
}
print STDERR "[cpbackup] Backup failed! $basemount is not mounted!\n";
exit 1;
}
else {
print "[cpbackup] Mount found ($mountline)\n";
}
}
}
sub unmount_backup_disk {
print "[cpbackup] Shutting down mount\n";
print "[cpbackup] running: " . join( ' ', $mount_bin, '-o', $mountkeyword . ',ro', $basemount ) . "\n";
system( $mount_bin, '-o', $mountkeyword . ',ro', $basemount );
print "[cpbackup] running: " . join( ' ', $umount_bin, $basemount ) . "\n";
system( $umount_bin, $basemount );
if ( $losetup_bin && $loopdev ) {
print "[cpbackup] running: " . join( ' ', $losetup_bin, '-d', $loopdev ) . "\n";
system( $losetup_bin, '-d', $loopdev );
undef $loopdev;
}
}
sub mount_backup_disk {
print "[cpbackup] Setting up mount\n";
print "[cpbackup] running: " . join( ' ', $mount_bin, $basemount ) . "\n";
system( $mount_bin, $basemount );
my ( $ismounted, $mountline ) = backup_disk_is_mounted();
if ($ismounted) {
if ( $mountline =~ /\([^)]*loop=([^,)\s]+)\)/ ) {
$loopdev = $1;
}
}
print "[cpbackup] running: " . join( ' ', $mount_bin, '-o', $mountkeyword . ',rw', $basemount ) . "\n";
system( $mount_bin, '-o', $mountkeyword . ',rw', $basemount );
}
sub exit_stderr_log_and_notify {
my ( $status, $subject ) = @_;
print STDERR "[cpbackup] $status\n";
require Cpanel::Notify;
Cpanel::Notify::notification(
'application' => 'cpbackup',
'status' => $status,
'priority' => 2,
'interval' => 60 * 60 * 24,
'subject' => $subject,
'message' => "The exact error was: $@",
'msgtype' => ''
);
Cpanel::Logger::logger(
{
'message' => $status,
'service' => 'cpbackup',
'level' => 'die',
'output' => 2,
'backtrace' => 0,
}
);
## logger should die out before it gets to the exit, but ensuring exit anyway.
exit(0);
}