Resticpy: A minimal Python API for Restic

I’ve just released resticpy, a minimal Python wrapper for the restic CLI.

I know some other authors have created Python wrappers, but those solutions all act as wrapper scripts rather than expose an API. I wanted a solution that provided a nice API so that a developer could decide how they wanted to use restic, so that’s what resticpy is.

It’s easy to get started with resticpy:

printf "mysecretpass" > password.txt
pip install resticpy
import restic

restic.repository = '/tmp/backup1'
restic.password_file = 'password.txt'

# Initialize a repo
restic.init()

# Back up a file
restic.backup(paths=['some-file.txt'])

# Restore the backup to a new location
restic.restore(snapshot_id='latest', target_dir='~/restored')

Some features:

  • The API is fully documented with plenty of example code
  • The API semantics closely mirror the CLI semantics, so they should be intuitive to anyone familiar with restic
  • The code is thoroughly unit tested (95% coverage)
  • The code includes continuous integration that automatically exercises the code through the unit tests and end-to-end tests against the real restic binaries
  • The code is open-source, licensed under the permissive MIT license

I’m currently using resticpy for my daily backup script, and I hope it can be useful to others. I’m happy to answer any questions or take any feedback.

3 Likes

I built a small GUI with PyQT5 and use the following for my backup as an example

process = subprocess.run(['restic','-r',backups...

My programming skills are on beginner level. What is the advantage of your solution?l

Thanks for checking it out!

What is the advantage of your solution?l

If you’re just calling restic backup once in your script, it’s probably simpler to use subprocess.run.

Resticpy becomes a little more handy when you start calling multiple restic commands and want to read restic’s output.

For example, my daily backup script call restic backup, restic prune, and restic stats. Instead of a lot of ugly subprocess calls where I’m translating Python data to and from the command-line, I can write with clean Python APIs, like this:

result = restic.backup(paths=['~/photos', '~/documents'],
                       exclude_patterns=['junk*'])
print('Backup result: %s' % result)
restic.forget(prune=True, keep_daily=30)
stats = restic.stats(mode='files-by-contents')
print('Backup stats: %s' % stats)
1 Like

I will try it. Thanks!

thanks for the awesome information.