-
-
Save eloo/35668c32dc8abc342354d38a5b5e89bd to your computer and use it in GitHub Desktop.
Create Raspberry Pi backups using a script launched as a cronjob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Cron installation: | |
# 30 05 * * 0 /root/scripts/raspberry-pi-backup.sh >> /var/log/backup.log 2>&1 | |
# Configuration | |
external="/mnt/backup" # Check if external storage is mounted | |
folder="$external/raspi" # Base backup folder | |
dev="/dev/mmcblk0" # Device to backup | |
days=60 # Delete backups older than X days | |
min_backups=30 # Amount of minimum backups to keep | |
echo "--------------------------------------" | |
echo "Start at: `date "+%Y-%m-%d %H:%M:%S"`" | |
if [ "$external" != "" ]; then | |
mounted=$(mount | grep $external) | |
if [ "$mounted" == "" ]; then | |
echo 'External Disk is not mounted' | |
mount | |
exit 1 | |
fi | |
fi | |
if [ ! -d "$folder" ]; then | |
install -d "$folder" | |
fi | |
image=$folder'/'$(date "+%Y-%m-%d_%H-%M-%S")'.img.gzip' | |
echo "Dumping dev to $image" | |
echo "dd if=$dev bs=1M | gzip > $image" | |
dd if=$dev bs=1M | gzip > $image | |
echo "Check amount of backups" | |
echo "find $folder -maxdepth 1 -name "*.img.gzip" -type f -mtime -$days | wc -l" | |
current_backups=`find $folder -maxdepth 1 -name "*.img.gzip" -type f -mtime -$days | wc -l` | |
if [ "$current_backups" -gt "$min_backups" ]; then | |
echo "More than $min_backups backups to keep found" | |
echo "Deleting old images" | |
echo "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 {} \; | |
fi | |
echo "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