Restic Python Backup to S3 Compatible Storage

Restic Python Backup to S3 Compatible Storage

Posted:  April 17, 2021

Restic Python Backup to S3 Compatible Storage

Forward

In my quest to get my servers fully automated, I realized I needed a reliable way to backup and restore my sites and databases.  Including a way to either browse the backups, or restore them to any other machine I have access to.

At first I thought… mmm…  duplicity seems nice, and while generally it is a viable solution, it did not quite have the OOOMPH I was looking for in my backups, so, in steps Restic, and Python.

This post is going to be mostly instructions on how to use my backup/restore binary’s and scripts, so if you need some more information about restic, please see here: https://restic.net/

Some of the backups are multi-threaded.

Description & Requirements

So, first things first, you are going to need a S3 compatible cloud object storage provider.  Something like Amazon’s S3 ( https://aws.amazon.com/s3/ ) will suffice, however, I would highly recommend giving Wasabi ( https://wasabi.com/ ) a chance (you won’t be disappointed)

Once you are signed up, you will need to gather some info to configure your backups.

  • Your S3 API Key
  • Your S3 API Secret
  • Your S3 API Endpoint (just the FQDN)
  • Your S3 Region
  • Your S3 Bucket

Got all that?   Great, don’t forget that I have set this up for my web servers, so you should probably gather up your MySQL admin info (either username and password, or the defaults file path), the starting path to your servers applications, as well, the name you wish to use for these backups and the “hash” you want to use to encrypt the backups.

Also note, this is created for Ubuntu based web servers primarily, so make sure your server is at least Ubuntu 18.04.  You also need to make sure your server has at least Python 3.6 installed.

Installing & Updating

At this point, you can go ahead and download the binary.  Yes, I did compile it to be a single binary, however, I did have to compile it separately for Ubuntu 18.04 and Ubuntu 20.04, so make sure you grab the right one for your distro.

Once you have it downloaded to your server, change the permissions on it to make it executable, rename it to kpbr, and move it to your /usr/bin/ directory.    This will make it that much easier to run it, especially when you need it for a cron job 🙂

Now, let’s get everything setup and ready to use.  Run the following command and pay attention.   kpbr -a setup  (NOTE: you’ll be doing this when I update the app too…   same download link, same command…).  It will get your server ready with the required modules and software to run.  (I’ll list that all out below…)

Once the setup is completed, it will automatically ask you to configure the app.  Just follow the prompts and answer the questions to finish… please do note, the hashes, keys & secrets are indeed secret, so you won’t be seeing them on the screen as you are typing or copying/pasting the information.

Backing Up and Restoring

Now that you are configured you can run your first backup.  Simply run the command: kpbr -a backup --what all to backup all web applications and databases on your server.

Now, if you need to restore an app or database, you have a few options.   You can do an automated restore, you can restore to a different location, or you can even mount the backups as a network drive and browse the files, you can also restore a backup from another machine if you want.  Just run the following command: kpbr -a restore and pay attention, you will be prompted to select what exactly it is you want to do.

Now for the nitty gritty of it:  I have the backups tested on both flavors of Ubuntu Server and Desktop, as well in the equivalent versions in WSL on Windows 10.   I also have the restores tested on the above as well.   I have tested the restores on Mac OS, however, have found there are too many differences in the Python versions and the modules for it to be able to support it (please I do not readily have access to a Mac), however, I have setup a bash script to allow you to be able to mount the backups as a network drive on a Mac.

The full readme, and command set is below, along with the repository of the source code.   If you do have any issues with this, please feel free to file an issue at the repo, just make sure you give me as much detail as possible.  I’ll at least need to know what OS you were using, along with what command you were running.

REQUIREMENTS

Operating System

    • Ubuntu 18.04+
    • Windows 10
      • Works and tested in WSL Ubuntu 18.04 & Ubuntu 20.04
      • Minor visual glitch with the select menus that go away if you go up and down once
      • UNTESTED in CYGWIN

Server Software & Binaries

    • NOTE: Do not run kpbr -a setup on Mac or Windows, instead please look up the proper documentation to install the proceeding packages for your system.
    • Python3.6+
      • Python dateutil, boto3, enquiries, mysqldb, invoke, & requests modules
      • for backing up & restore
    • restic
      • for backing up & restore
      • for restore/mount script
    • aws-cli
      • for restore selection
      • for restore/mount script
    • jq
      • for restore/mount script

INSTALL / UPDATE

For ubuntu, download the release binary associated to your flavor from here:

rename it to kpbr, make it executable, and move it to /usr/bin/

Once there, run sudo kpbr -a setup, it will install or update everything your server will need to run.

    • All in one command – Ubuntu 18.04: sudo wget https://kevinpirnie.com/kp/kpbr-18.04 -O kpbr && sudo chmod +x kpbr && sudo mv kpbr /usr/bin/ && sudo kpbr -a setup
    • All in one command – Ubuntu 20.04: sudo wget https://kevinpirnie.com/kp/kpbr-20.04 -O kpbr && sudo chmod +x kpbr && sudo mv kpbr /usr/bin/ && sudo kpbr -a setup

BACKUP MOUNT

Download and unzip the mount-scripts.zip from here: https://kevinpirnie.com/kp/mount-scripts.zip. You may need to make the 2 files executable.

Once expanded open .mb-source.sh and configure it. For the BACKUP_REPOS, it is a JSON string array. Follow the example to create more nodes.

USAGE – kpbr

    kpbr -a [setup|config|backup] [ARGS]

    Required Arguments Per Action:

        setup:
            NONE
                Will prompt you to configure or re-configure once the setup has been completed.

        config:
            NONE
                Will ask you questions to configure the app.  Please pay attention carefully.

        backup:
            --what [all|db|database|acct|account|app|other]
                all: 
                    NONE - also performs a full database backup
                db/database:
                    --db or --database THE DATABASE NAME | ALL - REQUIRED 
                acct/account:
                    --acct or --account THE ACCOUNT NAME - REQUIRED 
                app:
                    --acct or --account THE ACCOUNT NAME - REQUIRED 
                    --app THE APP NAME - REQUIRED 
                other:
                    --paths COMMA-DELIMITED STRING OF FULL PATHS - REQUIRED 
                NOTE:
                    --paths can also be passed for all|db/database|acct/account|app as well to add additional paths to backup

        restore:
            NONE: Everything will be prompted for you.
            PROMPTS:
                Backup Location:
                    Local: this is the local server's backup.  It will pull everything needed from the configured config file
                    Remote: this is a remote servers backup.  You will have to enter the S3 API Key, S3 API Secret, S3 Endpoint, S3 Bucket, S3 Region, backup name/server, and the backup hash.   Make sure you have all this information handy, it is all required.
                Type of Backup:
                    Application: Otherwise known as the web application, set of files to be restored.
                        Addition Prompts:
                            Account: The account to be restored
                            Application: The application in the chosen account to be restored
                            Snapshot: The specific backup point you would like to restore.
                    Database: The database to be restored
                        Addition Prompts:
                            Database: The account to be restored
                            Snapshot: The specific backup point you would like to restore.
                Restore Method:
                    Automatic: This will overwrite the originals, so make sure you really want to do this.
                    Manual: This will restore the files to the specified path.  This includes the mysqldump if Database has been chosen as the type.
                        Addition Prompts:
                            Restore Path: The files will be restored to this path and be left alone for you to do with them as you see fit. The default is `/tmp/restore`
                    Browse: This will mount the backup as a network drive, allowing you to browse the files as you see fit.  The default mount point is `/tmp/backup-mount` however you can specify whatever path you see fit.
                        Addition Prompts:
                            Mount Point: The mount point that the backup will be mounted to, allowing you to browse the files as you see fit.  The default mount point is `/tmp/backup-mount`

USAGE – MOUNT SCRIPTS

You can mount a backup as a networked drive.

Configure by opening .mb-source.sh and entering your AWS Key and Secret, the S3 Bucket, S3 Region, and S3 Endpoint (for aws this is s3.amazonaws.com).

Then add your server and server hash to the json nodes. name = the server or backup name, hash = the server hash

Once configured run the script mount-backup.sh then browse to /tmp/backup-mount/ on your machine. Inside it will contain the backups you selected. snapshots is the timestamped snapshots of the specified backup, with a symlink to the latest

REPO

https://gitlab.com/kp-development/python/kp-backup

Feel free to fork this, and if you run into any issues with it please fire up and issue at the repo so I can track it.

Thanks for looking and Happy Hosting!

Kevin Pirnie

20+ Years of PC and server maintenance & over 15+ years of web development/design experience; you can rest assured that I take every measure possible to ensure your computers are running to their peak potentials. I treat them as if they were mine, and I am quite a stickler about keeping my machines up to date and optimized to run as well as they can.