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.

Help needed with cpbackup - Any PERL coders out there?

Discussion in 'Data Protection' started by MGnet, Jan 13, 2004.

  1. MGnet

    MGnet Registered

    Joined:
    Dec 17, 2003
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Hi all!

    I'm using the bundled cPanel backup script to backup my sites to a different server via FTP. The script works great, except that I have one site which is used for backup of another server, so obviously I don't want to perform a backup of that backup (due to size and redundancy).

    I have edited a different backup script (written in PHP) which I use on my Ensim servers, adding a line as such:
    PHP:
    if ($domain == "ignore.this.domain") return;
    this single line does exactly what I want (i.e. skip a single site I don't want to backup).

    That's fine and dandy since the script I modified is written in PHP, but I honestly don't have a clue about Perl coding. SO, I'm hoping that anyone who knows Perl can help me with this. The snippet below is from the cPanel backup script and is the subroutine which I think all the backup action takes place.

    Can somebody please help me and provide a piece of code that does the same effect as the above PHP code? All I know is that cPanel "sites" are actually "users" in the /home dir, but I don't understand any of the following code....

    * the following is in PERL *
    PHP:
    sub backupaccts {
            
    my($target) = @_;
                     
            
    system("touch","${target}");
            
    #if another cpbackup starts just go bye bye
                            
            
    if (!($CONF{'BACKUPFILES'eq "no")) {
                    if (! -
    "$target/files") {
                            
    mkdir("$target/files",0700);   
                    }
                    
    my($file);
                    foreach 
    $file (@FILES) {
                            
    next if (! -e $file);
                            
    my $rawfile $file;
                            
    $rawfile =~ s/\//_/g;
                            
    if ($CONF{'BACKUPINC'eq "yes") {
                                    
    cpusystem("rsync","-a","--delete","$file","$target/files/$rawfile");
                            } else {
                                    
    cpusystem("cp","-f","$file","$target/files/$rawfile");
                                    
    chmod(0600,"$target/files/$rawfile");
                                    
    cpusystem("gzip","-f","$target/files/$rawfile");
                                    
    chmod(0600,"$target/files/$rawfile.gz");
                            } 
                    }
                    
    my($file);
                    if (! -
    "$target/dirs") {
                            
    mkdir("$target/dirs",0700);
                    }
                    foreach 
    $dir (@DIRS) {
                            
    next if (! -e $dir);
                            
    my $rawdir $dir;
                            
    $rawdir =~ s/\//_/g;
                            
    if ($CONF{'BACKUPINC'eq "yes") {
                                    
    cpusystem("rsync","-a","--delete","$dir/","$target/dirs/$rawdir");
                            } else {
                                    
    cpusystem("tar","cfzp","$target/dirs/$rawdir.tar.gz","$dir");
                                    
    chmod(0600,"$target/dirs/$rawdir.tar.gz");
                            }
                    }               
            }
    Please help!! :confused:

    p/s - maybe this is not even the correct bit of code to edit ...... I don't know, I told you I can't read Perl!! :p
     
  2. MGnet

    MGnet Registered

    Joined:
    Dec 17, 2003
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    problem solved

    OK...

    I decided to "study" the script myself and use GOOGLE to find out what was going on. Finally, found a way to make the script skip a single user. The small edit is towards the end of the file (the subroutine I mentioned in my previous post was not the one!!) :D hehe

    Here is the original code:
    PHP:
    if (!($CONF{'BACKUPACCTS'eq "no")) {
                    while(
    $user getpwent()) {
                            if (-
    "/var/cpanel/users/${user}") {
                                    if (!(
    $CONF{'BACKUPINC'eq "yes")) {  if (-"$target/${user}") { cpusystem("rm","-rf","$target/${$
                                    if (
    $CONF{'BACKUPINC'eq "yes") { $ENV{'INCBACKUP'} = 1; }
                                    
    cpusystem("/scripts/pkgacct2",$user,$target,"backup");
                                    if (
    $CONF{'BACKUPINC'eq "yes") { delete $ENV{'INCBACKUP'}; }
                                    if(
    $CONF{'BACKUPTYPE'eq "ftp") {
                                            
    ftpsend("$target","${target}/${user}.tar.gz","${user}.tar.gz");
                                            
    unlink("${target}/${user}.tar.gz");
                                    }   
                            }

                    }
            }
    And here it is after the small modification:
    PHP:
    if (!($CONF{'BACKUPACCTS'eq "no")) {
                    while(
    $user getpwent()) {
                            if (-
    "/var/cpanel/users/${user}") {
                                    if (!(${
    usereq "backup")) {
                                            if (!(
    $CONF{'BACKUPINC'eq "yes")) {  if (-"$target/${user}") { cpusystem("rm","-rf","$t$
                                            if (
    $CONF{'BACKUPINC'} eq "yes") { $ENV{'INCBACKUP'} = 1; }
                                            cpusystem("
    /scripts/pkgacct",$user,$target,"backup");
                                            if (
    $CONF{'BACKUPINC'} eq "yes") { delete $ENV{'INCBACKUP'}; }
                                            if(
    $CONF{'BACKUPTYPE'} eq "ftp") {
                                                    ftpsend("
    $target","${target}/${user}.tar.gz","${user}.tar.gz");
                                                    unlink("
    ${target}/${user}.tar.gz");
                                            }
                                    }
                            }
                    }
            }
    Basically, an extra if statement to check whether the current user is = backup. Actually, it checks if the user is NOT backup and then proceeds with the backup. Of course, you'll have to replace the user name you want to skip!

    Hope this is useful to somebody ;)
     
  3. Til

    Til Registered

    Joined:
    Nov 3, 2003
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    You are my hero! :D:

    Thank You, this was just what i needed to backup all my sites except one large site. I just made the modification and it works beautifully.
     
  4. C3W

    C3W Member

    Joined:
    Jun 10, 2003
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    does anyone know which particular file is to be edited?
     
  5. Til

    Til Registered

    Joined:
    Nov 3, 2003
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Yes, /scripts/cpbackup
     
  6. anand

    anand Well-Known Member

    Joined:
    Nov 11, 2002
    Messages:
    1,435
    Likes Received:
    1
    Trophy Points:
    38
    Location:
    India
    cPanel Access Level:
    DataCenter Provider
    Start with reading the cpbackup script, yo will then understand how cpanel takes backup of any account / data for a user. This way you can do the same for any user. ;)
     
  7. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    My change is similar to MGnet however I made a small change.

    Code:
    if (!($CONF{'BACKUPACCTS'} eq "no")) { 
                    while($user = getpwent()) { 
                            [COLOR=red]if (-f "/var/cpanel/users/${user}") {[/COLOR]  
                                    if (!($CONF{'BACKUPINC'} eq "yes")) {  if (-d "$target/${user}") { cpusystem("rm","-rf","$target/${$ 
                                    if ($CONF{'BACKUPINC'} eq "yes") { $ENV{'INCBACKUP'} = 1; } 
                                    cpusystem("/scripts/pkgacct2",$user,$target,"backup"); 
                                    if ($CONF{'BACKUPINC'} eq "yes") { delete $ENV{'INCBACKUP'}; } 
                                    if($CONF{'BACKUPTYPE'} eq "ftp") { 
                                            ftpsend("$target","${target}/${user}.tar.gz","${user}.tar.gz"); 
                                            unlink("${target}/${user}.tar.gz"); 
                                    }    
                            } 
                    } 
            }
    
    now looks like
    Code:
    if (!($CONF{'BACKUPACCTS'} eq "no")) { 
                    while($user = getpwent()) { 
                           [color=red] if (-f "/home/users/${user}") { [/color]
                                    if (!($CONF{'BACKUPINC'} eq "yes")) {  if (-d "$target/${user}") { cpusystem("rm","-rf","$target/${$ 
                                    if ($CONF{'BACKUPINC'} eq "yes") { $ENV{'INCBACKUP'} = 1; } 
                                    cpusystem("/scripts/pkgacct2",$user,$target,"backup"); 
                                    if ($CONF{'BACKUPINC'} eq "yes") { delete $ENV{'INCBACKUP'}; } 
                                    if($CONF{'BACKUPTYPE'} eq "ftp") { 
                                            ftpsend("$target","${target}/${user}.tar.gz","${user}.tar.gz"); 
                                            unlink("${target}/${user}.tar.gz"); 
                                    }    
                            } 
                    } 
            }
    
    Notice I am using
    instead of
    I simply created a file for each user I wanted to backup using "> username". The script just checks for the existance of a file.

    This way I only backup accounts that have a file in the "/home/users" directory
     
  8. anand

    anand Well-Known Member

    Joined:
    Nov 11, 2002
    Messages:
    1,435
    Likes Received:
    1
    Trophy Points:
    38
    Location:
    India
    cPanel Access Level:
    DataCenter Provider
    smart mod asmithjr :)
     
  9. stillwaiting

    stillwaiting Member

    Joined:
    Jan 14, 2004
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    will these changes to the script get overwritten when cpanel updates?

    i saw this in the code

    if (isolderthen(.7,"${basedir}/daily")) {
    backupaccts("${basedir}/daily");
    } else {
    print "Daily Backup is up to date\n";
    }

    which skips a backup if the existing tar file is less than 0.7 days old, so why not make a dummy file with the name the backup would have, and then use touch to set the date to be 25 years in the future?

    i think this would work too and would continue working if cpbackup is updated [obviously, as long as cpbackup isn't completely re-written not to use the isolderthen function].
     
  10. anand

    anand Well-Known Member

    Joined:
    Nov 11, 2002
    Messages:
    1,435
    Likes Received:
    1
    Trophy Points:
    38
    Location:
    India
    cPanel Access Level:
    DataCenter Provider
    yes they would get overwritten. you can create a copy of it and run that instead for taking backups.
     
  11. stillwaiting

    stillwaiting Member

    Joined:
    Jan 14, 2004
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    yes, my point is, that all your ideas need the server owner to monitor when the backup script is overwritten but my method doesn't, i think?

    my method would be set-and-forget instead of 'edit-and-monitor for changes'
     
  12. anand

    anand Well-Known Member

    Joined:
    Nov 11, 2002
    Messages:
    1,435
    Likes Received:
    1
    Trophy Points:
    38
    Location:
    India
    cPanel Access Level:
    DataCenter Provider
    Incase of your mod, if we have many accounts on the server (which usually everyone has), we would need to touch files for all users. Also this was only one scenerio, there are people who want different mods in their backup. So for everyone it keeps on changing.
     
  13. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
  14. taco

    taco Member

    Joined:
    Mar 19, 2004
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    The real easy solution by cPanel guys

    When you take a look at the /scrpts/cpbackup , you find a line with:
    open(CPBACK,"/etc/cpbackup-userskip.conf");
    This made me think........
    This file doesn't exist usually, but we can work on that!
    Just mention one username on each line.

    It's real simple and very safe for future updates!
    I don't like to mess around in scripts, because you don't know what will be overwritten with newer cPanel versions.

    You all guys will enjoy this option very much! Thank you cpanel-programmers!
     
Loading...

Share This Page