help-bash
[Top][All Lists]
Advanced

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

[Help-bash] Backup Script


From: Craig A. Adams
Subject: [Help-bash] Backup Script
Date: Thu, 24 May 2012 08:05:22 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1

Hi,

I am trying to build a backup script for the following scenario.

1) User plugs in external usb hard drive.
2) udev detects the hard drive and executes the backup script.
3) A staging script is used to fire off the actual backup script to avoid suspending udev operations. 4) The backup script needs to be locked in order to avoid parallel execution. 5) The backup script mounts the disk and double checks that it is actually mounted.
6) The rsync backup is executed.
7) A log file is created and emailed to predefined addresses.

What I have come up with so far is as follows...

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

/backup/scripts/start-disk-1.sh
#!/bin/bash
echo flock -xn /tmp/disk-1-lock /backup/scripts/backup-disk-1.sh | at now + 1 minute

flock is supposed to create the lock file if it does not exist and thus prevents any parallel scripts triggered by udev from running.

This is the most graceful way I could figure to execute the script without suspending udev operation.


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

# Backup to Disk 1

# Required Packages: sendemail

# Set Variables for Script

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

# 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."
        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!"
    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}/

# 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}

# End of Script

Unless I completely missed something this script should do the trick.

It was mentioned that my checking the mount point may introduce a race condition. If so, how can I avoid this?

I am completely lost on how to generate the log file. Can anyone give me any ideas? I would like the echo statements in the scripts along with the output from the rsync command.

Kindest Regards

Craig A. Adams



reply via email to

[Prev in Thread] Current Thread [Next in Thread]