Auto backup script for cPanel Website

Auto backup script for cPanel websites on Weekly bases and delete older backup

Backing up all your Website Data with MySQL databases one by one is a pain. Here is a small bash script made to dump and compress all databases to a directory and compress website data to another directory for better management. So Auto backup script for cPanel Website on Weekly bases are shown below which can be update on daily or monthly and script automatic delete older backup when disk space is our concern

Here you can find out some user defined variables which is used in bash script.

1. Apache Website Configuration file path

2. Backup Directory in local server to store backup of cpanel data

3. Date format while creating backup

4. Remote Server Ip where backup will move using rsync

Note: Local server should have access to remote server using key based authentication without password

5. Remote server port to connect

6. LocalStorage defines the no. of days that backup will store in local hard diskĀ  rest will delete automatically

 

First create a new file using any editor and write below code

vi backup.sh

 

 ######################################## Developed by Sahil Kataria   ###############################
        #######################  Backup All Running sites on Server with Mysql ##############################

                        ###########  User Define Variables  ###########
apache_conf="/etc/httpd/conf/httpd.conf"
backup_dir=/backup                         ## Define backup directory where backup will store
date=`date +%F`
remoteip=x.x.x.x        ## remote backup server ip
port=22                 ## remote server Port to connect throug ssh
localstorage=2          ## no. of days to store backup on local server rest will move to backup remote server

mkdir -p $backup_dir/web_backup/$date $backup_dir/mysql_backup/$date
cat /dev/null > ./completed

########## Get all configued sites ########
grep ServerName $apache_conf|sed 's/ServerName//g;s/ //g'|grep -v [0-9].[0-9]|grep -v `hostname`|sort|uniq > sites

while read line
do
        flag=0                          # Initialize variable for checking site on our server or not
        ip=`host -TtA $line|grep "has address"|awk '{print $4}'`        # Get ip address using host command
        if [ -n "$ip" ]
        then
                         docroot=`cat $apache_conf|grep -w -A 5 "ServerName $line"|grep DocumentRoot|uniq|sed 's/DocumentRoot//g;s/ //g'`
                         cpanel=`echo $docroot|cut -d'/' -f1-3`
                                tar czf $backup_dir/web_backup/$date/$line-$date.tar.gz $cpanel
                                echo $line $cpanel >> completed
        fi
done < ./sites

mysql -e 'show databases'|grep -v information_schema |grep -v leechprotect|grep -v performance_schema|grep -v mysql|grep -v Database > all_db
while read db
do
        mysqldump $db > $backup_dir/mysql_backup/$date/$db-$date.sql
done < ./all_db
rsync -e "ssh -p $port" -avz $backup_dir/web_backup/$date root@$remoteip:/web_backup/
rsync -e "ssh -p $port" -avz $backup_dir/mysql_backup/$date root@$remoteip:/sql_backup/
cd $backup_dir/web_backup/;find ./ -maxdepth 1 -mtime +$localstorage -exec rm -rf {} \;
cd $backup_dir/mysql_backup/;find ./ -maxdepth 1 -mtime +$localstorage -exec rm -rf {} \;


#############Conf Backup 15 days ############
day=`date +%d`
if [ $(( $day % 14 )) -eq 0 ]
then
        /bin/crontab -l > crontab
        tar czf $backup_dir/conf_backup/conf-$date.tar.gz /usr/local/apache /usr/local/lib /etc/my.cnf /etc/varnish/ /etc/redis/ /etc/hosts.allow /etc/exim* /etc/ssl /etc/pki/ /var/cpanel/ssl/ crontab
        rsync -e "ssh -p $port" -avz $backup_dir/conf_backup/conf-$date.tar.gz root@$remoteip:/conf_bkp/
fi

 

After that save and exit from this file and give appropriate permission to this file.

chmod +x backup.sh

Next we can configure it as cron entry on daily or weekly bases.

Here we are writing cron entry for executing this script on weekly bases.

crontab -e
0 1 * 1 * sh /root/backup.sh

This above configuration will execute backup script on every monday at 1 AM.

 

 

LinuxTweaks

Linuxtweaks Blog helping Server Admin to Manage their servers, Desktop users for making more friendly with linux. Tutorials , guides and tips for linux server maintenance. Here you can learn how to tweak linux servers with code and how to manage it properly.

You may also like...

Leave a Reply

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