prekillacct – backup cpanel accounts on termination

by brian on February 2, 2010

Ever terminate a cPanel account by mistake? This script backs up a cPanel account as it is terminated.

That makes it really easy to restore the current state of the account if it is accidentally deleted via killacct or WHM/Terminate. Without this script, you have to rely on your last backup, which may or may not be recent and may or may not have even worked; at best, you may have lost hours of email or database changes.

The script works as killacct, which implements the account termination, runs the script /scripts/prekillacct before it does the actual kill. It does a pkgacct backup of the account, and copies a few useful statistics including DNS zones and maxemails into the home directory before the backup is made.

The account backup is kept in /backup/deleted and that directory is rsync’ed to /home/deleted at the end of the script. That makes it possible to lose /backup, or lose /home, without losing your backup of deleted accounts. To undelete an account after deletion, simply copy the backup to /home or /home2 and go into WHM/Restore acct and enter the username (you’ll see the backup file there).

To use this script, simply copy it to /scripts/prekillacct and make it mode 755 (ie: chmod 755 /scripts/prekillacct).

Download prekillacct.sh [1643 downloads] txt

Original release: August 2009

  • Pingback: Why is so easy to terminate accounts? - cPanel Forums()

  • Im sure that if you follow those instructions, it will not execute the script…

    I suggest you add in that you have to modify the killacct script to execute prekillacct or it wont work…

    nano killacct
    add in “prekillacct” after all the comments before it does anything

    • Actually, that would result in the prekillacct script being run twice. Killacct will run a file called /scripts/prekillacct before it removes the account, it’s part of built-in functionality. Additionally you are running the risk of cpanel destroying your modification when it next updates the system.

      As mentioned, we’ve been running this script for some years now without issues,so it does work!

  • Yeah, I found that it would run it twice shortly after posting that comment…

    What caused me to think that I had to do that? I looked at the “KillAcct” script, I noticed there was no call to “PreKillAcct”. So I added it in.

    When I went to test to see if it did backup my users data, I found that it does call the “PreKillAcct” script, it’s just the calling code is not inside of “KillAcct” script. The calling code is actually inside of the file that calls the “KillAcct” script which makes much more sense… Because then your not “daisy chaning” your PHP scripts. It makes the code easier to follow too.

    I saw 0 comments, so I thought maybe someone made the script, noone took it, and they stopped developing/testing/releasing it…

    Nice work, I can imagine this will save my a** in the future 🙂

    Thanks for pointing that out though, I almost forgot I modified the “KillAcct” script, and now I can go back and remove it 🙂

    • Actually, it’s saved our a**es a number of times!

      I actually wrote it just after I cleverly managed to remove ALL of a reseller’s accounts, thinking I was removing them from a secondary server … and even after it made me type a whole confirmation sentence. But it’s also useful to have a final backup of a user’s account that doesn’t get deleted as the normal backup cycles progress.

  • AJ

    I know this post is old but…

    I am new to this whole reseller webhosting thing. Where do I find the scripts/ folder? In my WHMCS install or on my reseller host account? I do not have root access.

    AJ

    • /scripts – requires root access. Unfortunately you can’t do this as a reseller.

      The post may be old, but all the scripts are in active use still!

      Cheers!

  • Ed

    Brian,
    What do you suggest we look at if the script isn’t working as described?
    The /home/deleted directory doesn’t exist.
    Might be related to cPanel backups not running on this server?
    Thank you.

    • If cPanel backups don’t work you may have bigger problems! Depending on the cause for that you may have problems running this script, as it effectively does a backup before the account is removed.

      I’d start by trying to run the script – could be permissions on the file?

  • Ed

    Sorry, should have said cPanel backups are not configured.
    Running manually does produce the /backup/deleted and /home/deleted directories.
    Never have had much faith in the cPanel backup program but was intrigued with your script as a fail safe option.
    Guess I’ll have to re-consider using the cPanel backup options.
    Thank you Brian.

    • Ed – sorry I missed this on the first reading some months ago:

      The prekillacct script is run as part of account termination. It doesn’t require cpanel backups to be configured as such, though it does manually call /scripts/pkgacct to do the backup of the deleted account during the deletion.

      I’d suspect permissions on /scripts/prekillacct – is it executable with mode 755 or 700? That’s the most likely breakage I can think of. This script does work terribly stably on all our servers.

  • It works perfectly.

    Only one question: As I already had a default “prekillacct” file under /scripts, is there a chance the script would be overwritten when cPanel updates?

    • We’ve never had it overwritten; and usually I don’t see a default prekillacct script if I remember correctly. Please let us know if you do get it overwritten.

  • Secmas

    Hi.
    This seems like a nice addon to cpanel, Does this works with today latest cpanel WHM 62.17?

    Regards,
    Sergio

Previous post:

Next post: