Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Created October 27, 2015 10:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save eusonlito/419dbb16af7941db6fa0 to your computer and use it in GitHub Desktop.
Save eusonlito/419dbb16af7941db6fa0 to your computer and use it in GitHub Desktop.
Create Raspberry Pi backups using a script launched as a cronjob
#!/bin/bash
# Cron installation:
# 30 05 * * * /root/scripts/raspberry-pi-backup.sh >> /root/logs/raspberry-pi-backup.log 2>&1
# Configuration
external="/backup" # Check if external storage is mounted
folder="$external/raspi" # Base backup folder
dev="/dev/mmcblk0" # Device to backup
days=10 # Delete backups older than X days
e() {
echo $1
echo ''
}
echo ""
e "Start at: `date "+%Y-%m-%d %H:%M:%S"`"
if [ "$external" != "" ]; then
mounted=$(mount | grep $external)
if [ "$mounted" == "" ]; then
echo ''
echo 'External Disk is not mounted'
echo ''
mount
exit 1
fi
fi
if [ ! -d "$folder" ]; then
install -d "$folder"
fi
image=$folder'/'$(date "+%Y%m%d-%H%M%S")'.img.gzip'
e "Dumping dev to $image"
e "dd if=$dev | gzip > $image"
dd if=$dev | gzip > $image
e "Deleting old images"
e "find $folder -maxdepth 1 -name *.img.gzip -type f -mtime +$days -exec rm -rf {} \;"
find $folder -maxdepth 1 -name "*.img.gzip" -type f -mtime +$days -exec rm -rf {} \;
e "End at: `date "+%Y-%m-%d %H:%M:%S"`"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment