Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Last active March 3, 2024 12:34
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save perfecto25/f528f8d14e1c4b6e2a912513539a5af7 to your computer and use it in GitHub Desktop.
Save perfecto25/f528f8d14e1c4b6e2a912513539a5af7 to your computer and use it in GitHub Desktop.
Restic cheatsheet

Restic backup application - commands cheatsheet

Installation & config

  1. add Retic repo
  2. yum install restic

add a Restic credential file to root

vim /root/.restic

export RESTIC_REPOSITORY=s3:https://s3.amazonaws.com/bkup/restic/<name of host>
export AWS_SECRET_ACCESS_KEY=<key>
export AWS_ACCESS_KEY_ID=<id>
export RESTIC_PASSWORD="<pw>"

initialize S3 Repo

source /etc/restic/cred
restic init

created restic repository 73974eeba3 at s3:https://s3.amazonaws.com/bkup/restic/awstestbox

Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.

Backing up directories

backup a single directory

restic backup /home/user

create a file that contains paths to backup (include and exclude), run restic

restic backup --files-from=restic_include.conf --exclude-file=restic_exclude.conf

open repository
repository 73974eeb opened successfully, password is correct

Files:        4136 new,     0 changed,     0 unmodified
Dirs:            3 new,     0 changed,     0 unmodified
Added to the repo: 261.182 MiB

processed 4136 files, 285.394 MiB in 0:18
snapshot a40aec1c saved

Check file difference

use diff to compare different snapshots show snapshots

restic snapshots

repository 73974eeb opened successfully, password is correct
ID        Time                 Host        Tags        Paths
-------------------------------------------------------------------------
a40aec1c  2019-02-27 16:08:54  awstestbox              /home  /root

16d01d35  2019-02-27 16:13:19  awstestbox              /home  /root
-------------------------------------------------------------------------
2 snapshots

compare diff between 2 snapshots

restic diff a40aec1c 16d01d35

repository 73974eeb opened successfully, password is correct
comparing snapshot a40aec1c to 16d01d35:

+    /root/test.txt
M    /var/log/messages
M    /var/spool/mail/root

Files:           1 new,     0 removed,     2 changed

Find a file in a snapshot

restic find /etc/hosts

List files in latest snaphot

restic ls -l latest

Cat file in a snapshot

restic dump latest /etc/hosts (or insert snapshot ID instead of latest)

Restoring from a snapshot

example, restore a user's /home directory from latest snapshot

restic restore latest --target / --include /home/<user>

to restore to a specific dir (temporary dir)

restic restore latest --target /tmpdir --include /home/<user>

to restore all paths,

restic restore latest (or snapshot ID) --target /

Cron

configure restic to run daily and email status

0 0 * * * sh /etc/restic/run_backup.sh 

run_backup.sh

#!/bin/bash
# Runs Restic backup on a schedule via cron, emails with status

FROM="restic@$(hostname)"
EMAIL="admin@company.com"
EMAIL_ALL="group@company.com"
LOG="/var/log/restic.log"
RDIR="/etc/restic"

### keep last # of days of snapshots
KEEPDAYS=10

log() { 
    echo -e "$(date "+%m%d%Y_%H%M%S"): ${1}" | tee -a $LOG
}

notify() {
    echo -e "${1}" | mail -r "${FROM}" -s "Errors running Restic Backup on host: $(hostname)" "$1" 
}

cd $RDIR

echo -e "\n" | tee -a $LOG

if [ ! -f cred ]
then
	log "${RDIR}/cred file not present, exiting..\n\n"
	exit 1
fi

source ${RDIR}/cred

log "starting backup.."

msg=$(restic backup --files-from=include --exclude-file=exclude >> $LOG 2>&1)

if [ $? -eq 1 ]
then
    notify "[restic backup]\n${msg}" $EMAIL_ALL
    log "${msg}\n-----------------------------------------"
    exit 1
fi

msg=$(restic check >> $LOG 2>&1)

# Check for Errors
if [ $? -eq 1 ]
then
    notify "[restic check]\n${msg}" $EMAIL_ALL
    log "${msg}\n--------------------------------------"
    exit 1
fi


log "removing old snapshots.."

msg=$(restic forget --keep-daily ${KEEPDAYS} --prune)

if [ $? -eq 1 ]
then
    notify "[restic forget]\n${msg}" $EMAIL
    log "${msg}"
    exit 1
fi


log "end of run\n-----------------------------------------\n\n"

# notify OK
echo -e "Snapshot complete, snapshots older than $KEEPDAYS days deleted." | mail -r "${FROM}" -s "Restic Backup OK on: $(hostname)" ${EMAIL}

include and exclude examples:

include

/home
/root
/etc
/var/spool
/var/cache/salt
/var/www
/boot

exclude

*.log
*.swp
/home/*/.local/share/Trash
/home/*/.Trash
/home/*/.thumbnails
**.log
**.qlog

Removing old snapshots

remove a snapshot by ID

restic forget SNAPSHOT_ID
restic prune

keep only last 5 snapshots, remove all others

restic forget --keep-daily

Mount S3 as a file server

install fusermount

yum install fuse
restic mount /mnt/restic

restic will mount the S3 bucket. Quit the process to unmount.

all files are located under

/mnt/restic/snapshots/latest
@pzeinlinger
Copy link

Thanks for your excellent work! :-) There is a small error in the notify function:
notify() {
echo -e "${1}" | mail -r "${FROM}" -s "Errors running Restic Backup on host: $(hostname)" "$1" <- should be $2
}

@kayvanaarssen
Copy link

@perfecto25 Cannot get the log from a failure in the emal only getting: [restic backup] in the body of the email.
Would like to get the hole error, like if the repository is not available you get a error from Restic etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment