How to Back up your computer to a S3 bucket with Restic and Autorestic
Having a copy of your data is a peace of mind! Let’s focus here on how to back up your computer to a S3 bucket at (almost) no cost with an open source solutions: Restic (and Autorestic).
1. Presentation of (Auto)Restic
Restic is a cross-platform tool that backs up your files to different storage types, including online services such as S3 storages. Having some computer knowledge is required, so this technique is not available to everyone. However, the setup complexity is decent, especially if you use Autorestic.
Autorestic is a wrapper around Restic to simplify the setup and cron job part. It’s not required, but definitely appreciated.
Advantages of using such a method:
- You only pay for the external storage you use.
- It’s efficient and customizable.
- You choose your S3 provider.
- You can additionaly back up the data to an external drive at no cost.
2. The S3 provider
Choose your provider
First and foremost, you have to decide where you data is going to be stored.
At least three criteria you could consider:
- Price (storage and bandwidth)
- Stability
- Ethics and regulations
Whatever provider you choose, as long it follows S3 specs, you should be good to go.
I chose to host my data at Scaleway, where I currently work.
The pricing is quite honest, neither the highest nor the cheapest. Let’s say you have 100 GB of data to back up:
- storage: 1,46€ / month, or 17,52€ / year
- transfer: 75gb free (each month), and then 0,01€ / Go
Of course, it might not be worth for non-sensitive data. Or maybe you would be better just saving to an external drive or NAS. It’s up to you to decide if having an additional backup in a remote location is worth the money.
Create a bucket
At this point, you should create an account with the provider you have chosen, create a bucket, and create an access and secret token (ideally, scoped to this storage project) allowing you access to it. It means you have in your possession:
- An access token
- A secret token
- The URL of your bucket (including the region and the bucket name)
3. Install and setup
The installation depends on your OS. If you have a Mac, you could use Homebrew as package manager and run brew install restic autorestic
; else, please refer to their documentation.
Write the configuration file
Let’s get to the point, and directly create the configuration file for autorestic called .autorestic.yml
located in your home directory (so at ~/.autorestic.yml
on Mac).
Here, I plan to save two folders in one S3:
- My
~/Documents
folder where I keep the important stuff. - My Notes app folder (in case iCloud issues).
|
|
Notes:
- The locations are places that you want to back up. The backends are places where the backup will be achieved.
- The S3 path should include the bucket name at the end, and NOT as a subdomain.
- You could move the secrets out of this file, either in a environment file, or in environment variables.
Initialize the S3 bucket
The configuration file has been edited to your needs and is at the proper location? GOOD.
But the S3 bucket should still be empty at this point. Launching a backup won’t work at the moment, it requires a one-time initialization:
|
|
- The
exec
command launches the restic exec. - The
-a
option tells autorestic to apply the command for all the backends. - The
init
command tells restic exec to initialize your backend.
If everything went fine, check the content of your S3 bucket, it shouldn’t be empty anymore.
If so, launch your first backup with the following command:
|
|
- The
backup
command speaks for itself. - The
-a
option tells autorestic to apply the command for all the backends.
Then you just wait! It shouldn’t be that long, except if you have terabytes or data.
Setup a cronjob
Finally, we have to setup a cronjob so Autorestic will launch the backup at the proper time.
For that, just open your jobs with crontab -e
, and add a new line:
|
|
/opt/homebrew/bin/autorestic
defines the path to the autorestic binary, change it if you didn’t use Homebrew.>> /tmp/autorestic.log 2>&1
appends the logs into a file, useful to debug!- Change the cronjob to something that fits your needs. So
*/5
would be fine to debug.- The regex should be consistent with what your
.autorestic.yml
contains.
- The regex should be consistent with what your
Restore a backup
Let’s hope you won’t need to restore files anytime soon! But just in case, let’s learn how to restore all the files from a bucket (consider the transfer pricing before doing so!).
The command line is straighforward:
|
|
- The
restore
command speaks for itself. - The
--location notes
option specifies the location we want to restore. - The
--to ~/Desktop/my-saved-notes
option specifies the path to restore the data.
You can also restore a specific snapshot (i.e. not just the latest one). First, you need to list the snapshots:
|
|
- The
--verbose
is mandatory for an obvious reason (bug?), else the snapshots don’t display.
You should have a table similar to this one:
|
|
Then, you can use the ID in the restore command:
|
|
4. Final words
Open source exists thanks to people contributing to it! So if (Auto)Restic has been helpful to you, consider eihter checking their GitHub projects to help, or donate if accepted.
Author Vinzius
LastMod 30 November, 2023