A typed interface for rclone.
Project description
pyRclone
A typed rClone interface for python.
Written to more easily express awkward logic, that would be a pain or not (easily) possible on the command line.
A short example, to tidy up and half the number of backups is shown below. The logic can be trivially expanded to instead say, parse the time stamp of the backup out of the folder name, to allow different logic to be applied depending on the age of a backup (do nothing if 1 month old, half is 2 months old, remove every other after a year old etc).
import json
import logging
import os
import sys
from pyrclone import Rclone, RcloneError
def main():
# Setup rclone runner, and have it run in verbose and dry run mode.
rclone: Rclone = Rclone()
rclone.verbose_mode = True
rclone.dry_run_mode = True
# Attach a logger, for more immediate output, rather than just on command completion.
rclone.logger.setLevel(logging.DEBUG)
handler: logging.StreamHandler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter: logging.Formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler.setFormatter(formatter)
rclone.logger.addHandler(handler)
# List all folders in backup directory to iterate over them.
remote_path: str = "drive:PC/Backups"
output = rclone.lsd(remote_path)
# If we failed to run, stop.
if output.return_code is not RcloneError.SUCCESS:
print(output.error)
return
# lsd (and all other ls commands) will default to using lsjson.
# Lets decode the JSON, and get a list of all our folders.
backup_folders = []
for folder in json.loads("".join(files)):
backup_folders.append(f"{remote_path}/{folder['Path']}")
# With our list of folders, lets delete every other folder, excluding these
# useful ones.
#
# This could also be more indepth logic, such as using the modified timestamp
# or the timestamp in the folder name to do logic to different backups of
# varying age. This is allows the ease of scripting in Python to help, versus
# confusing/verbose command line options and shell scripts.
ignore_list = [
"drive:PC/Backups/2017-09-18_WindowsUpdate",
"drive:PC/Backups/2019-07-30_FlatMove",
"drive:PC/Backups/2019-08-12_HDD-Swap",
]
# Iterate over every other folder and delete if not in the ignore.
#
# Of course, right now we are in dry_run mode, so this won't do anything
# except list what it would have done.
for folder in backup_folders[1::2]:
if folder in ignore_list:
continue
rclone.delete(folder)
if __name__ == "__main__":
main()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyrclone-1.0.1.tar.gz
(17.5 kB
view details)
Built Distribution
pyRclone-1.0.1-py3-none-any.whl
(18.1 kB
view details)
File details
Details for the file pyrclone-1.0.1.tar.gz
.
File metadata
- Download URL: pyrclone-1.0.1.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.3 CPython/3.7.3 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6baaea54d1104dba854c06b9680ce5e395068d78b937a8bb6bf4a07c028b37a |
|
MD5 | b2ac5360f68e7a21200a6deeb64b75ae |
|
BLAKE2b-256 | 80f9f50fd8e6b241705382c78a6a5159a900483a9f0b1fca5bbd95162a9f9c1b |
File details
Details for the file pyRclone-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: pyRclone-1.0.1-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.3 CPython/3.7.3 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | edea1230efb424e7d4ded9bbb52d44b6f9611a50f221fbd386533666afa591f4 |
|
MD5 | 7dc3902601c878fcdd813a7068bd6ff0 |
|
BLAKE2b-256 | 09d17c66e2b2962005f5d0bb9ee235677797e58454e758a3b300e77564282127 |