Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
/ backup-to-cloud Public archive

Python app to manage backups using google drive

License

Notifications You must be signed in to change notification settings

sralloza/backup-to-cloud

Repository files navigation

backup-to-cloud: the google drive backup manager

Actions Status Actions Status License: GPLv3 Code style: black

Simple app designed to upload backups to google drive. Settings are handled by a yml file and it's explained here. Before using the app, you need to get some google drive credentials, expained here.

Contents:

Enviroment settings

In order to use the library you must set the enviroment variable BTC_ROOT_PATH to a existing folder. Said folder must contain the credentials in the file credentials.json. The logs and token will be stored in that folder.

Settings

Settings must be placed in .automatic.yml, written in YAML, in the root dir.

Each entry must be declared like this:

name:
  type: <type>
  root-path: /path/to/file-or-folder
  cloud_folder_id: <folder_id>
  zip: true
  zipname: <zipname.zip>
  filter: <filter>

Notes:

  • name, type and root-path are required.
  • zip only affects behaviour if type is multiple-files.
  • zipname only affects behaviour if type is multiple-files and zip is true.
  • filter only affects behaviour if type is multiple-files.

Explanation:

  • name: the name of the entry. It is irrelevant, only representative.
  • type: the entry type. Right now it can be single-file or multiple-files.
  • root-path: if type is single-file, it represents the path of the file. If type is multiple-files, it represents the root folder where the system will start listing files.
  • zip: only used if the type is multiple-files. If True, the files will be zipped and uploaded as a single file, rather than multiple files.
  • zipname: only used if type is multiple-files and zip is True. In that case, it must be provided. It sets the zip name to upload to google drive. Note that as it is a zip file, the extension should be zip.
  • cloud_folder_id: id of the folder to save the file(s) into. If is not present or is root, the files will be stored in the root folder (Drive). More info for folder's id here.
  • filter: if the type is multiple-files, this regex filter will be applied to every file located below root-path. The search it's recursively. For example, to select all pdf files, use filter=.py. By default is '.', which is a regex for match anything. It is encouraged to check the regex before creating the first backup. To check the regex read this. If all you want to do is just filter files by extension, read this. To write advanced filters, try this web.

Examples

Back up apache configuration: saves all files with conf extension and saves them as a zip with name sites-available.zip, in a specific folder.

apache-config:
  type: multiple-files
  root-path: /etc/apache2/sites-available
  zip: true
  zipname: sites-available.zip
  cloud_folder_id: <folder_id>

Back up a specific file.

specific-file:
  type: single-file
  path: /home/user/data.db
  cloud_folder_id: <folder_id>

Check regex

You can test if the regex matches the files you want to back up by using the command check-regex:

python launcher.py check-regex "<root-path>" "<regex>"

If root-path contains spaces, quotes ("/path with/spaces") must be used.

Common filters

One of the usages of the regex filter is filter by extension. In order to do so, write filter=ext$ (where ext is the extension) in the automatic file. The dollar symbol ($) means the end of the line. Without it, a file named /folder/something.ext/invalid.pdf would match the filter.

Get folder's id

When opening the folder in the browser, the URL will look like https://drive.google.com/drive/u/3/folders/<folder-id>. The folder's id appears at the end of the URL. It has letters (lowercase and uppercase), numbers and hyphens.

Credentials

In order to use this app, you need to get some credentials from the google drive api. If you don't have any projects created, create one in this link. After having a project, you need to generate credentials to talk to the API. To do so, visit this link. After creating the credentials, download them.

download credentials When you press the download icon (as shown in the picture), a json file will be downloaded. Rename it to credentials.json and place it in the root folder (next to this file).

The file credentials.json is used to create the token, which the app uses to upload files into google drive. To create the token, run

python launcher.py gen-token

And that's all. You don't need to care about loosing data anymore!

Other uses

You can also use this library to upload a file to google drive:

from backup_to_cloud.upload import backup


file_data = "drive_document.pdf"
folder_id = "kb0T7lBnqADunAhsjy2Txhs9Qjoe1zmUu"
mimetype = "application/pdf"
filename = "real_document.pdf"

response = backup(file_data, mimetype, folder_id, filename)
print(response)
# {'kind': 'drive#file', 'id': '1QMFjfQdy_2defFdJM5vojuAjXbfs6qp51', 'name': 'real_document.pdf', 'mimeType': 'application/pdf'}