Created
November 19, 2016 22:58
-
-
Save ntfc/35d7f6feb852a240e293d0ba94af3549 to your computer and use it in GitHub Desktop.
Used to synchronize photos between a syncthing share folder and an actual Photos folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Synchronize two photo folders using rsync. | |
# * Handles moves by: | |
# 1. See if a file to be synched is already on target (with the same name) | |
# 2. If file is already on target, compare checksums. | |
# 2a- If checksums match, it was a file move, so delete file from target | |
# 2b- If checksums don't match, we are possibly talking about different files | |
# * Deletions are not synchronized to keep all files on target | |
SRC="$1/" | |
DST="$2/" | |
DUPS_TO_DEL=() | |
if [ ! -d "$SRC" ]; then | |
echo "ERROR: source folder '$SRC' does not exist" | |
exit 1 | |
fi | |
if [ ! -d "$DST" ]; then | |
echo "ERROR: destination folder '$DST' does not exist" | |
exit 1 | |
fi | |
# find files to be copied | |
MOD_FILES=$(/usr/bin/rsync -ain --ignore-existing "$SRC" "$DST" | egrep '^>' | cut -f 1 -d ' ' --complement) | |
while IFS= read -r f; do | |
# get the filename with extension | |
FILE_NAME=$(basename "$f") | |
# returns something if filename is already present on $DST | |
FIND_RESULTS=$(/usr/bin/find "$DST" -type f -name "$FILE_NAME") | |
if [ ! -z "$FIND_RESULTS" ]; then | |
while IFS= read -r DST_FILE; do | |
SRC_MD5=$(md5sum "$SRC/$f" | awk '{print $1}') | |
DST_MD5=$(md5sum "$DST_FILE" | awk '{print $1}') | |
# do a md5 comparison | |
if [ "$SRC_MD5" == "$DST_MD5" ]; then | |
DUPS_TO_DEL+=("$DST_FILE") | |
fi | |
done <<< "$FIND_RESULTS" | |
fi | |
done <<< "$MOD_FILES" | |
# perform the actual sync | |
/usr/bin/rsync -av "$SRC" "$DST" | |
if [ ${#DUPS_TO_DEL[@]} -ne 0 ]; then | |
echo | |
echo "Duplicated files to be removed on destination path '$DST':" | |
echo "${DUPS_TO_DEL[@]}" | |
/usr/bin/rm -v "${DUPS_TO_DEL[@]}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment