Cleanup old backups cPanel/WHM plugin

Here you can find tutorials and notes for server-side maintenance/configuration

Remove old account backups on a cPanel server

Postby lik » Tue Apr 21, 2009 12:23 am

Code: Select all
#!/usr/bin/perl
#################################################
# version 1.1
#
# Clean old cpanel backups
#
# Author: Igor Russian
##################################################


use Date::Parse;

my @all_dirs = ('/backup/cpbackup/daily', '/backup/cpbackup/weekly');

my $query = "ls -l |";
my $expire_day_period = 30; # Number of days
my $expire_period = $expire_day_period * 24 * 60 * 60 ; # expire_day_period in seconds
my $i=1;
my @rm_dirs = ();
my @dir_en = ();
my $type;
my $name;
my $group;
my $mtime;
my $diff_time;

#If backup is enabled
my $backup_conf = "/etc/cpbackup.public.conf";
my $backup_enable = `/bin/cat $backup_conf | grep BACKUPENABLE`;

chomp $backup_enable;

(my $tmp1, my $tmp2) = split(/ /,$backup_enable);
$backup_enable = $tmp2;

if ($backup_enable eq 'yes')
{
   foreach my $b_dirs (@all_dirs)
   {
     if (-d $b_dirs)
     {
       chdir($b_dirs);
       open(DIR, $query);

       while(<DIR>)
       {
         $i++;
         if ($i>3)
         {
            @dir_en = split;
            $type = substr $dir_en[0],0,1;
            $group = $dir_en[3];
            $mtime = $dir_en[6]." ".$dir_en[5]." ".$dir_en[7];
            $name = $dir_en[8];

            if ($type eq 'd')
            {
               if (($name ne $group) and ($name ne 'files') and ($name ne 'dirs'))
               {
                  $diff_time = time() - str2time($mtime);
                  if ($diff_time >= $expire_period)
                  {
                    push(@rm_dirs, $b_dirs."/".$dir_en[8]);
                  }
               }
             }
           }
        }
       close(DIR);
    }
    next;
  }

#  print "Remove old backups...\n";

  foreach my $ft (@rm_dirs)
  {
    my $tmp_str = "/usr/local/cpanel/bin/cpuwatch 10.0 /bin/rm -rf $ft";
    print $tmp_str."\n";
    system($tmp_str);
    next;
  }
}
else
{
  print "Backup status: ".$backup_enable."\n";
}
lik
Founder
Founder
 
Posts: 497
Joined: Wed Dec 15, 2010 3:21 am

Cleanup backup files of deleted accounts (cpanel) deprecated

Postby lik » Sat Aug 08, 2009 1:55 am

Information below is deprecated. The author of the script decides to continue the development of the script and extend functionality.
Please follow post Cleanup old backups cPanel/WHM plugin

Code: Select all
#!/usr/bin/perl
# cPlicensing.net - scripts             Copyright(c) 2003 cPlicensing.net.
#                                       All rights Reserved.
# support@cPlicensing.net               http://cPlicensing.net
# Unauthorized copying is prohibited
#Version: 0.01

use POSIX;

$delete_old    = '0'; #0 or 1, set to 1 to remove old account backups
if($ARGV[0] eq "-d") { $delete_old = '1'; }


$|++;
POSIX::nice(19);

print "Reading Backup Config...";
open(CPBACK,"/etc/cpbackup.conf") or die("Failed, Does it exist and do you have access?\n");
while(<CPBACK>) {
        s/\n//g;
        my($name,$value) = split(/ /, $_);
        $CONF{$name} = $value;
}
close(CPBACK);
print "Complete\n";

if ($CONF{'BACKUPENABLE'} ne "yes") {
        die "Backup Not Enabled\n";
}
if (! -e $CONF{'BACKUPDIR'}){
   die "Backup Dir Doesnt Exist\n";
}

until (`ps -ax` !~ m/cpbackup/) {
   print "Detected cpbackup process...Sleeping for 60 Seconds\n";
   sleep(60);
};

if ($CONF{'BACKUPMOUNT'} eq "yes") {
   if(`mount` !~ m/$CONF{'BACKUPDIR'}/){
      system("mount","$CONF{'BACKUPDIR'}");
   }
   system("mount","-o","remount,rw","$CONF{'BACKUPDIR'}");
}

cleandir("$CONF{'BACKUPDIR'}/cpbackup/daily");
cleandir("$CONF{'BACKUPDIR'}/cpbackup/weekly");
cleandir("$CONF{'BACKUPDIR'}/cpbackup/monthly");

if ($CONF{'BACKUPMOUNT'} eq "yes") { system("umount","$CONF{'BACKUPDIR'}"); }

sub cleandir {
   my($target) = @_;
   
   opendir(DIRTYDIR,"$target");
   @DIRTYDIR = readdir(DIRTYDIR);
   closedir(DIRTYDIR);

   foreach(@DIRTYDIR) {
      next if /^\.\.?$|^files$|^dirs$/;
      if(-f "$target/$_"){
         $user = $_;
         $user =~ s/.tar.gz//;
         if(! -f "/var/cpanel/users/$user"){
            if($delete_old == 1){
               unlink("$target/$_");
               print "Deleted Old Backup... $target/$_\n";
            } else {
               print "Detected Old Backup... $target/$_\n";
            }
         }
         next;
      }
      if(-d "$target/$_"){
         if(! -f "/var/cpanel/users/$_"){
            if($delete_old == 1){
               #system("rm","-r","$target/$_");
               print "Directory Delete Not Supported, If you really want to remove dirs (incremental backups) you can edit this script and uncomment the line that looks like #system(\"rm\",\"-r\",\"$target/$_\")\n";
            } else {
               print "Detected Old Backup... $target/$_\n";
            }
         }
         next;
      }
   }
}

Chmod the file 700 (root only run).

Ensure on the first run that $delete_old=0. Run the script
Code: Select all
./cleanbackups

verify the output is correct, if it is correct change the $delete_old=1. Re-run script.

http://www.cplicensing.net/files/scripts/cleanbackups
lik
Founder
Founder
 
Posts: 497
Joined: Wed Dec 15, 2010 3:21 am

Cleanup old backups cPanel/WHM plugin

Postby lik » Sat Nov 06, 2010 3:43 am

Recently very useful WHM plugin has been written by the NDChost and is freely available at http://www.ndchost.com/cpanel-whm/plugins/cleanbackups/
cPanels backup script will not remove backups for accounts that no longer exist and these backups will remain on the backup drive until removed. This plug-in gives administrators the ability to clean up their backup directory by setting how long they want to keep backups for accounts that no longer exist and removing those backups when they expire. The script can be run by hand or after automatically after the cPanel backup script finishes.


Installation Instructions
Code: Select all
cd /home
rm -f latest-cleanbackups
wget http://www.ndchost.com/cpanel-whm/plugins/cleanbackups/download.php
sh latest-cleanbackups

Log into the WHM, click on plugins, then Clean Backups. From there you can enable and disable the auto cleaning process, configure how long backups for non-existent accounts will be kept, see recent activity and even run the cleaning script by manually.
lik
Founder
Founder
 
Posts: 497
Joined: Wed Dec 15, 2010 3:21 am


Return to Server Side Actions

 


  • Related topics
    Replies
    Views
    Last post
cron