Qnap [Tuto QNAP] Garder deux fichiers locaux en sync

giopas

Grand Maître Jedi
20 Avril 2015
1 084
9
68
EU
Bonjour à tous,

voici un script qui vous permettra de garder deux fichiers qui se trouvent en local en sync. De plus, chaque fois que le script dois syncroniser les deux fichier, le système de notification QTS est mis en marche.

Code:
#!/bin/sh

# select the two files
FILE1="/dir1/file1"
FILE2="/dir2/file1"

# use or create a log file with timestamp of the output
LOG="Log.txt"
TIMESTAMP=$(date +"%Y-%m-%d %Hh:%M")

if [ ! -e $LOG ]; then
	touch $LOG
	echo "$TIMESTAMP - INFO: '$LOG' does not exists but has been created." >&2
	# send output to LOG file
	echo "$TIMESTAMP - INFO: '$LOG' does not exists but has been created." >> $LOG
# else
#	echo "$TIMESTAMP - INFO: '$LOG' exists and it will be used if any change to '$FILE1' 
# 	or to '$FILE2' is needed." >&2
fi

# You can also pass the two file names as arguments for the script
if [[ $# == 2 ]]; then
	FILE1=$1
	FILE2=$2
fi

# check if the two files exist and are regular
if [ -f "$FILE1" -a -f "$FILE2" ]; then

	# meanwhile compare FILE1 against FILE2
	# if files are identical, stop there
	if cmp "$FILE1" "$FILE2" 2>/dev/null>/dev/null; then
		# send output to terminal
		echo "$TIMESTAMP - INFO: '$FILE1' and '$FILE2' are identical." >&2
		# send output to LOG file
		# echo "$TIMESTAMP - INFO: '$FILE1' and '$FILE2' are identical." >> $LOG

	# if FILE1 is newer than FILE2, copy FILE1 over FILE2
	elif [ "$FILE1" -nt "$FILE2" ]; then
		# if copy is successful
		if cp -p "$FILE1" "$FILE2"; then
			# send output to terminal
			echo "$TIMESTAMP - INFO: '$FILE1' replaced '$FILE2'." >&2
			# send output to LOG file
			echo "$TIMESTAMP - INFO: '$FILE1' replaced '$FILE2'." >> $LOG
			# send output to QTS
			/sbin/log_tool -a "$TIMESTAMP - INFO: '$FILE1' replaced '$FILE2'." -t 1 >&2
		# if copy is unsuccessful
		else
			# send output to terminal
			echo "$TIMESTAMP - ERROR: FAILED to replace '$FILE2' with '$FILE1'." >&2
			# send output to LOG file
			echo "$TIMESTAMP - ERROR: FAILED to replace '$FILE2' with '$FILE1'." >> $LOG
			# send output to QTS
			/sbin/log_tool -a "$TIMESTAMP - ERROR: FAILED to replace '$FILE2' with '$FILE1'" -t 2 >&2
			exit 1
		fi

	# if FILE1 is older than FILE2, copy FILE2 over FILE1
	elif [ "$FILE1" -ot "$FILE2" ]; then 
		# if copy is successful
		if cp -p "$FILE2" "$FILE1"; then
			# send output to the LOG file
			echo "$TIMESTAMP - INFO: '$FILE2' replaced '$FILE1'." >&2
			# send output to LOG file
			echo "$TIMESTAMP - INFO: '$FILE2' replaced '$FILE1'." >> $LOG
			# send output to QTS
			/sbin/log_tool -a "$TIMESTAMP - INFO: '$FILE2' replaced '$FILE1'." -t 1 >&2
		# if copy is unsuccessful
		else
			# send output to the LOG file
			echo "$TIMESTAMP - ERROR: FAILED to replace '$FILE2' with '$FILE1'." >&2
			# send output to LOG file
			echo "$TIMESTAMP - ERROR: FAILED to replace '$FILE2' with '$FILE1'." >> $LOG
			# send output to QTS
			/sbin/log_tool -a "$TIMESTAMP - ERROR: FAILED to replace '$FILE2' with '$FILE1'." -t 2 >&2
			exit 1
		fi

	# if two files are not identical but with same modification date
	else
		# send output to the LOG file
		echo "$TIMESTAMP - ERROR: We should never reach this point. Something is wrong in the script." >&2
		# send output to LOG file
		echo "$TIMESTAMP - ERROR: We should never reach this point. Something is wrong in the script." >> $LOG
		# send output to QTS
		/sbin/log_tool -a "$TIMESTAMP - ERROR: We should never reach this point. Something is wrong in the script." -t 2 >&2
		exit 1
	fi

	# if one file does not exist or is not valid, exit
else
	# send output to the LOG file
	echo "$TIMESTAMP - ERROR: One of the files does not exist, has been moved or renamed." >&2
	# send output to LOG file
	echo "$TIMESTAMP - ERROR: One of the files does not exist, has been moved or renamed." >> $LOG
	# send output to QTS
	/sbin/log_tool -a "$TIMESTAMP - ERROR: One of the files does not exist, has been moved or renamed." -t 2 >&2
	exit 1
fi

Sauf la partie de notification sur QTS, le reste devrait marcher sur n'importe quel machine *unix.

enjoy, ;-)

giopas

CREDITS and REFERENCES:
http://forum.qnap.com/viewtopic.php?f=349&t=112917
http://stackoverflow.com/questions/31975667/copy-last-modified-binary-file-over-the-other-one/