WordPress Developer & Linux Administrator

cPanel shell script to pkgacct each user on a server

This script creates an extra backup of users on server with cPanel’s built-in packaging system. These are restorable backups which are basically a snapshot of any given cPanel account. When you restore, they will be perfect copies of the account as it was at the time of backup.

Due to limited space issues we all face at one time or another, this script will rsync each cPanel account backup to a remote server after creation, then removed.

#!/bin/bash
#
#  Date: Aug 7th 2014
#  Author: Will Ashworth (williamashworth.com || linuxscripts.org)
#
#  cPanel bash script to 'pkgacct' each user on a server
#
#  Copyright (C) 2015 Will Ashworth
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details. http://www.gnu.org/licenses/

IFS="$"
EMAILADDRESS="someone@domain.com"
EMAILMESSAGE="email-message.txt"

/bin/mail -s "Server backup started" "$EMAILADDRESS" < $EMAILMESSAGE

cd /var/cpanel/users

find * | while read CPUSER; do
    echo "Now processing ${CPUSER} ..."
    /scripts/pkgacct ${CPUSER}
    rsync --remove-sent-files /home/*.tar.gz username@remoteserver.com:/home/backups/      
done

/bin/mail -s "Server backup finished" "$EMAILADDRESS" < $EMAILMESSAGE

Categories: Bash, cPanelTags: , , ,

2 Responses

  1. hanifeoglu says:

    Hi,
    First i want thank you very nice script i tru very good workin just i want learn
    2 step have
    first step i want single account backup how to get single account cd /var/cpanel/users/xxx
    second step
    how to add root password script in for example this is posible

    IFS=”$”
    EMAILADDRESS=”someone@emailaddress”
    EMAILMESSAGE=”emailmessage.txt”

    /bin/mail -s “Server backup started” “$EMAILADDRESS” < $EMAILMESSAGE

    cd /var/cpanel/users

    find * | while read CPUSER; do
    echo “Now processing ${CPUSER} …”
    /scripts/pkgacct ${CPUSER}
    rsync –remove-sent-files /home/*.tar.gz mypassword root@remoteserver.com:/home/backups/
    done

    /bin/mail -s “Server backup finished” “$EMAILADDRESS” < $EMAILMESSAGE

    • Charly Biscay says:

      Hello! Better late than never. Sorry I didn’t see this message for so long.

      In short, you’ll need to come up with a password-less solution to authenticate with the remote server that you’re placing the files on. You may or may not have root on the remote, but either way you’re going to need something to keep track of the password. I mean, you can type the password each time it runs the rsync command if you want, but that’s a lot of work.

      For this, I’d suggest setting up SSH keys so you can run the command without needing to type a password at each run. Also, it’s probably a good idea to not use root for this anyways (I’ll update the script so it doesn’t suggest it). I hope this helps!

Comments

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.