# rm-rf.es

Script para borrar backups de cuentas de cPanel eliminadas

Quienes usen cPanel sabrán que cuando eliminas un dominio o cuenta en el servidor, el backup que hay en ese momento de dicho dominio sigue almacenado y no se elimina. Esto provoca que queden restos de backups antiguos que probablemente no necesitemos.

Bien, hay un script que os pongo más adelante que sirve para limpiar estos backups, por supuesto hay que ejecutarlo con cuidado. Primero, ejecutalo con la variable $delete_old=0, y si la salida del script es correcta entonces activalo cambiando la variable a $delete_old=1

Cambia también los permisos a 700 para que solamente root pueda ejecutarlo.

Fuente del scrip: www.cplicensing.net

#!/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() {
        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;
		}
	}
}
Salir de la versión móvil