-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfunctions
245 lines (160 loc) · 5.81 KB
/
functions
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
####################################################################
# Prey Retrieve Module Base Functions - by Tomas Pollak (bootlog.org)
# URL: http://preyproject.com
# License: GPLv3
####################################################################
perform_backup(){
local find_args=$(parse_arguments)
if [ -z "$find_args" ]; then
log ' !! No arguments for query given. You need to enter a search string, date range or file size range.'
return 1
fi
local upload_count=0
local exclude="-not -empty -not -regex '.*/\..*'" # exclude hidden files
local escaped_extensions=$(echo $backup__search_extensions | sed "s/|/\\\|/g")
local search_path=$(eval echo "$backup__search_path")
local find_command="find "$search_path" -type f $find_args -regex \".*\.\(${escaped_extensions}\)\" $exclude -printf \"%T@\t%s\t%p\n\""
log " -- Looking for files in ${search_path} using criteria: ${find_args}"
# currently sorting by size, from small to big, so we get to send as many as possible
# reverse sort -n -r
local found_files=$(eval "$find_command" | sort -n)
local found_files_count=$(echo "$found_files" | grep -v '^$' | wc -l)
log " -- ${found_files_count} matches found!"
[ $found_files_count -eq 0 ] && return 0
while read file; do
local file_date=$(echo "$file" | cut -f1)
local file_name=$(echo "$file" | cut -f3)
local file_size=$(echo "$file" | cut -f2)
local file_size_human=$(($file_size/1024))
[ ! -f "$file_name" ] && continue
log " -- Found file: (${file_size_human} KB) $file_name"
if [ -n "$(is_file_sent "$file_name" "$file_date")" ]; then
log " -- File has already been sent! Skipping..."
else
zip_and_upload "$file_name"
if [ $? -eq 0 ]; then
log " -- File sent! Marking as uploaded..."
mark_as_sent "$file_date" "$file_size" "$file_name"
store_in_memory "$file_name" "$file_size"
upload_count=$(($upload_count+1))
# [ -n "$test_mode" ] && break
else
log " -- Unable to upload to backend. Check your login/pass and that you have available storage space."
break
fi
fi
done <<< "$found_files"
log " ++ ${upload_count} files backed up!"
if [ $upload_count -gt 0 ]; then
local file_list=$(generate_query_string 'backup_files', '&device')
local full_query="device[backup][backend]=${backup__backend}$file_list"
update_device_info_with "$full_query"
fi
}
store_in_memory(){
local urlencoded=$(urlencode "$1")
store_key_value 'backup_files' "backup__${urlencoded}" "$2"
}
parse_arguments(){
local find_args=''
if [ -n "$backup__search_string" ]; then
find_args="-iname *${backup__search_string}*"
fi
if [ -n "$backup__max_size_in_kb" ]; then
find_args="$find_args -size -${backup__max_size_in_kb}k"
fi
if [ -n "$backup__last_modified" ]; then
find_args="$find_args -mtime -${backup__last_modified}"
fi
echo "$find_args"
}
zip_and_upload(){
log " ++ Processing $file_name..."
local file_name="$1"
local normalized=$(normalize_file_name "$file_name")
local zip_file_name="${tmpdir}/$(basename $normalized).zip"
log " -- Zipping to ${zip_file_name}..."
zip_file "$file_name" "$zip_file_name"
[ $? -ne 0 ] && echo " -- Error while zipping!" && return 2
upload_file "$zip_file_name"
local rs=$?
log " -- Removing zipped file..."
rm -f "$zip_file_name"
return $rs
}
normalize_file_name(){
echo "$1" | sed "s/[^a-zA-Z0-9\.-\/]/_/g"
}
is_file_sent(){
egrep "${1}|${2}" "${module_path}/${backup__sent_files_list}" &> /dev/null && echo 1 || return 0
}
mark_as_sent(){
echo -e "$1,$2,$3" >> "${module_path}/${backup__sent_files_list}"
}
clear_sent_files(){
echo "" > "${module_path}/${backup__sent_files_list}"
}
# gets filename, normalized
zip_file(){
local original=$1
local zipped=$2
[ -n "$backup__zip_password" ] && zip_password="-P $backup__zip_password"
zip -9 $zip_password "$zipped" "$original" 1> /dev/null
[ -f "$zipped" ] && return 0 || return 1
}
upload_file(){
log " -- Uploading to ${backup__backend}!"
# [ -n "$test_mode" ] && return 0
eval upload_to_${backup__backend} "$1"
}
upload_to_dropbox(){
local file_to_upload="$1"
[ -n "$backup__backend_path" ] && remote_path="-d $backup__backend_path"
"${module_path}/lib/dropbox_uploader.sh" $remote_path -v \
-u "$backup__backend_user" \
-p "$backup__backend_pass" \
-f "$file_to_upload"
return $?
}
upload_to_amazon_s3(){
local file_to_upload="$1"
[ -z "$backup__backend_path" ] && get_last_bucket_in_s3
[ $? -ne 0 ] && return 1
log " -- Uploading to bucket $backup__backend_path at Amazon S3..."
S3_ACCESS_KEY_ID="$backup__backend_user" \
S3_SECRET_ACCESS_KEY="$backup__backend_pass" \
"${module_path}/lib/s3" put "$backup__backend_path" "$file_to_upload"
return $?
}
get_last_bucket_in_s3(){
log " -- Getting bucket list..."
backup__backend_path=$(S3_ACCESS_KEY_ID="$backup__backend_user" \
S3_SECRET_ACCESS_KEY="$backup__backend_pass" \
"${module_path}/lib/s3" buckets | head -1)
return $?
}
upload_to_ubuntu_one(){
[ -z "$(which python2)" ] && log " -- Python interpreter not found! Cannot continue." && return 1
local file_to_upload="$1"
local credentials_file='credentialfile.txt'
if [ -z "$ubuntu_one_oauth_tokens" ]; then
log " -- Getting OAuth tokens..."
PYTHONPATH="${module_path}/lib" "${module_path}/lib/u1rest/createfilekeystore.py" \
--email "$backup__backend_user" \
--password "$backup__backend_pass" \
--name PreyBackup &> /dev/null
[ $? -ne 0 ] && return 1
log " -- Done! Pushing file to Ubuntu One's servers..."
# keep them in memory to avoid checking every time
ubuntu_one_oauth_tokens=$(cat "$credentials_file")
else
log " -- Reusing previous OAuth tokens!"
# dump in memory oauth tokens to u1rest's credentials file
echo "$ubuntu_one_oauth_tokens" > "$credentials_file"
fi
python2 "${module_path}/lib/u1_upload.py" "$file_to_upload"
rs=$?
rm -f "$credentials_file"
return $rs
}