View Single Post
Old 03-10-2014, 01:59 AM   #1
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
linux backup of Kindle, annotations/pagenumbers/stuff with rsync

Just thought I'd share a script I use to backup my Kindle ebooks with the sidecar data. But first:


I use a custom column in calibre to make Amazon-sourced books match up to the .sdr data, so I can keep my notes/annotations even for sideloaded books and don't have to worry about syncing to Amazon etc.

The custom column (of the default type "Text, column shown in the tag browser") is named "#file", titled "Original Filename". In it, I store the filename that the Amazon book had when I added it from my Kindle, minus the file extension. It will be in the form of {book-title}_{ASIN}.

I use a custom Save Template for sending to device, (limited to the kindle2 device driver, since other devices don't need this) which looks like this:

Code:
program: 
ifempty(
	field('#file'),
	template(
		'{author_sort:sublist(0,1,&)}/{title} - {authors:sublist(0,1,&)}'
	)
)
Copy this code into the Save Template field of the device driver (see post #3 for details).

It uses the contents of custom column "file" as the savepath, if available. If not, (for instance, it isn't originally from Amazon, so I left it blank) it falls back on the main template, which I actually tweaked to only use the first author.



Now the script:

In order to preserve the .sdr/ and book data, I use an rsync script outside of calibre (saved as "$HOME/bin/kindlesync") which saves the contents of "/media/Kindle" not including the tts or index files and stuff, just the parts that might ever need to be preserved.

This also lets me restore my Kindle if it ever needs to be reset to factory conditions, or if I get a new device.

kindlesync script:
Code:
#!/bin/bash

## Set defaults

backup_folder="$HOME/Kindle Stuff/Kindle Backups/"
# These go in the backup folder
backup_excludes=.rsync-backup-exclude
restore_excludes=.rsync-restore-exclude


##### Declare usage

usage()
{
	cat <<- _EOF_
		Usage: kindlesync [OPTIONS]
		Back up contents of Kindle drive as Kindle.rsync	
		Does not include system info or TTS
		Deletes:
		    Personal Letters
		    EndActions
		    .fuse and .goutputstream garbage

		OPTIONS
		        --backup-excludes
		                        optional excludes file for backup
		                        defaults to: '$backup_excludes'
		        --restore-excludes
		                        optional excludes file for restore
		                        defaults to: '$restore_excludes'
		    -f, --folder        chooses backup folder, defaults to:
		                            '$backup_folder'
		    -b, --backup        perform backup only
		    -r, --restore       perform restore only
		    -d, --dry-run       backup, then do restore in test mode
		    -h, --help          show this usage message then exit
_EOF_
}


##### Test for interactive options

## By default, all switches turned off

backup_only=
restore_only=
dry_restore=

while [ "$1" != "" ]; do
    case $1 in
        -h|--help)          usage
                            exit
                            ;;
        -f|--folder)        shift
                            backup_folder=$1
                            ;;
		--backup-excludes)  shift
                            backup_excludes=$1
                            ;;
		--restore-excludes) shift
                            restore_excludes=$1
                            ;;
        -b|--backup)        backup_only=1
                            ;;
        -r|--restore)       restore_only=1
                            ;;
        -d|--dry-run)       dry_restore=1
                            ;;
        *)                  echo "kindlesync: unrecognized option '$1'"
                            echo "Try 'kindlesync --help' for more information."
                            exit 1
    esac
    shift
done

##### Declare Functions

do_backup()
{
    rsync -amv --exclude-from=$backup_excludes /media/Kindle/ ./Kindle.rsync
}

do_restore()
{
    rsync -amv --exclude-from=$restore_excludes --delete ./Kindle.rsync/ /media/Kindle
}

do_dry_restore()
{
    rsync -amv --dry-run --exclude-from=$restore_excludes --delete ./Kindle.rsync/ /media/Kindle
}

################################################################################################
#####                  OLD SYNC ONLY DOCUMENTS FOLDER                                ###########
#####                                                                                ###########
##### rsync -amv --exclude-from=.rsync-exclude /media/Kindle/documents/ ./documents  ###########
##### rsync -amv --delete ./documents/ /media/Kindle/documents                       ###########
################################################################################################



#### MAIN

if [ ! -d /media/Kindle/ ]; then
    echo "Your Kindle is not plugged in."
    exit 1
fi

cd "$backup_folder"

if [ "$backup_only" = "1" ]; then
    echo "Backing up to: $(pwd)/Kindle.rsync"
    do_backup
elif [ "$restore_only" = "1" ]; then
    echo "Restoring..."
    do_restore
elif [ "$dry_restore" = "1" ]; then
    echo -e "Backing up to: $(pwd)/Kindle.rsync\n\n"
    do_backup
    echo -e "Simulating restore; this isn't really happening.\n\n"
    do_dry_restore
else
    echo -e "Backing up to: $(pwd)/Kindle.rsync\n\n"
    do_backup
    echo -e "Restoring...\n\n"
    do_restore
fi
backup-excludes file:
Code:
#DOCUMENTS JUNK#
###Stupid letters I never read
- Personal\ letter*
- .fuse*
- .goutputstream-*
###I hate EndActions
- EndActions*
###Don't delete, since it may tell Amazon to download more .sdr
###But I don't need it in the backup
- *.partial

#SYSTEM#
+ /system/
+ /system/thumbnails/
+ /system/thumbnails/*
- /system/*

#TTS#
- /tts/

#Sandbox#
- /.active_content_sandbox/

#JAILBREAK#
- /acxe/
- /developer/
- /extensions/
- /linkss/
- /python/
- /usbnet/
restore-excludes file:
Code:
- /tts/
- /.active_content_sandbox/
###Don't delete, since it may tell Amazon to download more .sdr
- /documents/*.partial

#SYSTEM#
+ /system/
+ /system/thumbnails/
+ /system/thumbnails/*
- /system/*

#JAILBREAK#
- /acxe/
- /developer/
- /extensions/
- /linkss/
- /python/
- /usbnet/
Attached is an archive of the actual scripts, which you can unpack to your user profile.
Attached Files
File Type: gz kindlesync.tar.gz (1.5 KB, 275 views)

Last edited by eschwartz; 03-08-2015 at 03:14 AM. Reason: update excludes and stuff
eschwartz is offline   Reply With Quote