Automate File Backups to Google Drive Using Python

Hey everyone! I’m trying to set up an automated backup system for my files to Google Drive. Here’s what I’ve got so far:

  1. I wrote a Python script to copy files:
import os
from pathlib import Path
import shutil

source = Path('/home/user/documents')
destination = Path('/home/user/GoogleDrive/Backups')

for file in source.glob('*.txt'):
    if file.is_file():
        shutil.copy2(file, destination)
  1. I’m planning to turn this into an app with py2app.
  2. Then use Automator to run it regularly.

But I’m running into a snag. When I copy files to the Google Drive folder, they’re getting ‘blacklisted’ somehow. Not sure what’s going on there.

Is this a good way to do backups? Or am I barking up the wrong tree? Any tips or better methods would be awesome. Thanks!

hey TomDream42, that’s a cool idea! i’ve had similar issues with google drive. have u tried using the google drive API instead? it’s a bit more complex but gives u more control. Plus, u won’t have to worry about files being ‘blacklisted’. just a thought, good luck with ur project!

I’ve been down this road before, and I can tell you that using the Google Drive API is definitely the way to go. It might seem daunting at first, but it’s actually not too bad once you get the hang of it. Plus, it gives you way more flexibility than just copying files to a local folder.

Here’s what I’d suggest: Look into the google-auth and google-auth-oauthlib libraries for authentication, and then use google-api-python-client to interact with Drive. You can set up a service account for automated backups, which eliminates the need for user intervention.

One thing to keep in mind is rate limits. Make sure you’re not hammering the API too hard, especially if you’re backing up a lot of files. Implement some kind of throttling mechanism to avoid getting temporarily blocked.

Also, consider encrypting sensitive files before uploading. Better safe than sorry when it comes to cloud storage.

While your approach is creative, it might not be the most reliable for long-term backups. Google Drive’s local sync can be finicky, leading to those ‘blacklisted’ issues you’re experiencing. Have you considered using a dedicated backup solution like Duplicati or rclone? These tools are designed specifically for cloud backups and offer features like encryption, deduplication, and incremental backups. They also handle Google Drive’s quirks much better than a custom script. If you’re set on using Python, look into the official Google Drive API as others have suggested. It’s more robust and gives you direct control over file uploads and management.