pkgacct set destination folder with variable date format

ethical

Well-Known Member
Apr 7, 2009
96
8
58
Hi There,

I am adapting this code from a feature request
Change WHM cpanel Backup to allow Database only backups


Code:
    #!/usr/bin/perl
    BEGIN { unshift @INC, '/usr/local/cpanel'; }
    use Cpanel::Config::Users  ();
    my @cpusers = Cpanel::Config::Users::getcpusers();
use Time::Piece;
my $date = localtime->strftime('%Y-%m-%d');
my $backupdir = "/backup/" . $date;
print $backupdir;

foreach my $user (@cpusers) {
    my $cmd = "/scripts/pkgacct --skiphomedir $user $backupdir";
    open my $OUTPUT, '-|', $cmd or next;
    while (<$OUTPUT>) {
    print $_;
    }
    close $OUTPUT or next;
    }
I am trying to get it to create the backup in

/backup/2016-10-15 for example but it just does not work. If i set it to just use /backup/ it DOES word correctly

Code:
my $backupdir = "/backup/";
so I don't understand what is going wrong by me trying to add a subfolder here (this way i can run a cron and create multiple backup points instead of overwriting each time)


the output of the print statement shows me the path formatted correctly as well.


any help is greatly appreciated.

thanks
 
C

cPanelUser-Inactive

Guest
It looks like pkgacct expects the directory to exist before you start running the script, and defaults to /home if it doesn't. Adding a check to create the directory (assuming it will never exist prior to you creating it) fixed it for me. Note, I also added a trailing slash to the backupdir:

Code:
#!/usr/bin/perl
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use Cpanel::Config::Users ();
my @cpusers = Cpanel::Config::Users::getcpusers();
use Time::Piece;
my $date = localtime->strftime('%Y-%m-%d');
my $backupdir = "/backup/" . $date . "/";
print $backupdir;

unless(mkdir $backupdir) {
    die "Unable to create $backupdir\n";
}

foreach my $user (@cpusers) {
    my $cmd = "/scripts/pkgacct --skiphomedir $user $backupdir";
    open my $OUTPUT, '-|', $cmd or next;
    while (<$OUTPUT>) {
        print $_;
    }
    close $OUTPUT or next;
    }
print $cmd;
}
 

ethical

Well-Known Member
Apr 7, 2009
96
8
58
thanks a lot. I added in the code but i get the error that the directory could not be created? could it be because /backup already exists (its a mounted separate drive)

also you have an extra } at the end there.

actually i think i got it with this instead does this make sense?

Code:
    #!/usr/bin/perl
    BEGIN { unshift @INC, '/usr/local/cpanel'; }
    use Cpanel::Config::Users  ();
    my @cpusers = Cpanel::Config::Users::getcpusers();
use Time::Piece;
my $date = localtime->strftime('%Y-%m-%d');
my $backupdir = "/backup/" . $date . "/";
print $backupdir;

mkdir $backupdir;

foreach my $user (@cpusers) {
    my $cmd = "/scripts/pkgacct --skiphomedir $user $backupdir";
    open my $OUTPUT, '-|', $cmd or next;
    while (<$OUTPUT>) {
    print $_;
    }
    close $OUTPUT or next;
    }
thanks!
 
C

cPanelUser-Inactive

Guest
thanks a lot. I added in the code but i get the error that the directory could not be created? could it be because /backup already exists (its a mounted separate drive)

also you have an extra } at the end there.

actually i think i got it with this instead does this make sense?

thanks!
Yup! That definitely should work. My only caution is that you don't catch any errors with that. When you were running it before did you make sure that the directory (in this case I assume it's /backup/2016-10-17/) didn't already exist. It'll error if it already exists (because it technically can't create it), causing the failure you mentioned.