[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Automatic Backup Script Help



Hi,

I am trying to setup a basic backup script for a Debian 6.0.5 server and am wanting to validate the scripts I have so far.

Here is the scenario...

The server has 2 x 1Tb hard drives in RAID 1 config. A third 1Tb hard drive is used for scheduled rsync snapshots.

I also have 5 x USB 3.0 1Tb hard drives for taking backups off site.

The aim is to plug one of the external hard drives and rsync the snapshot backup to it automatically.

Using udev rules I now have the hard drives being recognised on plug in and udev firing off a test script.

As it is not a good idea to block up udev by running the backup script from the udev rule, I need a staging script to start the backup, but not immediately.

The backup script is intended to be run 1 minute after the external hard drive is plugged in, and this is what I have come up with:

Staging Script
==============

/data/backups/scripts/start-disk-1.sh
#!/bin/bash
echo /data/backups/scripts/backup-disk-1.sh | at now + 1 minute

Is this the correct way to call the backup script, or is there a better way?

Backup Script
=============

Now the backup script needs to do certain things.

1) Mount the hard drive and fail if necessary.
2) Carry out the rsync to the external hard drive.
3) Unmount the external hard drive.
4) Generate a log of the backup process.
4) Send an email with the log as the message body.

I have not figured out how to generate the log.
I am not sure of my script syntax including " vs ' vs ` delimiters.

The actual backup script I have come up with is as follows... (I hope email line wrapping doesn't ruin the script layout.)

/data/backups/scripts/backup-disk-1.sh
#!/bin/bash
# Backup to Disk 1

# Set Variables for Script

backup_description=Disk-1
backup_source='/data/snapshots/'
backup_target='/data/backups/disk-1'
start_date_time='date +%Y-%m-%d-%H:%M:%S'
from_addr='xyz@abc.com'
to_addr='abc@xyz.com'
cc_addr='def@uvw.com"
subject='${backup_description} backup at ${current_date_time}'
smtp_server='smtp.somewhere.com'
user_name='xyz@abc.com'
mail_pwd='password'
backup_log_file='/data/backup/logs/${backup_description}-${start_date_time}.log'

# Check if another backup is already in progress. If not, create a progress file, else fail.

if -a /data/backups/${backup_description}-in-progress; then
	echo "FAIL ${backup_description} backup already in progress."
	exit 1
else touch /data/backups/${backup_description}-in-progress
fi

# Start disk mount process

echo "${backup_description} Automatic Backup Starting at ${start_date_time}"

# Check and mount hard drive if not already mounted.

if ! mountpoint -q ${backup_target}/; then
	echo "Mounting the external hard drive."
	echo "External hard drive mounted at ${backup_target}"
	if ! mount ${backup_target}; then
		echo "FAILURE! An error was returned during mounting of hard drive."
		rm /data/backups/${backup_description}-in-progress
		exit 1
	else echo "External hard drive mounted successfully.";
	fi
else echo "${backup_target} is already mounted, but should not be. Check logs!";
fi

# If hard drive is still not mounted, exit ungracefully

if ! mountpoint -q ${backup_target}/; then
	echo "FAILURE! Mounting of external hard drive failed!"
	rm /data/backups/${backup_description}-in-progress
	exit 1
fi

# Start actual rsync backup

echo "Start of rsync backup."
sudo rsync --archive --verbose --human-readable --itemize-changes --progress --delete ${backup_source} ${backup_target}/

# Remove backup in progress file

rm /data/backups/${backup_description}-in-progress

# Unmount external hard drive

umount ${backup_target}

# Set backup finish time

finish_date_time='date +%Y-%m-%d-%H:%M:%S'

# End of backup message

echo "${backup_description} Automatic Backup finished at ${finish_date_time}."

# Send eMail Log of backup

sendemail -f ${from_addr} -t ${to_addr} -u ${subject} -m ${backup_log_file} -s ${smtp_server} -cc ${cc_addr} -xu ${user_name} -xp ${password}

Any help with this would be gratefully received.

Kindest Regards

Craig A. Adams


Reply to: