Skip to content

Instantly share code, notes, and snippets.

@magarcia
Last active November 2, 2015 19:32
Show Gist options
  • Save magarcia/cc66e4bf1c3d748798e7 to your computer and use it in GitHub Desktop.
Save magarcia/cc66e4bf1c3d748798e7 to your computer and use it in GitHub Desktop.
Automated RaspberryPi image backup into a backup.img.gzip
!/bin/bash
# Backup OS to the USB Hard Disk Drive
# Create a filename with timestamp for our current backup (without .img suffix)
ofile="/data/Backups/$(date +%Y-%m-%yT%T)"
# Create final filename, with suffix
ofilefinal=$ofile.img.gz
# Begin the backup process, should take about 1 hour from 8Gb SD card to HDD
sudo dd if="/dev/mmcblk0" bs=1M | gzip > $ofile
# Collect result of backup procedure
result=$?
# If command has completed successfully, delete previous backups and exit
if [ $result=0 ]; then
find /data/Backups/* -mtime +30 -exec rm {} \;
mv $ofile $ofilefinal;
exit 0;
fi
#If command has failed, then delete partial backup file
if [ $result=1 ]; then rm -f $ofile; exit 1;fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment