Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
# BASH-RaspberryPI-System-Backup
Bash script for automatic imaging backup of a raspberry pi system while it's running
It also cleans out the backups older then the set retention period in days.
You can have several Raspberry Pi's backup to the same location since it will only clean
it's own backups based on $HOSTNAME.

Bash script for automatic imaging backup of a Raspberry Pi system while running. It also cleans out the backups older than the set retention period in days.

You can have several Raspberry Pi backups in the same location since it will only clean its backups based on `$HOSTNAME`.

## Install

``` bash
wget https://github.com/kallsbo/BASH-RaspberryPI-System-Backup/raw/master/system_backup.sh
chmod +x system_backup.sh
```

## Use

In order for the script to work you need either a mounted share or a mounted usb stick.
Either you can update the defualt values in the script in regards to backup path and retention or
provide it as command line parameters.
For the script to work, you need either a mounted share or a mounted USB stick.

Example: sudo ./system_backup.sh /mnt/usbstick 7
You can update the default values in the script in regards to backup path and retention or provide it as command line parameters.

This will put the backup in /mnt/usbstick and keep the backups for 7 days before cleaning them out.
Example: `sudo ./system_backup.sh /mnt/usbstick 7`

## Automate
This will put the backup in `/mnt/usbstick`` and keep the backups for 7 days before cleaning them out.

## Compression

Once you have tested the script and are done with the settings you can automate this bu adding it to
crontab. Since it needs to run as root you need to edit the root crontab like this:
If you desire a compressed image, you may use the optional `-c` flag at the end of your invocation.

Example: `sudo ./system_backup.sh /mnt/usbstick 7 -c`

Compression, especially on a Pi Zero, takes a significantly longer amount of time.

## Automate

sudo crontab -e
Once you have tested the script and are done with the settings, you can automate this by adding it to `cron`. Since it needs to run as root, you need to edit the root crontab like this:

I usally go for the default option 2 - nano. Then just add the line
`sudo crontab -e`

0 3 * * * /mnt/backup/system_backup.sh
If asked to select an editor, I usually use the default option 2 - nano. Then add the line:

This will make the script take a full image backup every night at 3 am.
`0 3 * * * /mnt/backup/system_backup.sh`

## More info
https://www.hackviking.com/single-board-computers/raspberry-pi/automated-raspberry-pi-backup-complete-image/
This will make the script take a full image backup every night at 3 a.m.
31 changes: 22 additions & 9 deletions system_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# Kristofer Källsbo 2017 www.hackviking.com
#
# Usage: system_backup.sh {path} {days of retention}
# Usage: system_backup.sh {path} {days of retention} {-c to compress}
#
# Below you can set the default values if no command line args are sent.
# The script will name the backup files {$HOSTNAME}.{YYYYmmdd}.img
Expand All @@ -15,15 +15,16 @@
# Declare vars and set standard values
backup_path=/mnt/backup
retention_days=3
compress=0

# Check that we are root!
if [[ ! $(whoami) =~ "root" ]]; then
echo ""
echo "**********************************"
echo "*** This needs to run as root! ***"
echo "**********************************"
echo ""
exit
echo ""
echo "**********************************"
echo "*** This needs to run as root! ***"
echo "**********************************"
echo ""
exit
fi

# Check to see if we got command line args
Expand All @@ -35,14 +36,26 @@ if [ -n "$2" ]; then
retention_days=$2
fi

# Check for compress flag in any arg position
for var in "$@"
do
if [ "$var" == "-c" ]; then
compress=1
fi
done

# Create trigger to force file system consistency check if image is restored
touch /boot/forcefsck

# Perform backup
dd if=/dev/mmcblk0 of="$backup_path"/"$HOSTNAME"."$(date +%Y%m%d)".img bs=1M
if [ "$compress" == 1 ]; then
dd bs=1M if=/dev/mmcblk0 | gzip > "$backup_path"/"$HOSTNAME"."$(date +%Y%m%d)".img.gz
else
dd if=/dev/mmcblk0 of="$backup_path"/"$HOSTNAME"."$(date +%Y%m%d)".img bs=1M
fi

# Remove fsck trigger
rm /boot/forcefsck

# Delete old backups
find "$backup_path"/"$HOSTNAME".*.img -mtime +"$retention_days" -type f -delete
find "$backup_path"/"$HOSTNAME".*.img* -mtime +"$retention_days" -type f -delete