Requirements

  • Python 3
  • The pip package
  • Internet access
  • Google account with Google Drive

Get the Drive API credentials

You will need a client_secret.json file from Google. In order to get one, you will need to use this wizard. When asked to where you will be calling the API from select Other non GUI.

Download the json file and rename it client_secret.json

Set up

The script is available on my github account here.

In order to run the script, open the command line and type :

python3 ./backup_script.py  

The first time you will be prompted to accept the program to manage your Google Drive account. If you are in a command line only environnement (like a server over ssh), you will need to manually paste the code given by your web browser.

Configuration

When the script starts, a tmp folder is created and can be used to save all sorts of file. It is the default path for zip file. At the end of the script, this folder is deleted.

Backup folder

The backup script will upload all your files in a specific Google Drive repository. You have to put the id of that repository where the backup will take place.

BACKUP_FOLDER_ID = 'YOUR_FOLDER_ID'  

The name of the backup folder is a simple timestamp.

Zips

You can zip entire folders. The BACKUP_ZIPS variable is an array of tuple, the first argument being the name of the zip and the second argument the path of the folder to zip.

BACKUP_ZIPS = [  
       ('letsencrypt', '/etc/letsencrypt')
   ]

Here, the script will zip the entire folder /etc/letsencrypt and save it to pathofyourscript/tmp/letsencrypt.zip.

Commands

You can execute all sorts of command like saving your database in a file. The BACKUP_COMMANDS variable is an array of string containing the command to run in a shell. Here the command will create a file test.test in the tmp folder containing the string "test".

BACKUP_COMMANDS = [  
       ('echo test > tmp/test.test')
   ]

Backup files

You can save all sorts of files. The BACKUP_FILES variable is an array of tuple. The first argument being the path of the file and the second the Google Drive directory to create and to save to.

BACKUP_FILES = [  
      ('tmp/letsencrypt.zip', ''),
      ('tmp/test.test', 'test/test')
   ]

Output example

Script running on my server

Google Drive ouput

Feedback

Any feedback will be appreciated. This is a simple script and can be improved. I will try to keep this script up to date.