forked from egeerardyn/grive-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrive-sync.sh
More file actions
executable file
·135 lines (111 loc) · 4.09 KB
/
grive-sync.sh
File metadata and controls
executable file
·135 lines (111 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
###############################################################################
# grive-sync
# This script runs grive and greps the log output to create a
# notify-osd message indicating what happened
# It's intended to be called via cron
#
# Grive is created by Nestal Wan at https://github.com/Grive/grive/
# This script is hacked up by Josh Beard at http://signalboxes.net
#
# I don't know how robust this script is and haven't done much testing
# Feel free to do whatever you want with it.
###############################################################################
# This needs to best set for notify-send if calling from cron
DISPLAY=:0.0
# Path to directory of your Google Drives, separate multiple drives by ":"
#GRIVE_DIRS="/home/myself/Grive/user@domain.com:/home/myself/Grive/user2@domain2.com"
GRIVE_DIRS="$1"
# Path to an icon for notify-osd
#NOTIFY_ICON="/home/josh/.icons/google-drive.png"
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
NOTIFY_BIN=$(which notify-send)
NOTIFY_ICON="$SCRIPT_PATH/icons/grive.png"
GRIVE_BIN=$(which grive)
###############################################################################
# You don't need to edit below here unless you really want to
###############################################################################
[ -z "$GRIVE_DIRS" ] && printf "Syntax: grive-sync.sh \"':' SEPARATED LIST OF FOLDERS TO SYNCHRONIZE\"\n" && exit 1
ps_bin=$(which ps)
rm_bin=$(which rm)
grep_bin=$(which grep)
sed_bin=$(which sed)
mktemp_bin=$(which mktemp)
# Create a temporary log file
TMPLOG=$($mktemp_bin /tmp/grive-XXXX)
# These are the strings we'll look for in the grive log to figure out what
# files were changed.
REMOVEL_MSG="deleted in local. deleting remote"
REMOVER_MSG="deleted in remote. deleting local"
UPLOAD_MSG="doesn't exist in server, uploading"
DOWNLOAD_MSG="created in remote. creating local"
# Test if we can locate grive
if ! type grive >/dev/null 2>&1; then
printf "Can't locate grive\n"
exit 1
fi
IFS=':' read -ra grive_dirs_arr <<< "$GRIVE_DIRS"
for key in "${!grive_dirs_arr[@]}"; do
GRIVE_DIR="${grive_dirs_arr[$key]}"
# GRIVE_DIR doesn't exist
[ ! -d "${GRIVE_DIR}" ] && printf "${GRIVE_DIR} does not exist.\n" && exit 1
# Function to extract filename from log file
get_filename() {
filename=$($grep_bin "$1" "${TMPLOG}"|$sed_bin 's/.*"\(.*\)"[^"]*$/\1/')
printf "$(basename "$filename")"
}
# Check if it's already running
if ! $ps_bin aux|$grep_bin -q -e '[g]rive '; then
# Run grive and output to a temporary log
${GRIVE_BIN} -p "${GRIVE_DIR}" -l "${TMPLOG}"
# Get the count of operations
_ldeletions=$($grep_bin -c "${REMOVEL_MSG}" "${TMPLOG}")
_rdeletions=$($grep_bin -c "${REMOVER_MSG}" "${TMPLOG}")
_uploads=$($grep_bin -c "${UPLOAD_MSG}" "${TMPLOG}")
_downloads=$($grep_bin -c "${DOWNLOAD_MSG}" "${TMPLOG}")
# Setup the notify-osd message
notify="Finished synchronizing $GRIVE_DIR\n"
if [ $_ldeletions -gt 0 ]; then
# If it's only one file, show the filename
if [ $_ldeletions -eq 1 ]; then
_filename=$(get_filename "${REMOVEL_MSG}")
notify="${notify}$_filename removed from local"
else
notify="${notify}${_ldeletions} removed from local"
fi
fi
if [ $_rdeletions -gt 0 ]; then
[ ! -z "$notify" ] && notify="${notify}\n"
if [ $_rdeletions -eq 1 ]; then
_filename=$(get_filename "${REMOVER_MSG}")
notify="${notify}$_filename removed from remote"
else
notify="${notify}${_rdeletions} removed from remote"
fi
fi
if [ $_uploads -gt 0 ]; then
[ ! -z "$notify" ] && notify="${notify}\n"
if [ $_uploads -eq 1 ]; then
_filename=$(get_filename "${UPLOAD_MSG}")
notify="${notify}$_filename uploaded"
else
notify="${notify}${_uploads} uploaded"
fi
fi
if [ $_downloads -gt 0 ]; then
[ ! -z "$notify" ] && notify="${notify}\n"
if [ $_downloads -eq 1 ]; then
_filename=$(get_filename "${DOWNLOAD_MSG}")
notify="${notify}$_filename downloaded"
else
notify="${notify}${_downloads} downloaded"
fi
fi
# Display the notify-osd message
if [ ! -z "$notify" ]; then
${NOTIFY_BIN} -i "${NOTIFY_ICON}" "Grive" "$notify"
fi
# Remove the grive ouput log
$rm_bin -f "${TMPLOG}"
fi
done