Skip to content

Instantly share code, notes, and snippets.

@jgrar
Last active August 29, 2015 14:03
Show Gist options
  • Save jgrar/40957ef807c670d7f933 to your computer and use it in GitHub Desktop.
Save jgrar/40957ef807c670d7f933 to your computer and use it in GitHub Desktop.
autoclone - automagical disk backup
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd?", RUN+="/usr/local/bin/autoclone.sh"

INSTALL

`
	cp automount.sh /usr/local/bin && chmod a+x
	cp 10-autoclone.rules /etc/udev/rules.d
	udevadm control --reload-rules
	apt-get install pv
`

TODO

On Arch the script still gets killed before it's finished, no matter what I do.
So that needs to be fixed, but hopefully it will work fine on Raspbian (the target platform)
with just launching it in a subshell.

The progress indicator will need to accept lines of integers indicating percentage complete from
stdin, I've marked this line (25) where you need to replace a stop-gap and insert your own.
I found that pv, used in this way, will mimic dd, so using dd in the pipeline wasn't necessary (:

Mounting a backup image file partition

First find the offset from the fdisk output `image.info`, it will be located
in the 'start' column. multiply that number by 512 then use that as the
offset option to mount:

`
	# cat image.info

	Disk /dev/sdb: 7.5 GiB, 8004304896 bytes, 15633408 sectors
	Units: sectors of 1 * 512 = 512 bytes
	Sector size (logical/physical): 512 bytes / 512 bytes
	I/O size (minimum/optimal): 512 bytes / 512 bytes
	Disklabel type: dos
	Disk identifier: 0x00000000

	Device    Boot Start       End  Blocks  Id System
	/dev/sdb1          2  15633407 7816703   b W95 FAT32

	# mount -o loop,ro,offset=$((2 * 512)) -t auto image.img /media
`
#!/bin/sh
{
IMAGE_PATH="/var/spool/images"
dest="$IMAGE_PATH/$ID_SERIAL"
target="$DEVNAME"
if ! mkdir -p "$dest"
then
logger -t AUTOCLONE "failed to create destination path $dest"
exit 1
fi
destpart="$(df -P "$dest" | tail -1 | cut -d ' ' -f 1)"
if [ -n "$(echo $destpart | grep "$target""[0-9]*")" ]
then
logger -t AUTOCLONE "cannot clone disk to partition of that disk: $destpart"
exit 1
fi
fdisk -l "$target" > "$dest""/image.info"
pv -i 0.1 -nEE $target 2>&1 > "$dest""/image.img" |
/usr/local/bin/progress.py
} &
#!/usr/bin/python
# how will you indicate incompletion/errors ?
# obviously if the loop breaks before 100 that's an error
import sys
import time
# import gpio stuff
# init gpio stuff
# flash some leds
for line in sys.stdin:
if int(line) >= 25:
# 25% indicator led
if int(line) >= 50:
# 50% indicator led
if int(line) >= 75:
# 75% indicator led
if int(line) == 100:
# 100% indicator led
# break, should be EOF anyway
# flash leds to indicate completion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment