Version 0 of autosave

Updated 2008-03-28 13:31:48 by sarnold

2008-03-28 Sarnold: I wanted a cron task to make backups of an important file, and be able to simply restore or examine a backup. The following script is just to be invoked every 3 minutes (or more if you want) by calling autosave /path/to/myfile.

It creates a directory _SAVE and subdirectories named after the date (%Y%m%d) which contain indexed backups. Just see the listing:

- myprecious.gnumeric
|- myprecious.gnumeric
|-+ 20080320
 |- 1_myprecious.gnumeric
 |- 2_myprecious.gnumeric
|-+ 20080321
 |- 1_myprecious.gnumeric

autosave source code

#!/usr/bin/env tclsh

proc mkdir {dir name} {
	if {![file isdirectory $dir/$name]} {
		file mkdir $dir/$name
	return $dir/$name

proc save {file dir} {
	set date [clock format [clock seconds] -format "%Y%m%d"]
	set dir [mkdir $dir $date]
	set index 0
	while {[file exists $dir/[incr index]_$file]} {}
	file copy $file $dir/${index}_$file

proc main {file} {
	set dir [file dirname [file normalize $file]]
	cd $dir
	set file [file tail $file]
	set save [mkdir $dir _SAVE]
	if {![file exists $save/$file]} {
		file copy $file $save/$file
		save $file $save
	} elseif {[file mtime $file] > [file mtime $save/$file]} {
		file copy -force $file $save/$file
		save $file $save