# Script for restore version files.

**URL:** https://forum.syncthing.net/t/script-for-restore-version-files/1842
**Category:** Development
**Created:** [February 17, 2015, 11:11pm UTC](https://forum.syncthing.net/t/script-for-restore-version-files/1842 "2015-02-17T23:11:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ged](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/g/85e7bf/32.png) [@Ged](https://forum.syncthing.net/u/Ged)
#### Post date: [February 17, 2015, 11:11pm UTC](https://forum.syncthing.net/t/script-for-restore-version-files/1842/1 "2015-02-17T23:11:43Z")

</div>

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
```

---

<div class="post-metadata">

### Author: ![anaqreon](https://forum.syncthing.net/user_avatar/forum.syncthing.net/anaqreon/32/5218_2.png) [@anaqreon](https://forum.syncthing.net/u/anaqreon)
#### Post date: [February 18, 2015, 10:50am UTC](https://forum.syncthing.net/t/script-for-restore-version-files/1842/2 "2015-02-18T10:50:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![calmh](https://forum.syncthing.net/user_avatar/forum.syncthing.net/calmh/32/15311_2.png) [@calmh](https://forum.syncthing.net/u/calmh)
#### Post date: [February 19, 2015, 6:58am UTC](https://forum.syncthing.net/t/script-for-restore-version-files/1842/3 "2015-02-19T06:58:38Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Ged](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/g/85e7bf/32.png) [@Ged](https://forum.syncthing.net/u/Ged)
#### Post date: [February 24, 2015, 8:59pm UTC](https://forum.syncthing.net/t/script-for-restore-version-files/1842/4 "2015-02-24T20:59:27Z")

</div>

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.
