Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Backups

Rsync Time Backup

Submitted by on December 22, 2016 – 3:46 pm

This is a simple wrapper for the awesome rsync-time-backup script by Laurent Cozic. Through clever use of hard links rsync-time-backup greatly speeds up incremental backups and saves disk space.

The wrapper script is for my internal NAS environment. The script selects either production or Dev/QA NAS based on the server name. The script will then mount the correct NFS share, run various checks, prep the target folder and launch the backup.

#!/bin/bash
#		              |
#                 ___/"\___
#		  __________/ o \__________
#		    (I) (G) \___/ (O) (R)
#		         2016-12-22
# ----------------------------------------------------------------------------
# Salt wrapper for rsync-time-backup
# https://github.com/laurent22/rsync-time-backup
# ----------------------------------------------------------------------------
#
configure() {
    url="https://raw.githubusercontent.com/laurent22/rsync-time-backup/master/rsync_tmbackup.sh"
	this_host=`/bin/hostname | awk -F'.' '{print $1}'`
	if [ $(grep -c "^prod" <<<${this_host}) -eq 1 ] || [ "${this_host}" == "amidala" ]
	then
		nashost="prodnas01.krazyworks.com.local"
		nasshare="/nfspool_prod"
	else
		nashost="devqanas01.krazyworks.com.local"
		nasshare="/nfspool_devqa"
	fi
	mountdir="/nfspool"
	backupdir="rsync_time_backup"
    basedir="/var/adm/bin"
    if [ ! -d "${basedir}" ] ; then mkdir -p "${basedir}" ; fi
	this_time=$(date +'%Y-%m-%d %H:%M:%S')
	this_time_epoch=$(date -d "`echo ${this_time}`" "+%s")
    rsyncbackup="${basedir}/rsync_tmbackup.sh"
    if [ ! -x "${rsyncbackup}" ] || [ ! -s "${rsyncbackup}" ]
    then
        wget --no-check-certificate -O "${rsyncbackup}" "${url}" 2>/dev/null
        chmod 755 "${rsyncbackup}" 2>/dev/null
    fi
}

verify() {

	if [ ! -x "${rsyncbackup}" ]
	then
		echo "Rsync backup script ${rsyncbackup} not found. Exiting..."
		exit 1
	fi
    
	if [ ! -d "${mountdir}" ]
    then
        mkdir -p "${mountdir}"
    fi
	
    if [ `which mountpoint >/dev/null 2>&1 ; echo $?` -eq 0 ]
	then
		mountstatus=$(/bin/mountpoint "${mountdir}" >/dev/null 2>&1 ; echo $?)
		if [ ${mountstatus} -ne 0 ]
		then
			mount "${nashost}:${nasshare}" "${mountdir}"
			mountstatus=$(/bin/mountpoint "${mountdir}" >/dev/null 2>&1 ; echo $?)
			if [ ${mountstatus} -ne 0 ]
			then
				echo "Destination ${mountdir} is not mounted. Exiting..."
				exit 1
			fi
		fi
		else
		if [ `df "${mountdir}" | grep -c ${nashost}` -eq 0 ]
		then
			mount "${nashost}:${nasshare}" "${mountdir}"
			if [ `df "${mountdir}" | grep -c ${nashost}` -eq 0 ]
			then
				echo "Destination ${mountdir} is not mounted. Exiting..."
				exit 1
			fi
		fi
	fi
	
	if [ ! -d "${mountdir}/${backupdir}" ]
	then
		echo "Destination folder ${mountdir}/${backupdir} not found on ${nashost}. Exiting..."
		exit 1
	fi
    
	if [ ! -d "${mountdir}/${backupdir}/${this_host}" ]
    then
        mkdir -p "${mountdir}/${backupdir}/${this_host}"
    fi
	
	if [ ! -f "${mountdir}/${backupdir}/${this_host}/backup.marker" ]
    then
        touch "${mountdir}/${backupdir}/${this_host}/backup.marker"
    fi
}

do_backup() {
	nohup ${rsyncbackup} // "${mountdir}/${backupdir}/${this_host}" </dev/null >/dev/null 2>&1 &
}

do_check() {
	if [ -f "${mountdir}/${backupdir}/${this_host}/backup.inprogress" ]
    then
        echo "Another backup is already in progress. Exiting..."
		exit 1
    fi
}

# RUNTIME
configure
verify
do_check
do_backup

 

Print Friendly, PDF & Email

Leave a Reply