Skip to content

Instantly share code, notes, and snippets.

@Fieel
Last active November 19, 2019 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fieel/02c45df9e10db6bdcba27cafc3c0607b to your computer and use it in GitHub Desktop.
Save Fieel/02c45df9e10db6bdcba27cafc3c0607b to your computer and use it in GitHub Desktop.
Backup your Raspbian OS in a remote NAS in your LAN, compressed into a .tar.gz archive.
#!/bin/bash
#
# @uthor: Filipe Madureira
# Backups a compressed raspbian system to a remote NAS location.
#
NAS_PATH="//server/share"
MOUNT_PATH="/mnt/remote-nas"
BACKUP_PATH=$MOUNT_PATH"/backups/"
TEMP_PATH="/tmp/backup"
LOG_PATH="/var/log/backup.log"
DATE=$(date +"%Y-%m-%d_%H%M")
# Log backup start
echo $DATE": Backup START" >> $LOG_PATH
mkdir $MOUNT_PATH
mkdir $TEMP_PATH
# Mount remote NAS
sudo mount -t cifs -o guest $NAS_PATH $MOUNT_PATH
# Start by copying all the filesystem and storing it in the tmp directory
sudo /usr/bin/rsync -Pavzh --delete --exclude $TEMP_PATH --exclude /run --exclude /var/run --exclude /sys --exclude /mnt --exclude /proc --exclude /dev --exclude /boot --exclude /media --exclude /mnt --exclude /lost+found / $TEMP_PATH
# Compress even more the backup into a .tar.gz file
sudo tar -cpzf $BACKUP_PATH$DATE.tar.gz $TEMP_PATH
# Remove the tmp files
sudo rm -r $TEMP_PATH
# Unmount remote NAS
sudo umount $MOUNT_PATH
# Log backup end
echo "$(date +"%Y-%m-%d_%H%M %T"): Backup END" >> $LOG_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment