Skip to content

Instantly share code, notes, and snippets.

@jlstanus
Last active October 8, 2021 15:13
Show Gist options
  • Save jlstanus/0fba2e7725548dd7611116c0ef6dc36b to your computer and use it in GitHub Desktop.
Save jlstanus/0fba2e7725548dd7611116c0ef6dc36b to your computer and use it in GitHub Desktop.
RPI Backup
#!/bin/bash
#
# Automate Raspberry Pi Backups
# Inspired from https://raw.githubusercontent.com/kallsbo/BASH-RaspberryPI-System-Backup/master/system_backup.sh
# Kristofer Källsbo 2017 www.hackviking.com
#
# Usage: system_backup.sh {path} {days of retention}
#
# Below you can set the default values if no command line args are sent.
# The script will name the backup files {$HOSTNAME}.{YYYYmmdd}.img
# When the script deletes backups older then the specified retention
# it will only delete files with it's own $HOSTNAME.
#
# Declare vars and set standard values
backup_path=/mnt/backup/
retention_days=3
# Check that we are root!
if [[ ! $(whoami) =~ "root" ]]; then
echo ""
echo "**********************************"
echo "*** This needs to run as root! ***"
echo "**********************************"
echo ""
exit
fi
# Check to see if we got command line args
if [ ! -z $1 ]; then
backup_path=$1
fi
if [ ! -z $2 ]; then
retention_days=$2
fi
# Create trigger to force file system consistency check if image is restored
touch /boot/forcefsck
# Perform backup
dd if=/dev/mmcblk0 of=$backup_path/$HOSTNAME.$(date +%Y%m%d).img bs=1M
# Remove fsck trigger
rm /boot/forcefsck
# Delete old backups
find $backup_path/$HOSTNAME.*.img -mtime +$retention_days -type f -delete
@jlstanus
Copy link
Author

jlstanus commented Nov 4, 2020

Configuration

  1. Install nfscommon
sudo apt-get update

sudo apt-get install nfs-common
  1. Create mounting dir
mkdir /mnt/backup
  1. Setup NFS, create backup dir on NAS then create fstab parameters
    Nom d'hôte ou ip: ip du client (rpi) 192.168.1.10
    Privilège: lecture/écriture
    Squash: mappage de tous les utilisateurs
    Sécurité: sys
    Cocher: activer le mode asucnhrone, permettre la connexion à partir des portsnnon priviléiés, permettre à des utilisateurs d'accéder aux sous dossier, etc
sudo nano /etc/fstab

Vérifier
192.168.1.xxx:/volume1/8_BackUPs/rpi /mnt/backup nfs rsize=8192,wsize=8192,timeo=14,intr 0 0

  1. Test the mount
sudo mount -a
  1. Place the script in the shared rpi dir
chmod +x system_backup.sh
  1. Add it to crontab
sudo crontab -e

0 3 * * * /mnt/backup/system_backup.sh

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