initial commit
This commit is contained in:
		
				commit
				
					
						741725ba4e
					
				
			
		
					 1 changed files with 108 additions and 0 deletions
				
			
		
							
								
								
									
										108
									
								
								backup.sh
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										108
									
								
								backup.sh
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,108 @@
 | 
			
		|||
#!/usr/bin/env bash
 | 
			
		||||
# This script is intended to be run by a systemd timer
 | 
			
		||||
 | 
			
		||||
# Exit on failure or pipefail
 | 
			
		||||
set -e -o pipefail
 | 
			
		||||
 | 
			
		||||
#Set this to any location you like
 | 
			
		||||
BACKUP_PATHS="/"
 | 
			
		||||
BACKUP_EXCLUDES="/etc/restic/exclude"
 | 
			
		||||
 | 
			
		||||
LOG_DIR="/var/log/restic"
 | 
			
		||||
LOG_BASENAME="backup"
 | 
			
		||||
LOG_FILE="$LOG_DIR/$LOG_BASENAME-$(date +%Y%m%d%H%M%S)"
 | 
			
		||||
LOG_KEEP=14
 | 
			
		||||
 | 
			
		||||
BACKUP_TAG=$(hostname -s)
 | 
			
		||||
 | 
			
		||||
STARTTIME=$(date "+%Y-%m-%d %H:%M:%S")
 | 
			
		||||
 | 
			
		||||
# How many backups to keep.
 | 
			
		||||
RETENTION_DAYS=7
 | 
			
		||||
RETENTION_WEEKS=4
 | 
			
		||||
RETENTION_MONTHS=3
 | 
			
		||||
RETENTION_YEARS=1
 | 
			
		||||
 | 
			
		||||
source /etc/restic/env
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Paths to binaries
 | 
			
		||||
CURL="/usr/bin/curl"
 | 
			
		||||
TEE="/usr/bin/tee"
 | 
			
		||||
FIND="/usr/bin/find"
 | 
			
		||||
RESTIC="/usr/bin/restic"
 | 
			
		||||
ECHO="/usr/bin/echo"
 | 
			
		||||
 | 
			
		||||
HOST=$(hostname -s)
 | 
			
		||||
 | 
			
		||||
# define functions
 | 
			
		||||
run_backup () {
 | 
			
		||||
#Do the backup
 | 
			
		||||
	${ECHO} "Starting backup on $HOST at $STARTTIME"
 | 
			
		||||
	${RESTIC} backup \
 | 
			
		||||
		--tag $BACKUP_TAG \
 | 
			
		||||
		--exclude-file $BACKUP_EXCLUDES \
 | 
			
		||||
		$BACKUP_PATHS
 | 
			
		||||
	return $?
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
run_forget (){
 | 
			
		||||
	# Remove old Backups
 | 
			
		||||
	${ECHO} "Removing old backups per retention settings"
 | 
			
		||||
	${RESTIC} forget \
 | 
			
		||||
		--tag $BACKUP_TAG \
 | 
			
		||||
		--prune \
 | 
			
		||||
		--keep-daily $RETENTION_DAYS \
 | 
			
		||||
		--keep-weekly $RETENTION_WEEKS \
 | 
			
		||||
		--keep-monthly $RETENTION_MONTHS \
 | 
			
		||||
		--keep-yearly $RETENTION_YEARS
 | 
			
		||||
	return $?
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
run_check () {
 | 
			
		||||
	# Check if everything is fine
 | 
			
		||||
	${ECHO} "Running check"
 | 
			
		||||
	${RESTIC} check
 | 
			
		||||
	return $?
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
cleanup () {
 | 
			
		||||
	# Remove locks in case other stale processes kept them in
 | 
			
		||||
	${ECHO} "Removing lockfile"
 | 
			
		||||
	${RESTIC} unlock
 | 
			
		||||
	${ECHO} "Removing logs ($LOG_DIR/$LOG_BASE-*) older than $LOG_KEEP days"
 | 
			
		||||
	${FIND} $LOG_DIR -name $LOG_BASE-* -mtime +$LOG_KEEP
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
send_matrix () {
 | 
			
		||||
	key="***REMOVED***"
 | 
			
		||||
	room_id="***REMOVED***"
 | 
			
		||||
	${CURL} -skd "{\"body\": \"$1\"}" http://192.168.0.101:4785/\?key\=$key\&room_id\=$room_id > /dev/null
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
notify () {
 | 
			
		||||
	#notify works either from stdin or from $1, so you can call it as
 | 
			
		||||
	# notify "text to send"
 | 
			
		||||
	# or
 | 
			
		||||
	# echo "text to send" | notify
 | 
			
		||||
	local message=$1
 | 
			
		||||
	if [ -z "$message" ]; then
 | 
			
		||||
		read message
 | 
			
		||||
	fi
 | 
			
		||||
	
 | 
			
		||||
	# call whatever notification script/function you wish with "$message" as the part you want to show
 | 
			
		||||
	send_matrix "$message"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Do the actual work
 | 
			
		||||
if run_backup && restic unlock && run_forget && cleanup; then
 | 
			
		||||
# run_check
 | 
			
		||||
#cleanup
 | 
			
		||||
	ENDTIME=$(date "+%Y-%m-%d %H:%M:%S")
 | 
			
		||||
#status_total=$(( check_status + backup_status + forget_status ))
 | 
			
		||||
#if (( status_total == 0 ));then
 | 
			
		||||
	${ECHO} "Backup on $HOST completed successfully at $ENDTIME" | tee -a $LOG_FILE >(notify)
 | 
			
		||||
else
 | 
			
		||||
	${ECHO} "Backup on $HOST completed at $ENDTIME with issues (see $LOG_FILE)" | tee -a $LOG_FILE >(notify)
 | 
			
		||||
fi
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue