Centralized Backup Script

Posted by: Admin  :  Category: Web Hosting

Linux Cpanel shared hosting: 600 GB disk space, 6 TB bandwidth, free domain, unlimited databases and ftp accounts, web hosting cheap and pro at Hostony

Hello There!

I thought I’d share a backup script that was written to consolidate backups onto one server instead of spreading the backup process across several servers. The advantages are somewhat obvious to consolidating the script onto one server, namely being that editing or making changes is much easier as you only have one script to edit.

The environment where this may be ideal would be for environments with 15-20 servers or less. I’d recommend a complete end-to-end backup solution for servers that exceed that number such as Bacula perhaps.

The bash shell script that I pasted below is very straightforward and takes two arguments. The first is the hostname or ip address of the destination server you are backing up. The next (and potentially unlimited) arguments will be single quote encased folders which you would want backed up.

This script is dependent on the server the script is running on having ssh key based authentication enabled and implemented for the root user. Security considerations can be made with IP based restrictions either in the ssh configuration, firewall configuration or other considerations.

#!/bin/sh
# Offsite Backup script
# Written by www.stardothosting.com
# Dynamic backup script

currentmonth=`date "+%Y-%m-%d %H:%M:%S"`
currentdate=`date "+%Y-%m-%d%H_%M_%S"`
backup_email="backups@youremail.com"
backupserver="origin-backup-server.hostname.com"

# Check User Input
if [ "$  #" -lt 2 ]
then
        echo -e "nnUsage Syntax :"
        echo -e "./backup.sh [hostname] [folder1] [folder2] [folder3]"
        echo -e "Example : ./backup.sh your-server.com '/etc' '/usr/local/www' '/var/lib/mysql'nn"
        exit 1
fi

# get the server's hostname
host_name=`ssh -l root $  1 "hostname"`
echo "Host name : $  host_name"
if [ "$  host_name" == "" ]
then
        host_name="unknown_$  currentdate"
fi

echo "$  host_name Offsite Backup Report: " $  currentmonth > /var/log/backup.log
echo -e "----------------------------------------------------------" >> /var/log/backup.log
echo -e "" >> /var/log/backup.log

# Ensure permissions are correct
chown -R backups:backups /home/fileserver/backups/
ls -d /home/fileserver/backups/* | grep -v ".ssh|.bash" | xargs -d "n" chmod -R 775

# iterate over user arguments & set error level to 0
errors=0
for arg in "$  {@:2}"
do
        # check if receiving directory exists
        if [ ! -d "/home/fileserver/backups/$  host_name" ]
        then
                mkdir /home/fileserver/backups/$  host_name
        fi
        sanitize=`echo $  arg | sed 's/[^/]/+$   //'`
        sanitize_dir=`echo $  arg | awk -F '/' '{printf "%s", $  2}'`
        /usr/bin/ssh -o ServerAliveInterval=1 -o TCPKeepAlive=yes -l root $  1 "/usr/bin/rsync -ravzp --progress --exclude 'clam_quarantinedir' $  sanitize/ backups@$  backupserver:/home/fileserver/backups/$  host_name/$  sanitize_dir; echo $  ? > /tmp/bu_rlevel.txt" >> /var/log/backup.log 2>&1
        echo "/usr/bin/ssh -o ServerAliveInterval=1 -o TCPKeepAlive=yes -l root $  1 "/usr/bin/rsync -ravzp --progress --exclude 'clam_quarantinedir' $  sanitize/ backups@$  backupserver:/home/fileserver/backups/$  host_name/$  sanitize_dir""

        runlevel=`ssh -l root $  1 "cat /tmp/bu_rlevel.txt"`
        echo "Runlevel : $  runlevel"

        if [ "$  runlevel" -ge 1 ]
        then
                errors=$  ((counter+1))
        else
                echo -e "Script Backup for $  arg Completed Successfully!" >> /var/log/backup.log 2>&1
        fi

done


# Check error level
if [ $  errors -ge 1 ]
then
        echo -e "There were some errors in the backup job for $  host_name, please investigate" >> /var/log/backup.log 2>&1
        cat /var/log/backup.log | mail -s "$  host_name Backup Job failed" $  backup_email
else
        cat /var/log/backup.log | mail -s "$  host_name Backup Job Completed" $  backup_email
fi

Linux Cpanel shared hosting: 600 GB disk space, 6 TB bandwidth, free domain, unlimited databases and ftp accounts, web hosting cheap and pro at Hostony

It should be explained further that this script actually connects to the destination server as the root user, using ssh key authentication. It then initiates a remote rsync command on the destination server back to the backup server as a user called “backupuser”. So that means that not only does the ssh key need to be installed for root on the destination servers, but a user called “backupuser” needs to be added on the backup server itself, with the ssh keys of all the destination servers installed for the remote rsync.

Hopefully I did not over complicate this, because it really is quite simple :

Backup Server –> root –> destination server to backup — > backupuser rsync –> Backup Server

Once you implement the script and do a few dry run tests then it might be ready to implement a scheduled task for each destination server. Here is an example of one cron entry for a server to be backed up :

01 1 * * * /bin/sh /usr/local/bin/backups.sh destination-server-hostname '/etc' '/usr/local/www' '/home/automysql-backups'

The post Centralized Backup Script appeared first on Managed WordPress Hosting | Managed VPS Hosting | Stack Star.

Managed WordPress Hosting | Managed VPS Hosting | Stack Star

Linux Cpanel shared hosting: 600 GB disk space, 6 TB bandwidth, free domain, unlimited databases and ftp accounts, web hosting cheap and pro at Hostony

Key Elements when Looking for a Cloud Backup Services

Posted by: Admin  :  Category: VPS / Dedicated Servers

Imagine what would happen if all your company’s data suddenly disappeared: your website, software, business documents, emails, contacts, subscriber lists, employee records, customer credit card details, everything. This does happen. It can be caused by fire, flood, theft, human error, hacking, machine failure and various other things. Without a backup, the loss of this data …

Web Hosting UK Blog

Website Backup Provider CodeGuard Releases cPanel, WHMCS Plugins

Posted by: Admin  :  Category: Web Hosting News

Cloud-based website backup provider CodeGuard has released cPanel and WHMCS plugins to enable hosting providers to offer its service. CodeGuard is initially integrating with enterprise-class hosting provider A Small Orange.

The post Website Backup Provider CodeGuard Releases cPanel, WHMCS Plugins appeared first on Web Hosting Talk.

Web Hosting Talk

Backup options offered by Cloud server

Posted by: Admin  :  Category: Web Hosting

Cloudbox99 specializes in creating efficient, effective Cloud Environments.Environments. Cloudbox99 has developed a patent pending technology which offers a speed of a super computer
Find Articles, Free Articles Directory | Web Hosting Articles

How to backup your website with cPanel?

Posted by: Admin  :  Category: Web Hosting

Backup is necessary for each and every thing, you host a website with huge files and databases that are highly potential, and what if the server where you website is hosted is crashed due to some reason? All, you potent website files and databases will go for toss.
Find Articles, Free Articles Directory | Web Hosting Articles

SiteAutoBackup.Com Dramatically Reduces cPanel/WHM Server Backup Prices!

Posted by: Admin  :  Category: Web Hosting News

April 11, 2014
Langley, BC, Canada.

Available immediately, reduced cPanel/WHM backup pricing available at https://www.siteautobackup.com/cart.php

With the new cost savings in place, web hosting providers and resellers can offer their own backup service, backing up websites and servers hosted by the world's biggest web hosting providers…
Web Hosting Talk News