Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Bash script to create & transfer backups of all cPanel users to a backup server  (Read 2858 times)

0 Members and 1 Guest are viewing this topic.

pranavkrishnam

  • Guest
This bash script will help you to take backups of all cPanel users from a server, transfer them to a backup server and remove the created backup to save the disk space. Please follow the steps provided below to run the script.

1. Login to the source server from which you wanted to take backups of the cPanel users as root.
2. Navigate to location /opt in the source server.
3. Create a new file transfer.sh and provide executable permission to the file.
4. Insert the code provided below to the script.

************************************************************************************************************
#!/bin/bash

#Reading remote server's IP, password and initializing the log file for this script.
read -p "Enter Remote Server's IP Address: " REMOTE_HOST
read -sp  "Enter Root Password: " PASSWORD
LOG_FILE=/opt/backup_and_transfer.log

#Adding timestamp into the log file.
date=`date`
echo -e "\n========================================================================================================\n
$date : Initializing backup creation and transfer
"\n========================================================================================================" > $LOG_FILE

#For loop to create backup, transfer to the destination via SCP and removing the backup in the source server to save disk space.
for USER in `\ls /var/cpanel/users/`;
 do
echo -e "Creating the backup of $USER\n" >> $LOG_FILE;
echo -e "\n========================================================================================================\n" >> $LOG_FILE;
/scripts/pkgacct $USER >> $LOG_FILE;
expect -c " 
   set timeout 1
   spawn scp /home/cpmove-$USER.tar.gz root@$REMOTE_HOST:/opt/
   expect yes/no { send yes\r ; exp_continue }
   expect password: { send $PASSWORD\r }
   expect 100%
   sleep 1
   exit
" >> $LOG_FILE;
echo -e "\n========================================================================================================\n" >> $LOG_FILE;
rm -vf /home/cpmove-$USER.tar.gz >> $LOG_FILE;
echo -e "\n========================================================================================================\n" >> $LOG_FILE;
done;
************************************************************************************************************
5. Run the file transfer.sh in the screen of the source server.
6. You can check the progress of this task in the log file /opt/backup_and_transfer.log.
« Last Edit: May 10, 2017, 01:28:12 pm by pranavkrishnam »