All Projects → erikw → Restic Systemd Automatic Backup

erikw / Restic Systemd Automatic Backup

Licence: other
My restic backup solution using Backblaze B2 storage, systemd timers (or cron) and email notifications on failure.

Programming Languages

shell
77523 projects
script
160 projects

Projects that are alternatives of or similar to Restic Systemd Automatic Backup

butdr
Backup to Cloud( Google Drive, Dropbox ... ) use rclone
Stars: ✭ 49 (-84.39%)
Mutual labels:  backup, cron
aws-tag-sched-ops
Retired, please see https://github.com/sqlxpert/lights-off-aws
Stars: ✭ 24 (-92.36%)
Mutual labels:  backup, cron
VestaCP-Sync-Backups-To-Mega
VestaCP: uploading backups to the MEGA cloud
Stars: ✭ 17 (-94.59%)
Mutual labels:  backup, cron
Bash Toolkit
Este proyecto esá destinado a ayudar a los sysadmin
Stars: ✭ 13 (-95.86%)
Mutual labels:  cron, backup
Automation-using-Shell-Scripts
Development Automation using Shell Scripting.
Stars: ✭ 41 (-86.94%)
Mutual labels:  backup, cron
Sleepto
An alternative to traditional task schedulers
Stars: ✭ 98 (-68.79%)
Mutual labels:  cron, systemd
gnome-shell-extension-sermon
A GNOME Shell extension for monitoring and managing systemd services, cron jobs, docker and poman containers
Stars: ✭ 27 (-91.4%)
Mutual labels:  cron, systemd
pgsql-backup
PostgreSQL Backup Script. Ported from AutoMySQLBackup.
Stars: ✭ 24 (-92.36%)
Mutual labels:  backup, cron
dron
What if cron and systemd had a baby?
Stars: ✭ 30 (-90.45%)
Mutual labels:  cron, systemd
btrfs-backup
A simple, flexible script for versioned backups using btrfs and rsync
Stars: ✭ 59 (-81.21%)
Mutual labels:  backup, cron
Roam To Git
Automatic RoamResearch backup to Git
Stars: ✭ 489 (+55.73%)
Mutual labels:  cron, backup
emsm
A lightweight, easy to extend minecraft server manager.
Stars: ✭ 72 (-77.07%)
Mutual labels:  backup, cron
Resticprofile
Configuration profiles for restic backup
Stars: ✭ 48 (-84.71%)
Mutual labels:  backup, systemd
Mysqlbkup
Lightweight MySQL backup script in BASH
Stars: ✭ 129 (-58.92%)
Mutual labels:  cron, backup
cya
Easy to use snapshot and restore utility for any Linux (Unix) OS and filesystem powered by BASH
Stars: ✭ 73 (-76.75%)
Mutual labels:  backup, systemd
adsorber
Ad-blocker for Linux systems purely built on POSIX-compliant shell scripts.
Stars: ✭ 57 (-81.85%)
Mutual labels:  cron, systemd
docker-backup-to-s3
Docker container that periodically backups files to Amazon S3 using s3cmd and cron
Stars: ✭ 124 (-60.51%)
Mutual labels:  backup, cron
Routeros Scripts
a collection of scripts for Mikrotik RouterOS
Stars: ✭ 270 (-14.01%)
Mutual labels:  backup
Reshifter
Kubernetes cluster state management
Stars: ✭ 292 (-7.01%)
Mutual labels:  backup
Andotp
Open source two-factor authentication for Android
Stars: ✭ 3,326 (+959.24%)
Mutual labels:  backup

Automatic restic backups using systemd services and timers

Restic

restic 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 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. 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:

$ git clone https://github.com/erikw/restic-systemd-automatic-backup.git
$ cd restic-systemd-automatic-backup
$ sudo make install

1. Create Backblaze B2 account

First, see this official Backblaze tutorial 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

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:
$ 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: 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

Now we must initialize the repository on the remote end:

source /etc/restic/b2_env.sh
restic init

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.

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

Now see if the backup itself works, by running

$ /usr/local/sbin/restic_backup.sh
$ restic snapshots

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!

Put these files in /etc/systemd/system/:

  • restic-backup.service: A service that calls the backup script.
  • restic-backup.timer: A timer that starts the backup every day.

Now simply enable the timer with:

$ systemctl start restic-backup.timer
$ systemctl enable restic-backup.timer

You can see when your next backup is scheduled to run with

$ systemctl list-timers | grep restic

and see the status of a currently running backup with

$ systemctl status restic-backup

or start a backup manually

$ systemctl start restic-backup

You can follow the backup stdout output live as backup is running with:

$ journalctl -f -u restic-backup.service

(skip -f to see all backups that has run)

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. Follow the instructions over there.

Put this file in /usr/local/sbin:

  • systemd-email: Sends email using sendmail(1). This script also features time-out for not spamming Gmail servers and getting my account blocked.

Put this files in /etc/systemd/system/:

  • [email protected]: A service that can notify you via email when a systemd service fails. Edit the target email address in this file.

As you maybe noticed already before, restic-backup.service is configured to start status-email-user.service on failure.

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?

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].