Compress files and entire folders to a zip file and upload to Google drive. Subsequent uploads will update the same file on Google drive.
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 ofclient_secret.json
. - edit
save_credentials_file
with the file location forcredentials.json
. This file is automatically generated byPyDrive2
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
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.
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 ###
###############################
- 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 intoignore_dir
andignore_files
- compress with
lzma
if available else fallback tobzip
- lzma provides better compression but takes longer.
- Handle file not found errors
- skip file and print error to stdout