Skip to content

Instantly share code, notes, and snippets.

@zuf
Created July 6, 2015 15:48
Show Gist options
  • Save zuf/db65c7526d1eff00cc74 to your computer and use it in GitHub Desktop.
Save zuf/db65c7526d1eff00cc74 to your computer and use it in GitHub Desktop.
Shell script for automated sync mounted to usb action camera to my archive. GUI progress shown through zenity (matedialog) tool. This script was written for usage with udev rules.
#!/usr/bin/bash
# This script uses too much unneeded dependencies (like python for adding some integers). Sorry for that.
# You can clean it for your cases (replace python with bc for example).
# TODO: test rsync version shoud has --info=progress2 option (v3.0.10 or more)
#USER="`whoami`"
CONTOUR_MOUNTPOINT="/run/media/$USER/CONTOUR GPS/DCIM"
CONTOUR_ARCHIVE='/path/to/archive'
ZENITY="matedialog" # or "zenity"
executables=($ZENITY rsync exiftool find parallel stdbuf python numfmt awk mkdir wc rmdir rm head grep sort)
for item in ${executables[*]}
do
if [[ ! -x `which $item` ]]
then
echo "Executable unavailable: $item"
$ZENITY --error --text="Executable unavailable: $item"
exit 21
fi
done
if [[ ! `python -c"print(4398046511104 + 4398046511104 + 4398046511104 + 3)"` -eq 13194139533315 ]]
then
echo "Could not add big integers. Exit because may count wrong file sizes!"
$ZENITY --error --text="Could not add big integers. Exit because may count wrong file sizes!"
exit 22
fi
if [ -d "$CONTOUR_ARCHIVE" ]
then
echo "ContourGPS archive: $CONTOUR_ARCHIVE"
else
echo "Contour archive not found at: $CONTOUR_ARCHIVE"
$ZENITY --error --text="Contour archive not found at: $CONTOUR_ARCHIVE"
exit 1
fi
if [ -d "$CONTOUR_MOUNTPOINT" ]
then
echo "ContourGPS mounted at: $CONTOUR_MOUNTPOINT"
else
echo "Contour GPS not mounted at: $CONTOUR_MOUNTPOINT"
$ZENITY --error --text="Contour GPS not mounted at: $CONTOUR_MOUNTPOINT"
exit 2
fi
original_files_size=`find "$CONTOUR_MOUNTPOINT" -type f -printf '%s\n' | python -c"import sys; print(sum(map(int, sys.stdin)))"`
original_files_count=`find "$CONTOUR_MOUNTPOINT" -type f -printf '%s\n' | wc -l`
if [ $original_files_count -eq 0 ]
then
echo "Empty '$CONTOUR_MOUNTPOINT'. Nothing to sync with $CONTOUR_ARCHIVE."
$ZENITY --info --text="Empty '$CONTOUR_MOUNTPOINT'. Nothing to sync with $CONTOUR_ARCHIVE."
exit 10
fi
if [ $original_files_size -eq 0 ]
then
echo "All files in '$CONTOUR_MOUNTPOINT' has zero size. Nothing to sync with $CONTOUR_ARCHIVE."
$ZENITY --info --text="All files in '$CONTOUR_MOUNTPOINT' has zero size. Nothing to sync with $CONTOUR_ARCHIVE."
exit 11
fi
free_space=`df -P -B 1 $CONTOUR_ARCHIVE | tail -1 | awk '{print $4}'`
if [[ $free_space -le $original_files_size ]]
then
msg="Not enough free space at '$CONTOUR_ARCHIVE'! Need about `numfmt --to=iec-i --suffix=B $original_files_size`. Only `numfmt --to=iec-i --suffix=B $free_space` free."
echo "$msg"
$ZENITY --error --text="$msg"
exit 25
fi
LAST_SERIES_NUMBER=`find $CONTOUR_ARCHIVE -maxdepth 1 -type d -regextype posix-egrep -regex '.*/[0-9]{1,8}[_-][0-9]{4}[-_].*' | sort -r | head -n 1 | grep -Eo "/[0-9]{1,8}[-_]" | grep -Eo "[0-9]+"`
NEXT_SERIES_NUMBER=$((LAST_SERIES_NUMBER+1))
if test -z "$1"; then
NEW_ARCHIVE_DIR="${NEXT_SERIES_NUMBER}-`date +%Y-%m-%d`"
else
NEW_ARCHIVE_DIR="${NEXT_SERIES_NUMBER}-`date +%Y-%m-%d`_$1"
fi
NEW_ARCHIVE_DIR_PATH="$CONTOUR_ARCHIVE/$NEW_ARCHIVE_DIR"
echo "New archive directory: $NEW_ARCHIVE_DIR_PATH"
if [ -d "$CONTOUR_MOUNTPOINT" ]
then
#$(su "$USER" -c "export DISPLAY=:0.0; rsync -av | $ZENITY --progress")
mkdir -p "$NEW_ARCHIVE_DIR_PATH" || exit 4
if [[ ! -d "$NEW_ARCHIVE_DIR_PATH" ]]
then
msg="Archive destination '$NEW_ARCHIVE_DIR_PATH' does not exists."
echo "$msg"
$ZENITY --error --text="$msg"
exit 27
fi
echo "rsync"
rsync -a -v --info=progress2 --outbuf=None --out-format=#%n "$CONTOUR_MOUNTPOINT" "$NEW_ARCHIVE_DIR_PATH" | stdbuf -i16 -o16 -e16 tr '\r' '\n' | grep --line-buffered -oE "(#.*|[0-9]+%)" | $ZENITY --width=600 --progress --title="Synchronizing to $NEW_ARCHIVE_DIR_PATH" --percentage=0 --auto-kill --auto-close
echo "exif rename"
find "$NEW_ARCHIVE_DIR_PATH" -type f | parallel --bar 'exiftool -s -d %Y%m%d_%H%M%S_%%f.%%e "-filename<AllDates" {}' | $ZENITY --width=600 --progress --title="Rename files at $NEW_ARCHIVE_DIR_PATH" --percentage=0 --auto-kill --auto-close
echo "final move"
find "$NEW_ARCHIVE_DIR_PATH" -type f | parallel --bar mv {} "$NEW_ARCHIVE_DIR_PATH" | $ZENITY --width=600 --progress --title="Consolidating files in one directory: $NEW_ARCHIVE_DIR_PATH" --percentage=0 --auto-kill --auto-close
# TODO: extract GPS data from contour MOV files (https://gist.github.com/zuf/bd528783f98bf692747a)
echo "remove empty dirs"
find "$NEW_ARCHIVE_DIR_PATH" -depth -type d -empty -exec rmdir {} \;
copied_files_size=`find "$NEW_ARCHIVE_DIR_PATH" -type f -printf '%s\n' | python -c"import sys; print(sum(map(int, sys.stdin)))"`
copied_files_count=`find "$NEW_ARCHIVE_DIR_PATH" -type f -printf '%s\n' | wc -l`
if [ $original_files_size -eq $copied_files_size ]
then
if [ $original_files_count -eq $copied_files_count ]
then
echo "Looks like copied files are ok. Remove originals."
find "$CONTOUR_MOUNTPOINT" -depth -mindepth 1 -exec rm -rf {} \;
else
echo "Copied files size does not match original!"
$ZENITY --error --text="Copied files size does not match original!"
fi
else
echo "Copied file seizes other than originals!"
$ZENITY --error --text="Copied file seizes other than originals! ($original_files_size!=$copied_files_size)"
fi
echo "Done"
xdg-open "$NEW_ARCHIVE_DIR_PATH"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment