diff --git a/README.md b/README.md index a7c05a5..8c12253 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/system_backup.sh b/system_backup.sh index d32d2dc..71f75ef 100644 --- a/system_backup.sh +++ b/system_backup.sh @@ -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 @@ -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 @@ -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