Skip to content

Python script to backup your files to Google drive

License

Notifications You must be signed in to change notification settings

BennyThadikaran/backup2gdrive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

backup2gdrive

Compress files and entire folders to a zip file and upload to Google drive. Subsequent uploads will update the same file on Google drive.

Screenshot

Initial Setup

pip install -r requirements.txt

Follow the instructions here to generate the client_secret.json. Save the file to a location of your choice.

Open the settings.yaml,

  • edit client_config_file with the file location of client_secret.json.
  • edit save_credentials_file with the file location for credentials.json. This file is automatically generated by PyDrive2 during authentication.

settings.yaml

client_config_backend: file
client_config_file: /home/john/.config/pydrive/client_secret.json
save_credentials: True
save_credentials_backend: file
save_credentials_file: /home/john/.config/pydrive/credentials.json
get_refresh_token: True

Lastly, create a text file with all the files and folders you wish to back up.

daily_backup_files.txt

/home/john/.config
/home/john/Videos
/home/john/Music
/home/john/Documents/companies.csv

Usage

backup.py takes 2 arguments:

  • file path to save zip file
  • file path to the .txt file (in this case daily_backup_files.txt)

$ python3 backup.py daily_backup.zip daily_backup_files.txt

Upon successful upload, a meta.json with details of the file upload is saved in the same folder as the zip file.

The next time, the same command is run, it looks up meta.json and updates the Google drive file.

If the json file is missing, a new file is generated on Google drive.

Ignore specific files and folders

backup.py contains a ignore_dir and ignore_file variable which is a tuple of folder and file names to ignore or exclude.

Open backup.py and edit the tuple as per requirements.

backup.py

###############################
###   USER SETTINGS BELOW   ###
###############################

# Ingore directories: .git, node_modules, python venv, cache files
ignore_dir = ('bin', 'include', 'lib', 'lib64', 'share', '__pycache__',
              '.pytest_cache', 'node_modules', '.git')

ignore_files = ('pyvenv.cfg', )

###############################
###    END USER SETTINGS    ###
###############################

Dependencies

PyDrive2

Changelog (11th Dec 2023)

  • skip iterating over ignored directories
  • replace os.path with pathlib.Path
  • updated pydrive2 dependency to version 1.18.0
  • all upload details saved to meta.json.
  • ignore variable split into ignore_dir and ignore_files
  • compress with lzma if available else fallback to bzip
    • lzma provides better compression but takes longer.
  • Handle file not found errors
    • skip file and print error to stdout