-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_google_files.sh
More file actions
executable file
·408 lines (345 loc) · 13.6 KB
/
backup_google_files.sh
File metadata and controls
executable file
·408 lines (345 loc) · 13.6 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/bin/bash
# Google Drive Native Files Backup Script
# This script downloads Google-native files (Docs, Sheets, Slides, etc.) from the
# CodeLaundry folder on Google Drive, converts them to portable formats, and
# creates a zip archive preserving the folder structure.
set -e
# Configuration
GDRIVE_BASE="CodeLaundry"
GDRIVE_SUBFOLDERS=("Certificazione ISO9001" "Gestione" "Offerte" "Works")
RCLONE_REMOTE="gdrive"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUTPUT_DIR="${SCRIPT_DIR}/drive"
BACKUP_DIR="${TMPDIR:-/tmp}/google-files-backup-$$"
# Logging configuration
LOG_DIR="${SCRIPT_DIR}/logs"
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}" .sh)"
LOG_TIMESTAMP="$(date '+%Y-%m-%d_%H-%M-%S')"
LOG_FILE="${LOG_DIR}/${SCRIPT_NAME}_${LOG_TIMESTAMP}.log"
# Global variables to store file info between functions
GOOGLE_FILES_INFO=""
GOOGLE_FILES_COUNT=0
# Create directories
mkdir -p "${LOG_DIR}"
mkdir -p "${OUTPUT_DIR}"
mkdir -p "${BACKUP_DIR}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Log functions
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] $1" >> "${LOG_FILE}"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [WARN] $1" >> "${LOG_FILE}"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR] $1" >> "${LOG_FILE}"
}
log_file() {
echo -e "${BLUE}[FILE]${NC} $1"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [FILE] $1" >> "${LOG_FILE}"
}
# Check if rclone is installed
check_rclone() {
if ! command -v rclone >/dev/null 2>&1; then
log_error "rclone is not installed. Please run one of the backup scripts first to install it."
exit 1
fi
if ! rclone listremotes | grep -q "^${RCLONE_REMOTE}:"; then
log_error "rclone remote '${RCLONE_REMOTE}' not configured. Please run one of the backup scripts first."
exit 1
fi
log_info "rclone configured and ready."
}
# Check if Google Drive folders exist
check_gdrive_folders() {
for subfolder in "${GDRIVE_SUBFOLDERS[@]}"; do
local folder="${GDRIVE_BASE}/${subfolder}"
if ! rclone lsd "${RCLONE_REMOTE}:${folder}" >/dev/null 2>&1; then
log_error "Google Drive folder '${folder}' not found."
exit 1
fi
log_info "Found Google Drive folder: ${folder}"
done
}
# Get Google file type description from MIME type
get_file_type() {
local mime="$1"
case "${mime}" in
"application/vnd.google-apps.spreadsheet") echo "Google Sheet → .xlsx" ;;
*"spreadsheet"*) echo "Google Sheet → .xlsx" ;;
"application/vnd.google-apps.document") echo "Google Doc → .docx + .pdf" ;;
"application/vnd.google-apps.presentation") echo "Google Slides → .pptx + .pdf" ;;
"application/vnd.google-apps.drawing") echo "Google Drawing → .png + .pdf" ;;
"application/vnd.google-apps.form") echo "Google Form → .pdf" ;;
*"wordprocessing"*|*"document"*) echo "Google Doc → .docx + .pdf" ;;
*"presentation"*) echo "Google Slides → .pptx + .pdf" ;;
*) echo "Google File → .pdf" ;;
esac
}
# List Google-native files and ask for confirmation
list_and_confirm() {
log_info "Scanning for Google-native files in ${GDRIVE_BASE}/..."
local all_files_info=""
local total_count=0
echo ""
echo "=========================================="
echo " Google-native files found:"
echo "=========================================="
for subfolder in "${GDRIVE_SUBFOLDERS[@]}"; do
local folder="${GDRIVE_BASE}/${subfolder}"
# Get file list using rclone lsjson
local files_json
files_json=$(rclone lsjson "${RCLONE_REMOTE}:${folder}" \
--recursive \
--files-only \
2>/dev/null || echo "[]")
if [[ "${files_json}" == "[]" ]] || [[ -z "${files_json}" ]]; then
continue
fi
# Parse JSON and get Google-native files
# Google-native files have Size=-1 in rclone output
local google_files_info
google_files_info=$(echo "${files_json}" | \
python3 -c "
import sys
import json
data = json.load(sys.stdin)
for item in data:
size = item.get('Size', 0)
mime = item.get('MimeType', '')
path = item.get('Path', '')
# Google-native files have Size=-1
# Skip shortcuts
if size == -1 and 'shortcut' not in mime.lower():
print(f'{mime}|{path}')
" 2>/dev/null || echo "")
if [[ -z "${google_files_info}" ]]; then
continue
fi
echo ""
echo -e " ${GREEN}${subfolder}/${NC}"
while IFS='|' read -r mime path; do
if [[ -n "${path}" ]]; then
local file_type
file_type=$(get_file_type "${mime}")
echo -e " ${BLUE}•${NC} ${path}"
echo -e " ${YELLOW}${file_type}${NC}"
((total_count++))
all_files_info+="${subfolder}|${mime}|${path}"$'\n'
fi
done <<< "${google_files_info}"
done
echo ""
echo "=========================================="
if [[ ${total_count} -eq 0 ]]; then
log_info "No Google-native files found."
return 1
fi
log_info "Total: ${total_count} Google-native file(s) found"
echo ""
# Store the file info for later use
GOOGLE_FILES_INFO="${all_files_info}"
GOOGLE_FILES_COUNT="${total_count}"
# Ask for confirmation
echo -n "Do you want to download and convert these files? [y/N] "
read -r response
case "${response}" in
[yY][eE][sS]|[yY])
return 0
;;
*)
log_info "Operation cancelled by user."
return 1
;;
esac
}
# Download Google-native files
download_google_files_only() {
log_info "Downloading and converting ${GOOGLE_FILES_COUNT} Google-native files..."
for subfolder in "${GDRIVE_SUBFOLDERS[@]}"; do
local folder="${GDRIVE_BASE}/${subfolder}"
local subfolder_backup="${BACKUP_DIR}/${subfolder}"
# Filter files for this subfolder
local subfolder_files
subfolder_files=$(echo "${GOOGLE_FILES_INFO}" | grep "^${subfolder}|" || true)
if [[ -z "${subfolder_files}" ]]; then
continue
fi
log_info "Processing folder: ${subfolder}"
# Create temporary file lists for rclone --files-from
local xlsx_list="${TMPDIR:-/tmp}/google-xlsx-list-$$.txt"
local other_list="${TMPDIR:-/tmp}/google-other-list-$$.txt"
local pdf_list="${TMPDIR:-/tmp}/google-pdf-list-$$.txt"
rm -f "${xlsx_list}" "${other_list}" "${pdf_list}"
# Separate files: spreadsheets → xlsx only, others → native format + pdf
echo "${subfolder_files}" | while IFS='|' read -r sf mime file_path; do
if [[ -n "${file_path}" ]]; then
if [[ "${mime}" == *"spreadsheet"* ]]; then
echo "${file_path}" >> "${xlsx_list}"
else
echo "${file_path}" >> "${other_list}"
# For PDF list, replace extension with .pdf
local base_path="${file_path%.*}"
echo "${base_path}.pdf" >> "${pdf_list}"
fi
fi
done
# Download spreadsheets as xlsx
if [[ -s "${xlsx_list}" ]]; then
local xlsx_count
xlsx_count=$(wc -l < "${xlsx_list}" | tr -d ' ')
log_info " Downloading ${xlsx_count} spreadsheet(s) as xlsx..."
if rclone copy "${RCLONE_REMOTE}:${folder}" "${subfolder_backup}" \
--files-from "${xlsx_list}" \
--drive-export-formats "xlsx" \
--drive-acknowledge-abuse \
-q \
2>>"${LOG_FILE}"; then
log_info " Spreadsheets downloaded successfully."
else
log_warn " Some spreadsheets failed to download."
fi
fi
# Download other files in native format (docx, pptx, png)
if [[ -s "${other_list}" ]]; then
local other_count
other_count=$(wc -l < "${other_list}" | tr -d ' ')
log_info " Downloading ${other_count} document(s) in native format (docx/pptx/png)..."
if rclone copy "${RCLONE_REMOTE}:${folder}" "${subfolder_backup}" \
--files-from "${other_list}" \
--drive-export-formats "docx,pptx,png" \
--drive-acknowledge-abuse \
-q \
2>>"${LOG_FILE}"; then
log_info " Native format downloads completed."
else
log_warn " Some native format downloads failed."
fi
fi
# Download as PDF (using pdf_list with .pdf extensions)
if [[ -s "${pdf_list}" ]]; then
local pdf_count
pdf_count=$(wc -l < "${pdf_list}" | tr -d ' ')
log_info " Downloading ${pdf_count} document(s) as pdf..."
if rclone copy "${RCLONE_REMOTE}:${folder}" "${subfolder_backup}" \
--files-from "${pdf_list}" \
--drive-export-formats "pdf" \
--drive-acknowledge-abuse \
-q \
2>>"${LOG_FILE}"; then
log_info " PDF downloads completed."
else
log_warn " Some PDF downloads failed."
fi
fi
# Clean up temp files
rm -f "${xlsx_list}" "${other_list}" "${pdf_list}"
done
# Count downloaded files
local downloaded_count
downloaded_count=$(find "${BACKUP_DIR}" -type f 2>/dev/null | wc -l | tr -d ' ')
log_info "Downloaded ${downloaded_count} files total."
}
# Create zip archive
create_archive() {
local timestamp
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
local zip_file="${OUTPUT_DIR}/google_files_backup_${timestamp}.zip"
# Check if there are files to archive
if [[ ! -d "${BACKUP_DIR}" ]] || [[ -z "$(ls -A "${BACKUP_DIR}" 2>/dev/null)" ]]; then
log_warn "No files to archive."
return 1
fi
log_info "Creating zip archive..."
cd "${BACKUP_DIR}"
if zip -rq "${zip_file}" . -x "*.DS_Store" 2>>"${LOG_FILE}"; then
# Count files by extension
local xlsx_count pdf_count docx_count pptx_count png_count other_count total_count
xlsx_count=$(find . -type f -name "*.xlsx" 2>/dev/null | wc -l | tr -d ' ')
pdf_count=$(find . -type f -name "*.pdf" 2>/dev/null | wc -l | tr -d ' ')
docx_count=$(find . -type f -name "*.docx" 2>/dev/null | wc -l | tr -d ' ')
pptx_count=$(find . -type f -name "*.pptx" 2>/dev/null | wc -l | tr -d ' ')
png_count=$(find . -type f -name "*.png" 2>/dev/null | wc -l | tr -d ' ')
other_count=$(find . -type f ! -name "*.xlsx" ! -name "*.pdf" ! -name "*.docx" ! -name "*.pptx" ! -name "*.png" 2>/dev/null | wc -l | tr -d ' ')
total_count=$(find . -type f 2>/dev/null | wc -l | tr -d ' ')
# Show archive size
local size
size=$(du -h "${zip_file}" | cut -f1)
echo ""
echo "=========================================="
echo " Archive Summary"
echo "=========================================="
echo ""
echo -e " ${GREEN}Archive:${NC} ${zip_file}"
echo -e " ${GREEN}Size:${NC} ${size}"
echo ""
echo -e " ${BLUE}Files by type:${NC}"
[[ ${xlsx_count} -gt 0 ]] && echo -e " • Excel (.xlsx): ${xlsx_count}"
[[ ${docx_count} -gt 0 ]] && echo -e " • Word (.docx): ${docx_count}"
[[ ${pptx_count} -gt 0 ]] && echo -e " • PowerPoint (.pptx): ${pptx_count}"
[[ ${png_count} -gt 0 ]] && echo -e " • Image (.png): ${png_count}"
[[ ${pdf_count} -gt 0 ]] && echo -e " • PDF (.pdf): ${pdf_count}"
[[ ${other_count} -gt 0 ]] && echo -e " • Other: ${other_count}"
echo -e " ${GREEN}─────────────────────${NC}"
echo -e " ${GREEN}Total: ${total_count}${NC}"
echo ""
# Log to file
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] Archive created: ${zip_file}" >> "${LOG_FILE}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] Size: ${size}, Files: ${total_count} (xlsx: ${xlsx_count}, docx: ${docx_count}, pptx: ${pptx_count}, png: ${png_count}, pdf: ${pdf_count}, other: ${other_count})" >> "${LOG_FILE}"
else
log_error "Failed to create archive"
return 1
fi
}
# Cleanup function
cleanup() {
if [[ -d "${BACKUP_DIR}" ]]; then
rm -rf "${BACKUP_DIR}"
fi
}
# Main execution
main() {
echo ""
echo "=========================================="
echo " Google Files Backup Script"
echo "=========================================="
echo ""
log_info "Log file: ${LOG_FILE}"
log_info "Output directory: ${OUTPUT_DIR}"
for subfolder in "${GDRIVE_SUBFOLDERS[@]}"; do
log_info "Source folder: ${GDRIVE_BASE}/${subfolder}"
done
echo ""
# Set trap for cleanup on exit
trap cleanup EXIT
# Check prerequisites
check_rclone
check_gdrive_folders
# List files and ask for confirmation
if ! list_and_confirm; then
exit 0
fi
echo ""
# Download Google-native files
download_google_files_only
# Create archive
if create_archive; then
echo "=========================================="
echo -e " ${GREEN}Backup Complete${NC}"
echo "=========================================="
echo ""
else
echo ""
log_warn "No files were downloaded."
fi
}
# Run main function
main "$@"