- add Retic repo
- 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.
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
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
restic find /etc/hosts
restic ls -l latest
restic dump latest /etc/hosts (or insert snapshot ID instead of latest)
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 /
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="[email protected]"
EMAIL_ALL="[email protected]"
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
/home
/root
/etc
/var/spool
/var/cache/salt
/var/www
/boot
exclude
*.log
*.swp
/home/*/.local/share/Trash
/home/*/.Trash
/home/*/.thumbnails
**.log
**.qlog
remove a snapshot by ID
restic forget SNAPSHOT_ID
restic prune
keep only last 5 snapshots, remove all others
restic forget --keep-daily
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
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
}