snapshots without NFS

Mac User FR macuserfr at free.fr
Wed Nov 26 13:27:49 GMT 2008


Mag Gam a écrit :
> Thanks for the fast response Vitorio.
>
> Do you happen to have a simple example? I been trying to look thru
> google but unsuccessful.
>
>
> On Wed, Nov 26, 2008 at 8:11 AM, Mac User FR <macuserfr at free.fr> wrote:
>   
>> Mag Gam a écrit :
>>     
>>> Is it possible to implement snapshots schema without NFS or filesystem
>>> mount? I would like to implement it with ssh only.
>>>
>>> Has anyone done this before?
>>>
>>> tia
>>>
>>>       
>> Yes, it's possible using pre-xfer and post-xfer commands on the server side.
>> Read man rsyncd.conf for more details on how to do it.
>>
>> Best Regards,
>>
>> Vitorio
>>
>>     
In my case I wanted an incremental backup to be done.
I used one script (delete_olders.sh) on pre-xfer to delete older backups 
if space is needed.
Another one (timestamp.sh) on post-xfer to rotate backup.
I use rotation.sh to rotate the log file.

My rsyncd.conf is configured with:
pre-xfer exec = /mnt/data/delete_olders.sh >> /mnt/data/script.log 2>&1
post-xfer exec = /mnt/data/timestamp.sh >> /mnt/data/script.log 2>&1

If you don't feel like coding your own scipt, rsnapshot do this as well.

Best regards,

Vitorio
-------------- next part --------------
#!/bin/sh

#Debug mode
DEBUG=0

#Name of the modules path
MODPATH=/mnt/data/modules/
#MODPATH=/Users/vit/Documents/Technique/rsync/test/

#Partition ID
MOUNTNAME=/dev/mirror/gm0s2
#MOUNTNAME=/dev/disk0s3

#Name of the backup directory
LASTEST=lastest

if [ $DEBUG -gt 0 ]; then
	echo "Starting script $0 at `date +"%Y-%m-%d-%H%M%S"` for $RSYNC_HOST_NAME"
fi

