Skip to content

Instantly share code, notes, and snippets.

@tisseurdetoile
Last active January 23, 2018 01:37
Show Gist options
  • Save tisseurdetoile/f6078e69e4877133f5d7 to your computer and use it in GitHub Desktop.
Save tisseurdetoile/f6078e69e4877133f5d7 to your computer and use it in GitHub Desktop.
backup my PI to my NAS with rsync

My backup.sh

Backup script for my PI to a rsync Server (work with my QNAP) It will make 7 backup for the weekdays and a monthly backup (see the MONTHLYSAV variable) So you will get a backup of the last seven days and monthly one.

Getting Started

Just copy/paste the content of the gist backup.sh the file in some folder like /usr/local/bin/backup.sh Make it runable chmod +x /usr/local/bin/backup.sh

Prerequisites

This script need an RSYNC server configured. (see the doc of your NAS or rsync)

Installing

Edit the file : change the :

  • HOSTNAME - the name you want in the backup folder for this computer.
  • SERVER - the name you want in the backup folder for this computer.
  • USERNAME - the rsync username RSYNC_PASSWORD - the rsync password PREFIX - prefix of the backup folder AREXCLUDE - a list of folder to exclude for the backup.

Add this in a root crontab. like this 30 19 * * * /usr/local/bin/backup.sh

Authors

  • A lot of example from the web
  • Le tisseurDeToile - this work -

License

This project is licensed under the MIT License

#!/bin/bash
HOSTNAME=`hostname`
SERVER="SRVHOST"
USERNAME="USER"
export RSYNC_PASSWORD="password"
PREFIX="/backup"
AREXCLUDE=( "/dev/*" "/proc/*" "/sys/*" "tmp/*" "/run/*" "/mnt/*" "/media/*" "lost+found" "/usr/share/*" "/lib/*" "/var/lib/*" "/usr/lib/*" "/var/swap" "/var/cache/*" "/var/lock/*" "/var/run/*")
START=$(date +%s)
DAYNUM=`date '+%u'`
MONTHNUM=`date '+%m'`
MONTHLYSAV='4'
SAVPATH=''
ping -c1 -w1 $SERVER
if [ "$?" = 0 ]
then
echo "Serveur UP"
else
echo "Serveur DOWN :("
exit 0
fi
for elt in "${AREXCLUDE[@]}"; do EXPARAM="$EXPARAM--exclude=$elt ";done
if [ $DAYNUM = $MONTHLYSAV ]
then
SAVPATH="$PREFIX/$HOSTNAME-M_$MONTHNUM"
else
SAVPATH="$PREFIX/$HOSTNAME-D_$DAYNUM"
fi
rsync -aAXv --delete --delete-after $EXPARAM /* rsync://$USERNAME@$SERVER:$SAVPATH
FINISH=$(date +%s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment