Skip to content

Instantly share code, notes, and snippets.

@MatthewVance
Created May 18, 2019 15:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MatthewVance/c8c996bb052cdf265e498d15f0da0641 to your computer and use it in GitHub Desktop.
Save MatthewVance/c8c996bb052cdf265e498d15f0da0641 to your computer and use it in GitHub Desktop.
Restic backup maintenance script, scheduled with a systemd timer rather than cron job.
[Unit]
Description=Runs daily restic maintenance script
[Service]
Type=oneshot
ExecStart=/etc/restic/restic-maintenance-daily.sh
User=restic
[Unit]
Description=Run restic-daily-maintenance.service
[Timer]
Unit=restic-daily-maintenance.service
OnCalendar=*-*-* 02:35:00
[Install]
WantedBy=timers.target
#!/bin/bash
#: Title : restic-init
#: Date : July 15 2018
#: Author : Matt Vance
#: Version : 1.1
#: Description : This will run Restic maintenance tasks
#: License : MIT License (MIT)
# Copyright (C) 2019 Matthew Vance
export RESTIC_REPOSITORY=/Volumes/storage/matt/backup/repos/shared/
export RESTIC_PASSWORD_FILE=/etc/restic/restic-pw.txt
HOST=host_name
RESTIC=/usr/local/bin/restic
INCLUDE=/etc/restic/restic-include.txt
EXCLUDE=/etc/restic/restic-exclude.txt
LOG=/var/log/restic/restic.log
CACHE=/home/restic/.cache
#Define a timestamp function
timestamp() {
date "+%b %d %Y %T %Z"
}
# Add timestamp
echo "$(timestamp): restic daily maintenance started" | tee -a $LOG
echo "-------------------------------------------------------------------------------" | tee -a $LOG
# Check the repository for errors
$RESTIC check | tee -a $LOG
# Remove snapshots according to policy and remove unneeded data from the
# repository.
# It is more efficient to run forget just once in a 24-hour period and to
# run the prune command with it.
$RESTIC forget \
--prune \
--keep-last 3 \
--tag full \
--cache-dir $CACHE | tee -a $LOG
$RESTIC forget \
--prune \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 12 \
--keep-yearly 7 \
--keep-hourly 24 \
--cache-dir $CACHE | tee -a $LOG
# --keep-within 7y12m7d \
# Check the repository for errors
$RESTIC check | tee -a $LOG
# Add timestamp
echo "-------------------------------------------------------------------------------" | tee -a $LOG
echo "$(timestamp): restic daily maintenance finished" | tee -a $LOG
printf "\n" | tee -a $LOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment