Backup & Restore a Linux system

Production Device:

1. Make sure your NFS share is mounted during boot

go here

2. Create the following script, e.g. /usr/local/bin/backup_system.sh

#!/bin/bash
timestamp_start=`date +%s`
find / -type s -print > /tmp/sockets-to-exclude
tar cvpzf /mnt/NAS/backup.$(date +\%Y\%m\%d\%H\%M\%S).tar.gz -C / -X /tmp/sockets-to-exclude --exclude=dev --exclude=mnt --exclude=proc --exclude=sys --exclude=tmp --exclude=lost+found / > /mnt/NAS/backup.$(date +\%Y\%m\%d\%H\%M\%S).log
tar_returncode=$?
timestamp_end=`date +%s`
runtime=$((timestamp_end-timestamp_start))
echo "Backup script finished in " $runtime " seconds"
if [[ $tarreturncode -eq 1 ]]; then
        exit 0
else
        exit $tar_returncode
fi

3. Create a cron job

/etc/cron.d/backup_system

30 2 * * * root (tar --exclude=var/lib/openhab/cache --exclude=var/lib/openhab/tmp -czf /mnt/NAS/file-backups/system_data_$(date +\%Y\%m\%d\%H\%M\%S).tar.gz -C / etc/cron.d etc/fstab etc/ssmtp opt usr/local/bin etc/openhab var/lib/openhab var/log/openhab)
45 2 * * * root (find /mnt/NAS/file-backups -mtime +180 -delete)
30 4 * * * root (/usr/local/bin/backup_system.sh)