View Single Post
Old 08-13-2014, 05:24 AM   #5
Adoby
Handy Elephant
Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.Adoby ought to be getting tired of karma fortunes by now.
 
Adoby's Avatar
 
Posts: 1,736
Karma: 26785668
Join Date: Dec 2009
Location: Southern Sweden, far out in the quiet woods
Device: Thinkpad E595, Ubuntu Mate, Huawei Mediapad 5, Bouye Likebook Plus
OK!

I accepted the implied challenge and improved the script.

Now I use flock to ensure that only one instance of the script can run, use zenity to provide some feedback while the snapshot is written by rsync and write a small log to each snapshot folder. Also it is now all in one file, this is no longer a general folder snapshotter that is used to take snapshots of the calibre libraries.

It is now a calibre launcher script that also takes a snapshot after calibre exits.

Both flock and zenity are installed by default in Ubuntu 14.04.01, and also in many other dists, I think. So typically no need to do anything other than fill in the correct paths, place the script in the file ~/bin/calibre and make it executable.

~/bin/calibre
Spoiler:

Code:
#!/bin/sh
# calibre launcher that also takes a snapshot of the calibre libraries. 
# Call it just "calibre" and place in ~/bin and make executable
# Makes use of rsync and hardlinks to create snapshots that look like full backups. 
# Don't use or update files in a snapshot. Copy the snapshot first.
# Deletes snapshots older than a certain number of days.
# Uses flock(1) to ensure only one instance of this script can run.
# Uses zenity(1) to provide progress dialog and error message if needed.
# Both flock and zenity are installed by default in Ubuntu 14.04.

# Change these variables to customize:
snapshot_path='/full/path/to place to store snapshots'
source_path='/full/path/to place where the calibre library/libraries are'
age_snapshot='30'
snapshot_name=$(date +%F_%T)

# Use subshell and flock to ensure only one instance of this script can run
(
	# Exit if script is already running
	flock -n 9 || exit 1

 	# Run calibre
	/opt/calibre/calibre

	# Run the majority of the script in a subshell, and send the output from that subshell to zenity
	(
		# Output text to zenity progress dialog
		echo "10\n# Preparing for snapshot, please wait..."

		# Delete earlier failed attempt at making a snapshot, if any
		rm -rf "$snapshot_path"/unfinished		

		# Find latest snapshot, if any
		latest_snapshot=$(ls -rt "$snapshot_path" | tail -1)

		# Create folder for this snapshot, unfinished so far...
		mkdir "$snapshot_path"/unfinished

		# Grab new snapshot. Use rsync to create hard links to files already in latest previous snapshot(s).

		echo "40\n# Taking snapshot with rsync, please wait..."

		rsync -a --delete --stats \
		      --link-dest="$snapshot_path"/"$latest_snapshot" \
		      "$source_path" \
		      "$snapshot_path"/unfinished > "$snapshot_path"/unfinished/rsync.log

		# Finished!
		if [ $? -eq 0 ]; then
			mv "$snapshot_path"/unfinished "$snapshot_path"/"$snapshot_name"
		else
			exit 1
		fi

		# Delete snapshots older than $age_snapshot

		echo "# Deleting old snapshots, please wait..."

		find $snapshot_path -maxdepth 1 -type d -mtime +$age_snapshot -print0 | xargs -0 rm -rf

		echo "99\n# Snapshot done! Check rsync.log in snapshot folder."
		sleep 2
		echo "100"
	) |
	zenity --progress --auto-close --pulsate \
	  --title="Taking snapshot of calibre library" \
	  --text="Please wait..." \
	  --percentage=1

	# Snapshot failed?
	if [ "$?" = 1 ] ; then
	        zenity --error --text="Snapshot failed."
	fi

) 9>/var/lock/snapshot_calibre_lock
Adoby is offline   Reply With Quote