39 Commits

Author SHA1 Message Date
Erik Westrup
d01d2995f5 Update CHANGELOG.md 2021-12-03 18:40:24 +01:00
Erik Westrup
97a9475550 Update CHANGELOG.md 2021-12-03 18:35:51 +01:00
Erik Westrup
2e55d1cfef Modified: CHANGELOG.md 2021-12-03 18:35:07 +01:00
Erik Westrup
7a8861adc4 Merge pull request #41 from erikw/fix/40
Fix installation of template files
2021-12-03 18:32:26 +01:00
Erik Westrup
bf9b29575a Update README.md 2021-12-03 18:16:39 +01:00
Erik Westrup
428f4be872 Fix installation of template files
The target that copies the *.template files (#15) had a '/' prefixed which
should not be there. The copy should be locally here. The real install
happens in the install-conf that respects $PREFIX

Fixes #40
2021-12-03 17:09:14 +01:00
Erik Westrup
99f4ba4436 Add AUR maintainer badge 2021-12-02 15:31:33 +01:00
Erik Westrup
ae2b122134 Add AUR instructions 2021-12-02 15:28:40 +01:00
Erik Westrup
8e1853458c Add AUR badge 2021-12-02 15:26:55 +01:00
Erik Westrup
956ebfb29b Added: CHANGELOG.md 2021-12-02 15:23:48 +01:00
Erik Westrup
d34fbfc9d3 Modified: README.md 2021-12-01 18:07:12 +01:00
Erik Westrup
213fb95bdc Modified: README.md 2021-12-01 18:05:19 +01:00
Erik Westrup
ae228186eb Add badges 2021-12-01 18:02:53 +01:00
Erik Westrup
3460e9accf Full path in .gitignore 2021-10-30 13:52:27 +02:00
Erik Westrup
6689b1508f make uninstall target respect $PREFIX 2021-10-30 13:49:07 +02:00
Erik Westrup
77c095c12a Consistent code markup for console 2021-10-30 13:37:39 +02:00
Erik Westrup
47ac748a27 Merge pull request #39 from gerardbosch/doc/update-readme-uninstall
Update README: Add uninstall instructions
2021-10-30 13:36:44 +02:00
Gerard Bosch
28dece45ae Update README: Add uninstall instructions 2021-10-30 13:29:48 +02:00
Erik Westrup
d9459cb048 Fix b2_* install path 2021-10-30 09:16:12 +02:00
Erik Westrup
b8716ad377 Indent fix 2021-10-30 09:15:49 +02:00
Erik Westrup
b261235ba5 Simpler patsubst for installed files 2021-10-30 09:15:36 +02:00
Erik Westrup
88f2dc9eca Makefile: s/DEST_SCRIPT\b/DEST_SCRIPTS/ 2021-10-30 09:15:09 +02:00
Erik Westrup
16e3cb2df5 Makefile: remove out-commented line 2021-10-30 09:00:47 +02:00
Erik Westrup
9e0735ac01 Merge pull request #38 from erikw/fix/35
Add restic version requirements to README.md
2021-10-29 17:17:03 +02:00
Erik Westrup
6e24a66a1c Add restic version requirements to README.md
Fixes #32
2021-10-29 17:16:16 +02:00
Erik Westrup
265ff699f8 Merge pull request #37 from erikw/fix/30
Makefile: add uninstall target
2021-10-29 17:10:41 +02:00
Erik Westrup
bcd4a02e82 Makefile: add uninstall target
Fixes #30
2021-10-29 17:09:49 +02:00
Erik Westrup
e5502b0655 s/``bash/``console/ in README 2021-07-11 18:28:08 +02:00
Erik Westrup
be6ab0c37a Merge pull request #27 from wvolz/master
Fix conflicts for restic-check service
2020-06-02 07:41:49 +02:00
Warren Volz
c7d702a1ed Fix conflicts for restic-check service 2020-06-01 18:04:41 -06:00
Erik Westrup
fa220b3a91 Merge pull request #26 from shwang/patch-2
README: Fix backup_exclude destination
2020-01-22 08:59:38 +01:00
Steven H. Wang
e943e94f05 README: Fix backup_exclude destination
restic_backup.sh doesn't actually look for `/.backup_exclude` it seems
2020-01-21 16:46:55 -08:00
Erik Westrup
71f1bd8e61 Fix #25: correct b2_password.txt 2020-01-20 20:47:52 +01:00
Erik Westrup
2629c77192 Merge pull request #23 from tbm/typo
Fix typo in script
2019-09-23 13:24:44 +02:00
Martin Michlmayr
4bb30eee90 Fix typo in script 2019-09-23 10:45:38 +02:00
Erik Westrup
ac678acb46 Merge pull request #22 from toddejohnson/forget-prune
Forget now has --prune which might be more efficent.
2019-08-03 16:48:47 +02:00
Todd E Johnson
429ee40220 Add b2 connection limit to forget now that it prunes. 2019-07-31 22:59:03 -05:00
Todd E Johnson
be5c07bd0f Update comment describing restic-forget to note prune 2019-07-31 22:56:24 -05:00
Todd E Johnson
197b4b7064 Forget now has --prune which might be more efficent. 2019-07-25 22:51:07 -05:00
8 changed files with 111 additions and 47 deletions

5
.gitignore vendored
View File

@@ -1,2 +1,3 @@
b2_env.sh
b2_pw.txt
# Prevent check-in of these sensitive files. Instead they are generated from the corresponding *.template file.
/etc/restic/b2_pw.txt
/etc/restic/b2_env.sh

26
CHANGELOG.md Normal file
View File

@@ -0,0 +1,26 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## [1.0.1] - 2021-12-03
### Fixed
- $(make install) now works for the *.template files ([#40](https://github.com/erikw/restic-systemd-automatic-backup/issues/40))
## [1.0.0] - 2021-12-02
It's time to call this a proper major version!
### Added
- `uninstall` target for `Makefile`
- Add `--prune` to `restic-forget`
- README badges and updates.
### Fixed
- `backup_exclude` destination
- Conflicts for restic-check service
## [0.1.0] - 2019-07-23
- First tagged version.

View File

@@ -1,9 +1,9 @@
# Not file targets.
.PHONY: help install install-scripts install-conf install-systemd
.PHONY: help install install-scripts install-conf install-systemd uninstall
### Macros ###
SRCS_SCRIPTS = $(filter-out %cron_mail, $(wildcard usr/local/sbin/*))
SRCS_CONF = $(filter-out %template, $(wildcard etc/restic/*))
SRCS_CONF = $(patsubst %.template, %, $(wildcard etc/restic/*))
SRCS_SYSTEMD = $(wildcard etc/systemd/system/*)
# Just set PREFIX in envionment, like
@@ -12,6 +12,8 @@ DEST_SCRIPTS = $(PREFIX)/usr/local/sbin
DEST_CONF = $(PREFIX)/etc/restic
DEST_SYSTEMD = $(PREFIX)/etc/systemd/system
INSTALLED_FILES = $(addprefix $(PREFIX)/, $(SRCS_SCRIPTS) $(SRCS_CONF) $(SRCS_SYSTEMD)) \
$(DEST_CONF)/b2_env.sh $(DEST_CONF)/b2_pw.txt
### Targets ###
# target: all - Default target.
@@ -30,15 +32,15 @@ install-scripts:
install -d $(DEST_SCRIPTS)
install -m 0744 $(SRCS_SCRIPTS) $(DEST_SCRIPTS)
etc/restic/b2_env.sh:
install -m 0600 etc/restic/b2_env.sh.template etc/restic/b2_env.sh
etc/restic/b2_pw.txt:
install -m 0600 etc/restic/b2_pw.txt.template etc/restic/b2_pw.txt
# Copy templates to new files with restricted permissions.
# Why? Because the non-template files are git-ignored to preovent that someone who clones or forks this repo checks in their sensitive data like the B2 password!
etc/restic/b2_env.sh etc/restic/b2_pw.txt:
install -m 0600 $@.template $@
# target: install-conf - Install restic configuration files.
# will create these files locally only if they don't already exist
install-conf: | etc/restic/b2_env.sh etc/restic/b2_pw.txt
# | means that dependencies are order-ony i.e. only created if they don't already exist.
install-conf: | $(SRCS_CONF)
install -d $(DEST_CONF)
install -m 0600 $(SRCS_CONF) $(DEST_CONF)
@@ -46,3 +48,10 @@ install-conf: | etc/restic/b2_env.sh etc/restic/b2_pw.txt
install-systemd:
install -d $(DEST_SYSTEMD)
install -m 0644 $(SRCS_SYSTEMD) $(DEST_SYSTEMD)
# target: uninstall - Uninstall files from the install targets
uninstall:
@for file in $(INSTALLED_FILES); do \
echo $(RM) $$file; \
$(RM) $$file; \
done

1
OSSMETADATA Normal file
View File

@@ -0,0 +1 @@
osslifecycle=active

View File

@@ -1,6 +1,24 @@
# Automatic restic backups using systemd services and timers
[![GitHub Stars](https://img.shields.io/github/stars/erikw/restic-systemd-automatic-backup?style=social)](#)
[![GitHub Forks](https://img.shields.io/github/forks/erikw/restic-systemd-automatic-backup?style=social)](#)
<br>
[![AUR version](https://img.shields.io/aur/version/restic-systemd-automatic-backup)](https://aur.archlinux.org/packages/restic-systemd-automatic-backup/)
[![AUR maintainer](https://img.shields.io/aur/maintainer/restic-systemd-automatic-backup?label=AUR%20maintainer)](https://aur.archlinux.org/packages/restic-systemd-automatic-backup/)
[![SLOC](https://img.shields.io/tokei/lines/github/erikw/restic-systemd-automatic-backup)](#)
[![Open issues](https://img.shields.io/github/issues/erikw/restic-systemd-automatic-backup)](https://github.com/erikw/restic-systemd-automatic-backup/issues)
[![Closed issues](https://img.shields.io/github/issues-closed/erikw/restic-systemd-automatic-backup?color=success)](https://github.com/erikw/restic-systemd-automatic-backup/issues?q=is%3Aissue+is%3Aclosed)
[![Closed PRs](https://img.shields.io/github/issues-pr-closed/erikw/restic-systemd-automatic-backup?color=success)](https://github.com/erikw/restic-systemd-automatic-backup/pulls?q=is%3Apr+is%3Aclosed)
[![License](https://img.shields.io/badge/license-BSD--3-blue)](LICENSE.txt)
[![OSS Lifecycle](https://img.shields.io/osslifecycle/erikw/restic-systemd-automatic-backup)](https://github.com/Netflix/osstracker)
[![Latest tag](https://img.shields.io/github/v/tag/erikw/restic-systemd-automatic-backup)](https://github.com/erikw/restic-systemd-automatic-backup/tags)
<br>
## Restic
[![Contributors](https://img.shields.io/github/contributors/erikw/restic-systemd-automatic-backup)](https://github.com/erikw/restic-systemd-automatic-backup/graphs/contributors) including these top contributors:
<a href = "https://github.com/erikw/restic-systemd-automatic-backup/graphs/contributors">
<img src = "https://contrib.rocks/image?repo=erikw/restic-systemd-automatic-backup&max=24"/>
</a>
# Intro
[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!
@@ -11,58 +29,64 @@ Here follows a step-by step tutorial on how to set it up, with my sample script
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.
# Requirements
* `restic >=v0.9.6`
## Set up
# 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
```console
$ git clone https://github.com/erikw/restic-systemd-automatic-backup.git
$ cd restic-systemd-automatic-backup
$ sudo make install
````
### 1. Create Backblaze B2 account
Arch Linux users can install the aur package [restic-systemd-automatic-backup](https://aur.archlinux.org/packages/restic-systemd-automatic-backup/) e.g. :
```console
$ yaourt -S restic-systemd-automatic-backup
````
## 1. Create Backblaze B2 account
First, see this official Backblaze [tutorial](https://help.backblaze.com/hc/en-us/articles/115002880514-How-to-configure-Backblaze-B2-with-Restic-on-Linux) on restic, and follow the instructions ("Create Backblaze account with B2 enabled") there on how to create a new B2 bucket.
Take note of the your account ID, application key and password for the next steps.
### 2. Configure your B2 account locally
## 2. Configure your B2 account locally
Put these files in `/etc/restic/`:
* `b2_env.sh`: Fill this file out with your B2 bucket settings etc. The reason for putting these in a separate file is that it can be used also for you to simply source, when you want to issue some restic commands. For example:
```bash
```console
$ source /etc/restic/b2_env.sh
$ restic snapshots # You don't have to supply all parameters like --repo, as they are now in your environment!
````
* `b2_pw.txt`: Put your B2 password in this file.
* `b2_pw.txt`: This file should contain the restic repository password. This is a new password what soon will be used when initializing the new repository. It should be unique to this restic backup repository and is needed for restoring from it. Don't re-use your b2 login password, this should be different.
### 3. Initialize remote repo
## 3. Initialize remote repo
Now we must initialize the repository on the remote end:
```bash
source /etc/restic/b2_env.sh
restic init
```console
$ source /etc/restic/b2_env.sh
$ restic init
```
### 4. Script for doing the backup
## 4. Script for doing the backup
Put this file in `/usr/local/sbin`:
* `restic_backup.sh`: A script that defines how to run the backup. Edit this file to respect your needs in terms of backup which paths to backup, retention (number of backups to save), etc.
Put this file in `/`:
Copy this file to `/etc/restic/backup_exclude` or `~/.backup_exclude`:
* `.backup_exclude`: A list of file pattern paths to exclude from you backups, files that just occupy storage space, backup-time, network and money.
### 5. Make first backup & verify
## 5. Make first backup & verify
Now see if the backup itself works, by running
```bash
```console
$ /usr/local/sbin/restic_backup.sh
$ restic snapshots
````
### 6. Backup automatically; systemd service + timer
## 6. 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 day!
@@ -72,31 +96,31 @@ Put these files in `/etc/systemd/system/`:
Now simply enable the timer with:
```bash
```console
$ systemctl start restic-backup.timer
$ systemctl enable restic-backup.timer
````
You can see when your next backup is scheduled to run with
```bash
```console
$ systemctl list-timers | grep restic
```
and see the status of a currently running backup with
```bash
```console
$ systemctl status restic-backup
```
or start a backup manually
```bash
```console
$ systemctl start restic-backup
```
You can follow the backup stdout output live as backup is running with:
```bash
```console
$ journalctl -f -u restic-backup.service
````
@@ -104,7 +128,7 @@ $ journalctl -f -u restic-backup.service
### 7. Email notification on failure
## 7. Email notification on failure
We want to be aware when the automatic backup fails, so we can fix it. Since my laptop does not run a mail server, I went for a solution to set up my laptop to be able to send emails with [postfix via my Gmail](https://easyengine.io/tutorials/linux/ubuntu-postfix-gmail-smtp/). Follow the instructions over there.
Put this file in `/usr/local/sbin`:
@@ -116,14 +140,22 @@ Put this files in `/etc/systemd/system/`:
As you maybe noticed already before, `restic-backup.service` is configured to start `status-email-user.service` on failure.
### 8. Optional: automated backup checks
## 8. 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?
# 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.
# Uninstall
There is a make target to remove all files (scripts and configs) that were installed by `sudo make install`. Just run:
```console
$ sudo make uninstall
```

View File

@@ -1,7 +1,7 @@
[Unit]
Description=Check restic backup Backblaze B2 for errors
OnFailure=status-email-user@%n.service
Conflicts=restic.service
Conflicts=restic-backup.service
Requires=nm-unmetered-connection.service
[Service]

View File

@@ -65,12 +65,14 @@ restic backup \
$BACKUP_PATHS &
wait $!
# Dereference old backups.
# Dereference and delete/prune old backups.
# See restic-forget(1) or http://restic.readthedocs.io/en/latest/060_forget.html
# --group-by only the tag and path, and not by hostname. This is because I create a B2 Bucket per host, and if this hostname accidentially change some time, there would now be multiple backup sets.
restic forget \
--verbose \
--tag $BACKUP_TAG \
--option b2.connections=$B2_CONNECTIONS \
--prune \
--group-by "paths,tags" \
--keep-daily $RETENTION_DAYS \
--keep-weekly $RETENTION_WEEKS \
@@ -78,13 +80,6 @@ restic forget \
--keep-yearly $RETENTION_YEARS &
wait $!
# Remove old data not linked anymore.
# See restic-prune(1) or http://restic.readthedocs.io/en/latest/060_forget.html
restic prune \
--option b2.connections=$B2_CONNECTIONS \
--verbose &
wait $!
# Check repository for errors.
# NOTE this takes much time (and data transfer from remote repo?), do this in a separate systemd.timer which is run less often.
#restic check &

View File

@@ -2,7 +2,7 @@
# Send email notification from systemd.
# Source: https://serverfault.com/questions/876233/how-to-send-an-email-if-a-systemd-service-is-restarted
# Source: https://wiki.archlinux.org/index.php/Systemd/Timers#MAILTO
# Usage: systemd-email <recipinent-email> <failed-systemd-unit-name>
# Usage: systemd-email <recipient-email> <failed-systemd-unit-name>
# According to
@@ -47,11 +47,11 @@ if ! waited_long_enough; then
fi
recipinent=$1
recipient=$1
system_unit=$2
sendmail -t <<ERRMAIL
To: $recipinent
To: $recipient
From: systemd <root@$HOSTNAME>
Subject: [systemd-email] ${system_unit}
Content-Transfer-Encoding: 8bit