Sunday, February 17, 2019

Google Drive Offline Backup with Raspberry Pi

So, after retiring my Mac Mini earlier this year, I lost my ability to keep an offline backup of my Google drive files. I recently realized that I could put something together with my Raspberry Pi. Here is what I cobbled together.

I updated to the latest version of Raspian and installed the following with apt-get.

ntfs-ng
ssmtp
mailutils

I also spent the $5 on OverGrive for Linux to sync Google Drive to my Pi. They have very good instructions and I didn't have any trouble with that part. It took a couple hours to sync up but works well after the initial download.

Then I plugged in my USB drive and it took a few tweaks to get it working right and writable after a reboot. I followed this page but didn't have to add the delay part at the end.

http://posts.danharper.me/raspberry-pi-2-auto-mount-usb/

Basically, find the block ID of the USB drive.

sudo blkid

# /dev/sda1: LABEL="ELEMENTS" UUID="E033-1109" TYPE="vfat"
Create a new mount point.

sudo mkdir /mnt/usbel  
Take ownership.

sudo chown -R pi:pi /mnt/usbel  
Add the appropriate line to the fstab (update with your blockid and mount point). I installed 'ntfs-ng' for my USB drive and changed 'vfat' to 'ntfs' as well.

UUID=E033-1109 /mnt/usbel vfat auto,users,rw,uid=1000,gid=100,umask=0002 0 0  
Test it with a 'mount -a', adjust accordingly, reboot for a test.

I installed 'ssmtp' and 'mailutils' as well to send me a report of what gets updated. Installing these was a bit tricky with Google authentication. You will just have to search for the answers as I am sure they will change and might be different depending on your Google account. I had to enable two factor auth and generate an App Password for the outgoing mail to work. You can create a special email account for this just to send email if you don't want to mess with your default gmail account.

Next, add the rsync command to your crontab. It should look something like this.

0 5 * * * /usr/bin/rsync -avh --delete /home/pi/Google\ Drive/ /mnt/usb/GoogleBackup | mail -s "Google Drive Rsync Report" user@domain.com

It might be a good idea to manually test the rsync command and complete a copy first to avoid an errors that might take place.

I have other versioning systems in place for the live copy of my Google Drive (aside from the archives and recycle bin provided by Google). This one give me the peace of mind of having one on a USB drive I could grab in an emergency. Not that grabbing the entire Raspberry Pi would be much different. I guess this was also just fun to do.