27
Backup Reseller & Resold accounts
Filed Under (Scripts) by WebScHoLaR on 27-11-2008
Tagged Under : backup, reseller
Backup option is available in both cPanel and WHM. Backup option in cPanel can not be set to automatic backups and the user has to login and manually generate the backup. Backup feature in WHM allows scheduled backups but Resellers are not provided with that option. As backup is a must thing so I wrote a small bash script that will backup the databases and the accounts data for all the accounts under the mentioned reseller and transfer the backup to a backup server. This script requires that the backup server should allow scp or rsync and supports SSH Authentication Keys.This script doesn’t need any update if the reseller adds new accounts or any user creates new databases.
Here is the script. You can either copy it or download it directly from here
# SSH keys need to be added so that the backup can be transferred to backup server.
# Written by Web-ScHoLaR-at-Hotmail.com
#!/bin/sh
username=RESELLER_USERNAME
error_log=/home/$username/backup.error_log
backup_results=/home/$username/backup.results
if [ ! -d "/home/$username" ];
then
exit 1;
fi
echo “==============================================================” > $error_log
echo “ERRORS ENCOUNTERED DURING BACKUP” >> $error_log
echo “==============================================================” >> $error_log
echo “BACKUP” > $backup_results
echo “==============================================================” >> $backup_results
echo “**** BACKUP STARTING: `date`…” >> $backup_results
echo “BACKUP ERRORS …” >> $error_log
grep “OWNER=RESELLER_USERNAME” /var/cpanel/users/* | cut -d: -f 1 | cut -d”/” -f5 > /tmp/users
for user in `cat /tmp/users`;
do
if [ ! -d "/home/$user/MySQLDumps" ];
then
mkdir /home/$user/MySQLDumps ; chown $user.$user /home/$user/MySQLDumps
fi
mysqlshow | grep $user”_” | cut -d”|” -f2 > /tmp/db.list ; for db in `cat /tmp/db.list` ; do mysqldump $db | gzip > /home/$user/MySQLDumps/$db.sql.gz ; chown $user.$user /home/$user/MySQLDumps/$db.sql.gz ; done
rsync -zaHlv –safe-links –delete -e /usr/bin/ssh /home/$user USERNAME@BACKUPSERVER:backups 2>>$error_log
done
echo “**** BACKUP COMPLETED: `date`…” >> $backup_results
cat $error_log >> $backup_results
echo `date +%m%d%Y` > /home/$username/backup_run
mail -s “$username finished backup successfully” user@domain.com < $backup_results
rm -rf /home/$user/MySQLDumps/* /tmp/users /tmp/db.list ;
NOTE: The above script should be setup as a cron from root login because only root user has access to all the data on the server. SSH Keys need to be generated at backup server and added to the server having account so that the data can be rsysnc’d without specifying password.
