The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

pkgacct set destination folder with variable date format

Discussion in 'cPanel Developers' started by ethical, Oct 15, 2016.

Tags:
  1. ethical

    ethical Well-Known Member

    Joined:
    Apr 7, 2009
    Messages:
    79
    Likes Received:
    2
    Trophy Points:
    8
    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
     
  2. cPanelBenny

    cPanelBenny Community Manager, Development, dog scratcher
    Staff Member

    Joined:
    Apr 24, 2014
    Messages:
    43
    Likes Received:
    20
    Trophy Points:
    8
    Location:
    Michigan
    cPanel Access Level:
    Root Administrator
    Twitter:
    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;
    }
     
  3. ethical

    ethical Well-Known Member

    Joined:
    Apr 7, 2009
    Messages:
    79
    Likes Received:
    2
    Trophy Points:
    8
    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!
     
  4. cPanelBenny

    cPanelBenny Community Manager, Development, dog scratcher
    Staff Member

    Joined:
    Apr 24, 2014
    Messages:
    43
    Likes Received:
    20
    Trophy Points:
    8
    Location:
    Michigan
    cPanel Access Level:
    Root Administrator
    Twitter:
    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.
     
Loading...

Share This Page