restic-systemd-automatic-ba.../README.md

102 lines
3.4 KiB
Markdown
Raw Normal View History

# Automatic restic backups using systemd services and timers
## Restic
[restic](https://restic.net/) is a command-line tool for making backups, the right way. Check the official website for a feature explanation. As a storage backend, I recommend [Backblaze B2](https://www.backblaze.com/b2/cloud-storage.html) as restic works well with it, and it is (at the time of writing) very affordable for the hobbyist hacker!
Unfortunately restic does not come pre-configured with a way to run automated backups, say every day. However it's possible to set this up yourself using systemd/cron and some wrappers. This example also features email notifications when a backup fails to complete.
Here follows a step-by step tutorial on how to set it up, with my sample script and configurations that you can modify to suit your needs.
Note, you can use any of the supported [storage backends](https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html). The setup should be similar but you will have to use other configuration variables to match your backend of choice.
## Set up
Tip: The steps in this section will instruct you to copy files from this repo to system directories. If you don't want to do this manually, you can use the Makefile:
```bash
$ git clone https://github.com/erikw/restic-systemd-automatic-backup.git
$ cd restic-systemd-automatic-backup
$ sudo make install
2019-01-07 21:12:01 +00:00
```
2019-01-07 21:11:11 +00:00
### 1. Configure the Backup
2019-01-07 21:11:11 +00:00
First edit the following Files as you like
2019-01-07 21:11:11 +00:00
* restic-backup.service
* Add your Username
* restic-check.service
* Add your Username
* env.sh
* pw.txt
* password for your Backup
2019-01-07 21:11:11 +00:00
After that you can copy all files to the right directories with the Makefile in this Repository
```bash
2019-01-07 21:11:11 +00:00
$ sudo make install
2019-01-07 21:12:34 +00:00
```
2019-01-07 21:11:11 +00:00
### 2. Initialize your Backup
2019-01-07 21:11:11 +00:00
See https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html for how to do this
2019-01-07 21:11:11 +00:00
### 3. Make first backup & verify
Now see if the backup itself works, by running
```bash
$ /usr/local/sbin/restic_backup.sh
$ restic snapshots
2019-01-07 21:12:34 +00:00
```
2019-01-07 21:11:11 +00:00
### 4. Backup automatically; systemd service + timer
Now we can do the modern version of a cron-job, a systemd service + timer, to run the backup every week!
Now simply enable the timer with:
```bash
$ systemctl start restic-backup.timer
$ systemctl enable restic-backup.timer
2019-01-07 21:12:34 +00:00
```
You can see when your next backup is scheduled to run with
```bash
$ systemctl list-timers | grep restic
```
and see the status of a currently running backup with
```bash
$ systemctl status restic-backup
```
or start a backup manually
```bash
$ systemctl start restic-backup
```
You can follow the backup stdout output live as backup is running with:
```bash
$ journalctl -f -u restic-backup.service
2019-01-07 21:12:34 +00:00
```
(skip `-f` to see all backups that has run)
2019-01-07 21:11:11 +00:00
### 5. Optional: automated backup checks
Once in a while it can be good to do a health check of the remote repository, to make sure it's not getting corrupt. This can be done with `$ restic check`.
There are some `*-check*`-files in this git repo. Install these in the same way you installed the `*-backup*`-files.
## Cron?
If you want to run an all-classic cron job instead, do like this:
* `etc/cron.d/restic`: Depending on your system's cron, put this in `/etc/cron.d/` or similar, or copy the contents to $(sudo crontab -e). The format of this file is tested under FreeBSD, and might need adaptions depending on your cron.
* `usr/local/sbin/cron_mail`: A wrapper for running cron jobs, that sends output of the job as an email using the mail(1) command.