Script for restore version files.

I am write sh script for get info versions files and restore files for date. Please check, using and commenting the script.

This write on FreeBSD, may be run on other unix like systems.

#!/bin/sh 

PATH_VERSION=`pwd`
list_date() 
{
	ls -l -R "${PATH_VERSION}" | grep -E '.*\~20[0-9]{6}-[0-9]{6}\..{1,100}$' | awk -F'[~.]' '{print $(NF-1)}' | sed 's/-.*//' | sort | uniq
}

list_file_for_date()
{
	NEED_DATE=$1
	PATTERN_FIND=".*~${NEED_DATE}-[0-9][0-9].*"
	find "${PATH_VERSION}"  -regex ${PATTERN_FIND} -print
}

list_dir_for_date()
{
	NEED_DATE=$1
	PATTERN_FIND=".*~${NEED_DATE}-[0-9][0-9].*"
	(find "${PATH_VERSION}" -regex ${PATTERN_FIND} -print | while read FILE_OUT
	do
		echo "${FILE_OUT%/*}"

	done) | sort |uniq
}

restore_file_for_date()
{
	NEED_DATE=$1
	PATTERN_FIND=".*~${NEED_DATE}-[0-9][0-9].*"
	find "${PATH_VERSION}" -regex ${PATTERN_FIND} -print | while read FILE_CUR
	do
		restore_file "${FILE_CUR}"
	done
}

restore_file()
{
	FILE_IN="$1"
	FILE_OUT=`echo "${FILE_IN}" | sed -E 's/~[0-9]{8}-[0-9]{6}\./\./'| sed 's/.stversions\///' ` 
	echo check path "${FILE_OUT%/*}"
	if ! [ -d "${FILE_OUT%/*}" ]
	then
		echo dir not found, need make: "${FILE_OUT%/*}"
	###########  Please uncomment needs command
		# mkdir -p "${FILE_OUT%/*}"
	fi

	echo mv or cp \"${FILE_IN}\" \"${FILE_OUT}\" 

	###########  Please uncomment needs command
	# mv \"${FILE_IN}\" \"${FILE_OUT}\" 
	# cp \"${FILE_IN}\" \"${FILE_OUT}\" 
}

case "$1" in
	list_date)
		list_date
	;;
	list_file)
		list_file_for_date $2
	;;
	
	restore_file)
		restore_file_for_date $2
	;;

	list_dir_for_date)
		list_dir_for_date $2
	;;

	*)
		echo Please run the script from directory ".stversions" your repo
		echo command:
		echo	list_date
		echo	list_file date - date format YYYYMMDD
		echo	list_dir_for_date date - date format YYYYMMDD
		echo	restore_file date - date format YYYYMMDD
	;;
esac

Thanks for sharing! I have been hoping that eventually a file recovery tool would be built into the Syncthing web interface, but with the limited developer resources, this is a low priority item.

We could do something halfway “official” here, but the need hasn’t come up yet. You’re looking for stuff to restore a given folder to the state it was on at a given date? The information we keep isn’t really optimized for that (not being intended as a full backup system), particularly we don’t know when files were deleted.

I need to get information when files are changed. My script generates a report about it. And if you need some files to restore in batch mode - allows you to do this. This is somewhat easier than the hands analyze catalog file versions.