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.

Find out which backups of terminated accounts to archive (on the new Cpanel backup)

Discussion in 'Workarounds and Optimization' started by eurorocco, Oct 24, 2014.

  1. eurorocco

    eurorocco Well-Known Member

    Joined:
    Jun 23, 2003
    Messages:
    99
    Likes Received:
    0
    Trophy Points:
    6
    The new Cpanel backup system loses backups of accounts that are terminated, which is horrible. Many times I've had customers come back to our hosting just because we hold that dear and valuable backup, so losing backups of terminated accounts for us is a no-no.

    Here is a script I wrote to find out which backups out there in /backup/YYYY-MM-DD and /backup/monthly/YYYY-MM-DD that are not active anymore, that were terminated and need to be archived. I don't do weeklys in this server, and are not taken into account.

    Script is read-only and its purpose is to display the cp commands that you can review and shell in. Script does not write files or copy files. It only reads and reports what should be done to archive.

    Place in a file, chmod u+x, run. Look at the output. Shell output if you like it. Mkdir /backup/arc beforehand.

    Disclaimer: Use at your own risk. Feel free to point me to a better way of doing this. Maybe Cpanel should have a check mark in Backup Config to say "Retain deleted accounts? YES. Then a choice: Keep newest or oldest backup? Newest. That.

    Code:
    #!/usr/local/bin/perl
    
    open(INF,"</etc/trueuserdomains") || die "cannot open /etc/trueuserdomains\n";
    @lines = <INF>;
    close(INF);
    chomp(@lines);
    
    foreach $line (@lines){
        @cells = split(/\:\ /,$line);
        if(@cells == 2){
    	$tgzname = $cells[-1].".tar.gz";
    	$tgzdomain = $cells[0];
    	$tgzisactiveh{$tgzname} = $cells[0];
        }
    }
    
    @dailybranches = `find /backup/* -maxdepth 0 -type d`;
    @dailybranches = sort { $a <=> $b } @dailybranches;
    @dailybranches = grep(/[0-9]{4}-[0-9]{2}-[0-9]{2}$/,@dailybranches);
    chomp(@dailybranches);
    
    @monthlybranches = `find /backup/monthly/* -maxdepth 0 -type d`;
    @monthlybranches = sort { $a <=> $b } @monthlybranches;
    @monthlybranches = grep(/[0-9]{4}-[0-9]{2}-[0-9]{2}$/,@monthlybranches);
    chomp(@monthlybranches);
    
    @branches = ();
    push(@branches,@dailybranches);
    push(@branches,@monthlybranches);
    
    foreach $branch (@branches){
        @cells = split(/\//,$branch);
        @cells = grep(/[0-9]{4}-[0-9]{2}-[0-9]{2}/,@cells);
        $branchdate = $cells[0];
    
        $branchdateh{$branchdate} = $branch;
        $branch = $branch . '/accounts';
    }
    
    @branchdates = keys(%branchdateh);
    @branchdates = sort { $a <=> $b } @branchdates;
    
    @files = ();
    foreach $branch (@branches){
        @morefiles = `find $branch/*.tar.gz`;
        chomp(@morefiles);
        push(@files,@morefiles);
    }
    
    foreach $file (@files){
        @cells = split(/\//,$file);
        $tgzname = $cells[-1];
    
        @cells = grep(/[0-9]{4}-[0-9]{2}-[0-9]{2}/,@cells);
        $cell = $cells[0];
    
        $tgzdate = $cell;
    
        if(!exists($tgzdateh{$tgzname})){
    	$tgzdateh{$tgzname} = $tgzdate;
        }
        else{
    	$tgzdateh{$tgzname} = $tgzdateh{$tgzname}."|".$tgzdate;
        }
    
        $tgznamedate = $tgzname."|".$tgzdate;
        $tgznamedatefileh{$tgznamedate} = $file;
    }
    
    @tgznames = keys %tgzdateh;
    @tgznames = sort(@tgznames);
    
    @tgztakes = ();
    
    foreach $tgzname (@tgznames){
        if(!exists($tgzisactiveh{$tgzname})){
    	$tgzwhat = $tgzdateh{$tgzname};
    	print "tgzname=$tgzname tgzwhat=$tgzwhat\n";
    
    	@tgzdates = split(/\|/,$tgzwhat);
    	@tgzdates = sort { $a <=> $b } @tgzdates;
    
    	$tgzdate = $tgzdates[0];
    	$tgznamedate = $tgzname."|".$tgzdate;
    
    	$tgzfile = $tgznamedatefileh{$tgznamedate};
    	print "    take $tgzfile\n";
    
    	push(@tgztakes,$tgzfile);
        }
    }
    
    foreach $tgztake (@tgztakes){
        print "cp -fp $tgztake /backup/arc\n";
    }
    
    print "TAKE THESE NEWEST BACKUPS FOR DELETED ACCOUNTS!\n";

    THANKS TO THE CPANEL FORUM AND COMMUNITY THAT HAS SAVED ME A THOUSAND TIMES. :)

    SUBJECT: BACKUP RETENTION OF TERMINATED ACCOUNTS
     
    #1 eurorocco, Oct 24, 2014
    Last edited by a moderator: Oct 24, 2014
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,723
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  3. eurorocco

    eurorocco Well-Known Member

    Joined:
    Jun 23, 2003
    Messages:
    99
    Likes Received:
    0
    Trophy Points:
    6
Loading...

Share This Page