#Check if we have the good arg number
if [ $# -gt 0 ]; then
	if [ $# -lt 2 ]; then
		if [ $DEBUG -gt 0 ]; then
			echo "There is one argument, setting it as the module name"
		fi
		RSYNC_MODULE_PATH=$MODPATH$1
		RSYNC_MODULE_NAME=$1
		if [ $DEBUG -gt 0 ]; then
			echo "RSYNC_MODULE_PATH=$RSYNC_MODULE_PATH
RSYNC_MODULE_NAME=$RSYNC_MODULE_NAME"
		fi
	else
		echo "Too many arguments.
Usage:	$0 <name of the backup module> or
	$0 - without arguments, in which case RSYNC_MODULE_PATH
environment variable must indicate a module path without ending slash (/)
and RSYNC_MODULE_NAME a module name."
		exit
	fi
elif [ -z "$RSYNC_MODULE_PATH" ]; then
	echo "No RSYNC_MODULE_PATH detected.\n
Usage:	$0 <name of the backup module> or\n
	$0 - without arguments, in which case RSYNC_MODULE_PATH\
	environment variable must indicate a module path without ending slash (/)
and RSYNC_MODULE_NAME a module name."
	exit

elif [ -z "$RSYNC_MODULE_NAME" ]; then
	echo "No RSYNC_MODULE_NAME detected.\n
Usage:	$0 <name of the backup module> or\n
	$0 - without arguments, in which case RSYNC_MODULE_PATH\n
	environment variable must indicate a module path without ending slash (/)
and RSYNC_MODULE_NAME a module name."
	exit
fi

#Set the list of the modules
MODULES=$(echo $(ls -1 $MODPATH))

AVAIL=$(df | grep $MOUNTNAME | awk -F' ' '{print $4}')

if [ ! -e $RSYNC_MODULE_PATH/$LASTEST ]; then
	echo "$RSYNC_MODULE_PATH/$LASTEST directory does not exist at $(date +"%Y-%m-%d-%H%M%S")."
	exit
fi
SIZE=$(du -s $RSYNC_MODULE_PATH/$LASTEST | awk -F' ' '{print $1}')

if [ $DEBUG -gt 0 ]; then
	echo "MODULES=$MODULES\n
AVAIL = $AVAIL\n
SIZE = $SIZE"
fi

MODULE_COUNT=0

for tmp in $MODULES
do
	MODULE_COUNT=$(expr $MODULE_COUNT + 1)
	if [ $tmp = $RSYNC_MODULE_NAME ]; then
		i=$MODULE_COUNT
	fi
done
if [ $DEBUG -gt 0 ]; then
			echo "i=$i
MODULE_COUNT=$MODULE_COUNT"
fi

MODULE=$RSYNC_MODULE_NAME

while [ $AVAIL -lt $SIZE ];
do
	cd $MODPATH$MODULE
	tmp=$(ls -1 | head -n 1)
	if [ $DEBUG -gt 0 ]; then
			echo "MODPATH\$MODULE=$MODPATH$MODULE
tmp=$tmp"
	fi
	rm -Rf $tmp
	echo "Removing backup $tmp from $MODPATH$MODULE at $(date +"%Y-%m-%d-%H%M%S")."
	if [ $i -eq $MODULE_COUNT ]; then
		i=1
	else
		i=$(expr $i + 1)
	fi
	if [ $DEBUG -gt 0 ]; then
		echo "i=$i"
	fi
	AVAIL=$(df | grep $MOUNTNAME | awk -F' ' '{print $4}')
	MODULE=$(ls -1 .. | head -n $i | tail -n 1)
	if [ $DEBUG -gt 0 ]; then
		echo "AVAIL=$AVAIL
MODULE=$MODULE"
	fi
done

#echo ""
-------------- next part --------------
#!/bin/sh
DIR=/mnt/data
FILE=script.log

cd $DIR

if [ $? -ne 0 ]; then
	exit
fi

if [ -e $FILE ]; then
	exit
fi

cat $FILE | tail -n 100 > $FILE.new
mv -f $FILE.new $FILE
-------------- next part --------------
#!/bin/sh
#Turn on debug info
DEBUG=0

#Name of the backup and lastest directory
BACKUP=backup
LASTEST=lastest

if [ $DEBUG -gt 0 ]; then
	echo "Starting script $0 at $(date +"%Y-%m-%d-%H%M%S")"
fi

if [ $RSYNC_EXIT_STATUS -ne 0 ]; then
	echo "WARNING: rsync exited with status $RSYNC_EXIT_STATUS for $RSYNC_HOST_NAME at $(date +"%Y-%m-%d-%H%M%S")"
fi

#Check if we have the good arg number
if [ $# -gt 0 ]; then
	if [ $# -lt 2 ]; then
		if [ $DEBUG -gt 0 ]; then
			echo "There is one argument, setting it as the module path"
		fi
		RSYNC_MODULE_PATH=$1
	else
		echo "Too many arguments.\n
Usage:	$0 <path to the backup module> or\n
	$0 - without arguments, in which case RSYNC_MODULE_PATH\n
			 environment variable must indicate a module path."
		exit
	fi
elif [ -z "$RSYNC_MODULE_PATH" ]; then
	echo "No RSYNC_MODULE_PATH detected.\n
Usage:	$0 <path to the backup module> or\n
		$0 - without arguments, in which case RSYNC_MODULE_PATH\n
			 environment variable must indicate a module path."
	exit
fi

if [ $DEBUG -gt 0 ]; then
	echo "RSYNC_MODULE_PATH = $RSYNC_MODULE_PATH"
fi

#Change to working directory
cd $RSYNC_MODULE_PATH

if [ $? -gt 0 ]; then
	echo "Directory $RSYNC_MODULE_PATH does not exist at $(date +"%Y-%m-%d-%H%M%S")"
	exit
fi

#Check if the backup directory exists
if [ ! -d $BACKUP ]; then
        echo "Directory $BACKUP is missing in $PWD at $(date +"%Y-%m-%d-%H%M%S")"
	exit
fi

#Set up useful variables
DATE=$(date +"%Y-%m-%d-%H%M%S")

if  [ $DEBUG -gt 0 ]; then
	echo "DATE = $DATE"
fi

if [ ! -e lastest ]; then
	mv -f $BACKUP $DATE
	if [ $? -gt 0 ]; then
		echo "Could not move $BACKUP to $DATE for $RSYNC_HOST_NAME"
		exit
	fi
	
	ln -sf $DATE $LASTEST
	if [ $? -gt 0 ]; then
		echo "Could not link $LASTEST to $DATE for $RSYNC_HOST_NAME"
		exit
	fi
	if  [ $DEBUG -gt 0 ]; then
		echo "Moving backup and creating lastest."
	fi
else
    mv -f $BACKUP $DATE
    if [ $? -gt 0 ]; then
		echo "Could not move $BACKUP to $DATE for $RSYNC_HOST_NAME"
		exit
	fi
	
	rm -f $LASTEST
	if [ $? -gt 0 ]; then
		echo "Could not delete old $LASTEST symlink for $RSYNC_HOST_NAME at $(date +"%Y-%m-%d-%H%M%S")"
		exit
	fi
	
	ln -sf $DATE $LASTEST
	if [ $? -gt 0 ]; then
		echo "Could not link $LASTEST to $DATE for $RSYNC_HOST_NAME"
		exit
	fi
	if  [ $DEBUG -gt 0 ]; then
		echo "Moving backup and lastest."
	fi
fi

unset BACKUP DATE DEBUG LASTEST
#echo "--------------------------------------------------------------------------------\n"


More information about the rsync mailing list