Skip to content

Instantly share code, notes, and snippets.

@TimSoethout
Forked from wernerb/sync.sh
Last active February 21, 2016 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimSoethout/4c5db37e7bd1a2b4fb49 to your computer and use it in GitHub Desktop.
Save TimSoethout/4c5db37e7bd1a2b4fb49 to your computer and use it in GitHub Desktop.
Sync folder with Google Drive. Recursively downloads with wget and Google Drive public links. Used to sync ebooks to my eReaders (Kobo). Be warned that public links are dangerous as they could allow someone to download your files. Be sure to use this only in secure environments. This has been tested to be used in POSIX shells as well as in bash.
#!/bin/sh
set -x
#input sharelink here. Example: https://drive.google.com/folderview?id=0B1g-MbiD2F6vdtOT92b3MoerO&usp=sharing
# SHARELINK="https://drive.google.com/folderview?id=idU&usp=sharing"
SHARELINK="https://drive.google.com/folderview?id=0B8HjeRxyzNm7RjZIdDFVWHkxeXM&usp=sharing"
DESTINATION="/mnt/onboard/gdrive"
# Change following to false when you don't want to delete files when they are missing from google drive. This can
REMOVEFILES=false
# Begin code
download () {
url=$1
folder=$2
changed=$3
wgetoutput="`wget --no-check-certificate -qO- $url 2>&1`"
mkdir -p "$folder"
commands="$(echo "$wgetoutput" | grep -Eo '\[,,\"(.*?)\",,,,,\"(.*?)\"')"
missingfiles="$(find "$folder" -type f -print)"
missingdirectories="$(find "$folder" -type d -print)"
#Go to next
subfolders="$(echo "$wgetoutput" | grep -Eo '\[,".*?\",\".*?folder' | cut -c 4- | cut -d\" -f 1)"
IFS="
"
for subfolder in $subfolders; do
name=$(echo "$wgetoutput" | grep -Eo 'entry-'$subfolder'.*?</div></div><div class=\"flip-entry-title\">(.*?)</div>' | grep -Eo 'entry-title">.*?</div>$' | cut -c 14- | sed 's/<\/div>//')
newdest="$2/$name"
escapedfile="$(echo "$newdest" | sed -e 's/[]\$*.^|[]/\\&/g')"
missingdirectories=$(echo "$missingdirectories" | sed -e "s@$escapedfile@@g" | sed '/^$/d')
output=$(download "https://drive.google.com/folderview?id=$subfolder&usp=sharing" "$newdest" $changed)
changed=$output
done
#Download files
echo "Downloading for $folder..." 1>&2;
for line in $commands; do
id=$(echo "$line" | grep -Eio \"[0-9a-z]+\" | head -1 | tr -d '"')
name=$(echo "$line" | sed -e 's/\[,,\"//' | sed s/\".*//'')
file="$folder/$name"
cmd=$(echo "wget --no-check-certificate -nc -q --no-cookies -O \"$file\" \"https://docs.google.com/uc?export=download&id="$id"\"")
eval "$cmd"
commandresult=$?
if [ "$commandresult" -eq 0 ]; then
echo "Downloaded: $file" 1>&2;
changed=$(($changed + 1))
else
echo "Skipped (Already exists or error): $file" 1>&2;
fi
escapedfile=$(echo "$file" | sed -e 's/[]@$*.^|[]/\\&/g')
missingfiles=$(echo "$missingfiles" | sed 's@'$escapedfile'@@g' | sed '/^$/d')
done
echo "Finished downloading!" 1>&2;
#Remove missing files
if [ $REMOVEFILES = true ]; then
echo "Checking files to remove from $folder..." 1>&2;
i=0
for line in $missingfiles; do
echo "Removing file: $line" 1>&2;
rm -f "$line"
changed=$(($changed + 1))
done
for line in $missingdirectories; do
echo "Removing directory: $line" 1>&2;
rm -rf "$line"
changed=$(($changed + 1))
done
echo "Finished scan!" 1>&2;
fi
echo $changed
}
result=$(download "$SHARELINK" "$DESTINATION" 0)
echo "Changed: $result files/folders"
if [ $result -ne 0 ]; then
echo "Sync made changes to disk!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment