Monday, December 16, 2024

Daily Google Drive backup using rclone

I still make regular use of Google's Docs, Sheets, and so on but make regular backups to a local drive which is itself backed up off-site. I've settled on the following script, run from cron in the middle of the night. It sends email on failure, and reports success on Mondays just so I'll see that it is working. It is saved as /usr/local/bin/gdrive_sync.sh.

root@pve:~# cat /usr/local/bin/gdrive_sync.sh
#!/bin/sh

tmpfile=$(mktemp)
/usr/bin/rclone sync --fast-list --transfers=32 --create-empty-src-dirs \
	--exclude-if-present=exclude-from-rclone-sync \
	--drive-acknowledge-abuse \
	denny-at-example-com-google-drive-backup: \
	/pool1/GDrive/denny@example.com > ${tmpfile} 2>&1

if [ $? -ne 0 ]; then
	echo rclone Failed
	rm -f ${tmpfile}
	exit 1
fi

extra=$(grep -v -e "Duplicate object found in source" \
        -e "Duplicate directory found in source" ${tmpfile})
rm -f ${tmpfile}

if [ "${extra}" ]; then
	echo rclone errors found
	echo ${extra}
	exit 1
fi

dow=$(date +%w)
if [ ${dow} -eq 1 ]; then
	# Report success once per week
	echo Successfully backed up denny@example.com Google Drive
fi

exit 0