FYI

>>> hannesd <[email protected]> 4/25/2010 12:52 AM >>>
I wrote just a small script, which creates a backup copy on the
server, when a file is created or changed.
Next I am writing a start/stop script for /etc/init.d
Also a cron job should delete all files older than 30 days from the
backup directory.

#!/bin/sh
WATCH=/var/simias/data/simias/SimiasFiles
BACKUP=/srv/backup/SimiasFiles

inotifywait -mr --exclude '\.simias\.wf\.' --format "%w%f" -e
close_write $WATCH |
while read source
do
    if [ -d "${source}" ]
    then
        continue
    fi

    # convert absolute path to relative
    buffer=`echo "${source}" | sed 's_'$WATCH'/__'`
    # echo "${buffer}"

    # destination
    destination="$BACKUP/${buffer}"

    # directory: everything before last '/'
    directory="${destination%/*}"
    # echo "${directory}"

    # create directory
    if [ ! -d "${directory}" ]
    then
        # echo "mkdir -p ${directory}"
        mkdir -p "${directory}"
    fi

    # if destination not exists, just copy the file
    if [ ! -e "${destination}" ]
    then
        echo "cp ${source} ${destination}"
        cp "${source}" "${destination}"
        continue
    fi
    echo "${destination} exists"

    # if destination is not equal to source
    cmp --silent "${source}" "${destination}"
    if [ $? -eq 0 ]
    then
        echo "${destination} is the same"
        continue
    fi
    echo "${destination} needs to be copied"

    # create new filename: basename + datetime + number: filename!
yyyymmdd-hhmmss!1
    destinationnew="${destination}!$(date +%Y%m%d-%H%M%S)"
    if [ -e "${destinationnew}" ]
    then
        $i=1
        while [ -e "${destinationnew}!$i" ]
        do
            (($i++))
        done
    fi

    # rename existing file to new name
    echo "mv ${destination} ${destinationnew}"
    mv "${destination}" "${destinationnew}"

    # copy the file
    echo "cp ${source} ${destination}"
    cp "${source}" "${destination}"
done


--
Subscription settings: 
http://groups.google.com/group/ifolder-ubuntu-debian-dev/subscribe?hl=en
------------------------------------------------------------------------------
_______________________________________________
ifolder-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ifolder-devel

Reply via email to