Rsync raspberry bash script
Introduction
Here you'll find a bash script to rsync your Raspberry OS to a external sd-card.
Just incase something is screwed up with your production OS.
So basicly you have a clone of your current running OS.
When it happens you can simply swap cards and be up and running in 30 seconds.
WARNING: I would advise you to test this script on a non production OS first.
Just incase something goes wrong...
This script doesn't work for Raspbian for NOOBS installations
Dependencies
In order to use this script you need have the following dependencies:
- Hardware: Raspberry Pi or other unix based system
- unix/linux with rsync and parted installed
- sd-card with sd-card reader to put in a free USB-port
- A bit of general raspberry knowledge
No dependencies needed on the Domoticz side
Installation
1. Log into your Pi using ssh and type: sudo blkid
Take a note of the available devices, so you can identify your new sd-card later
2. Take a fresh sd-scard and connect it with you Pi using some sort of usb-reader
Which you can miss as it is supposed to sit for ever ;-)
3. Now type: sudo blkid again after you inserted your card-reader with the fresh sd-card
Take a note of the available device and look for a new device
In my examples below i use /dev/sda
4. Now it's time to prepare your intended backup sd-card
5. Type: sudo parted /dev/sda
In parted each new command line is listed with (parted)
6. now type: mktable msdos and confirm
7. now type: mkpart primary fat32 0% 100M
8. now type: mkpart primary ext4 100M 100%
9. When done type quit <hit enter> and it exits parted
10. Create the boot and root file systems by:
sudo mkfs.vfat -n BOOT -F 32 /dev/sda1
sudo mkfs.ext4 /dev/sda2
You sd-card is ready, only thing left is to prep the rsync.sh script
11. Back in ssh you type: sudo blkid to determine the new UUID for /boot and /root
You need these hashes to fill in settings part of the script and your ready to go rsync.
This step is needed, don't use the hashes you saw earlier as the sd-card is formated and tose hashes are changed.
Output should give you something like below:
pi@domoticz:~$ sudo blkid
/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="70F7-FA1D" TYPE="vfat" PARTUUID="37e33438-01"
/dev/sda1: LABEL="BOOT" UUID="30BA-4322" TYPE="vfat" PARTUUID="253a8c9d-01"
/dev/sda2: UUID="ae5422e9-a0d2-40eb-8f23-60fa03884047" TYPE="ext4" PARTUUID="253a8c9d-02"
/dev/sdb1: UUID="8f61af08-b00e-4b1f-b169-4995fb028969" TYPE="ext4" PARTUUID="c0ac452a-01"
/dev/sdb2: UUID="89d5240e-0fd7-4e84-b3eb-bf29ed8da2a5" TYPE="ext4" PARTUUID="c0ac452a-02"
As earlier doing sudo blkid we know /dev/sda1 & sda2 are newly create and there for our backup sd-card
12. Now edit rsync.sh with your desired editor
13. Add the sda1 /boot UUID to BOOT_UUID line in rsync.sh (not PARTUUID)
14. Add the sda2 /root UUID to DATA_UUID line in rsync.sh (not PARTUUID)
15. Now copy this script to your Raspberry Pi (for example: /home/pi/domoticz/scripts/bash/rsync.sh)
Again login into your Pi via ssh and make it executable by chmod +x /to_where_ever/rsync.sh
16. When done then you are ready to use this script to clone your current OS
By simply run: sudo bash /to_where_ever/rsync.sh
Script
#!/bin/bash
###############################################################################################################################################################
### rsync.sh
### @author : Siewert Lameijer
### @since : 27-12-2016
### @updated: 27-12-2016
### Script to rsync your Pi in case something screw your system.
### Just switch your current sd-card with the rsynced sd-card and your ready to go...
###############################################################################################################################################################
# WARNING: Try this script first before putting it in action on your production OS
# rync isn't 100% secure, it can screw things up and there for make your backup unusable, be aware that...
# For installation instructions then visite the link below:
# Wiki: http://domoticz.com/wiki/Rsync_raspberry_bash_script
# Forum: https://www.domoticz.com/forum/viewtopic.php?f=23&t=15086
###############################################################################################################################################################
### BEGINNING OF USER CONFIGURABLE PARAMETERS
###############################################################################################################################################################
MOUNT_LOCATION="/mnt" # Choose your desired mount point to start with
MOUNT_DATA_FOLDER="backup" # Choose your desired folder name to rsync to /mnt/backup for example
BOOT_UUID="30BA-4322" # Get your UUDI for /boot partition from your backup sd-card by sudo blkid in shell
DATA_UUID="ae5422e9-a0d2-40eb-8f23-60fa03884047" # Get your UUDI for /root partition from your backup sd-card by sudo blkid in shell
EXCLUDE_FILE="/mnt/storage/domoticz_scripts/rsync_exclude/rsync_exclude.txt" # exclude file only contains /mnt*/
###############################################################################################################################################################
### END OF USER CONFIGURABLE PARAMETERS
###############################################################################################################################################################
###############################################################################################################################################################
### Do not edit anything below this line unless your knowing what to do!
###############################################################################################################################################################
GREP_BOOT_UUID=`sudo blkid | grep $BOOT_UUID | /usr/bin/cut -d ":" -f 1`
GREP_DATA_UUID=`sudo blkid | grep $DATA_UUID | /usr/bin/cut -d ":" -f 1`
GREP_DATA_PARTUUID=`sudo blkid | grep $DATA_UUID | awk {'print $4'} | /usr/bin/cut -d '"' -f 2 | /usr/bin/cut -d '"' -f 1`
BOOT_MOUNT=$GREP_BOOT_UUID
DATA_MOUNT=$GREP_DATA_UUID
RSYNC=`sudo dpkg-query -l | grep rsync | wc -l`
echo "
____ _ _ ____
| _ \ ___ _ __ ___ ___ | |_(_) ___ ____ | _ \ ___ _ _ _ __ ___
| | | |/ _ \| _ _ \ / _ \| __| |/ __|_ / | |_) / __| | | | _ \ / __|
| |_| | (_) | | | | | | (_) | |_| | (__ / / | _ <\__ \ |_| | | | | (__
|____/ \___/|_| |_| |_|\___/ \__|_|\___/___| |_| \_\___/\___ |_| |_|\___|
|___/
"
sleep 1
### Stop Domoticz service
echo "::: Stop Domoticz Service before rsync OS"
echo "---------------------------------------------------"
sleep 1
echo "--- Stopping Domoticz Service"
echo "Please standby!"
sudo service domoticz.sh stop
echo "--- Done!"
echo " "
sleep 2
### Checking if backup folders are existing
echo "::: Checking necessities"
echo "---------------------------------------------------"
sleep 1
if [ $RSYNC -eq 1 ]; then
echo "--- Rsync seems to be installed!"
sleep 1
else
echo "---- Rsync isn't installed and trying to install it..."
echo "---- Please standby!"
sudo apt-get update > /dev/null 2>&1
sudo apt-get install -y rsync > /dev/null 2>&1
fi
echo "--- Done!"
echo " "
sleep 1
### Checking if backup folders are existing
echo "::: Checking if backup folder exists"
echo "---------------------------------------------------"
sleep 1
if [ -d $MOUNT_LOCATION/$MOUNT_DATA_FOLDER ] ; then
echo "--- $MOUNT_LOCATION/$MOUNT_DATA_FOLDER exists"
sleep 1
else
echo "--- $MOUNT_LOCATION/$MOUNT_DATA_FOLDER doesn't exist"
echo "--- Trying to create $MOUNT_LOCATION/$MOUNT_DATA_FOLDER"
mkdir $MOUNT_LOCATION/$MOUNT_DATA_FOLDER
sleep 1
if [ -d $MOUNT_LOCATION/$MOUNT_DATA_FOLDER ] ; then
echo "--- $MOUNT_LOCATION/$MOUNT_DATA_FOLDER created!"
sleep 1
else
echo "---- Something went when trying to create $MOUNT_LOCATION/$MOUNT_DATA_FOLDER..."
sleep 1
exit
fi
fi
echo " "
sleep 1
### Check if data mount location is mounted
if grep -qs "$DATA_MOUNT" /proc/mounts; then
echo "--- $DATA_MOUNT Is mounted"
sleep 1
else
echo "--- $DATA_MOUNT Isn't mounted"
echo "--- Trying to mount!"
sleep 1
sudo mount $DATA_MOUNT $MOUNT_LOCATION/$MOUNT_DATA_FOLDER
if [ $? -eq 0 ]; then
echo "--- $DATA_MOUNT Mount success!"
sleep 1
else
echo "---- Something went wrong with the mount..."
sleep 1
exit
fi
fi
echo "--- Done!"
echo " "
sleep 1
### Checking if /boot backup folders are existing
echo "::: Checking if backup /boot folder exists"
echo "---------------------------------------------------"
sleep 1
if [ -d $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot ] ; then
echo "--- $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot exists"
sleep 1
else
echo "--- $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot doesn't exist"
echo "--- Trying to create $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot"
mkdir $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot
sleep 1
if [ -d $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot ] ; then
echo "--- $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot created!"
sleep 1
else
echo "---- Something went when trying to create $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot..."
sleep 1
exit
fi
fi
echo " "
sleep 1
### Check if backup /boot is mounted
if grep -qs "$BOOT_MOUNT" /proc/mounts; then
echo "--- $BOOT_MOUNT Is mounted"
sleep 1
else
echo "--- $BOOT_MOUNT Isn't mounted"
echo "--- Trying to mount!"
sleep 1
sudo mount $BOOT_MOUNT $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot
if [ $? -eq 0 ]; then
echo "--- $BOOT_MOUNT Mount success!"
sleep 1
else
echo "---- Something went wrong with the mount..."
sleep 1
exit
fi
fi
echo "--- Done!"
echo " "
sleep 1
### Rsyncing to backup location
echo "::: Rsync in progress"
echo "---------------------------------------------------"
echo "--- Rsyncing /boot & /root to mounted backup location"
echo "--- Please Standby"
sleep 1
sudo rsync -ax --exclude-from $EXCLUDE_FILE --delete --info=progress2 / /boot $MOUNT_LOCATION/$MOUNT_DATA_FOLDER
echo "--- Done!"
echo " "
sleep 1
### Amending /boot/cmline.txt
echo "::: Amending /boot/cmline.txt "
echo "---------------------------------------------------"
echo "--- Setting new /root location in backuped cmdline.txt"
echo "--- Please Standby"
GREP_CURRENT_ROOTFS=`sudo cat $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot/cmdline.txt | grep root | awk {'print $3'}`
sudo sed -i "s,$GREP_CURRENT_ROOTFS,root=/dev/mmcblk0p2," $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot/cmdline.txt
sleep 1
echo "--- Done!"
echo " "
### umount backup location
echo "::: umount /boot & /root backup location "
echo "---------------------------------------------------"
echo "--- Unmounting"
echo "--- Please Standby"
cd ..
sudo umount $MOUNT_LOCATION/$MOUNT_DATA_FOLDER/boot
cd ..
sudo umount $MOUNT_LOCATION/$MOUNT_DATA_FOLDER
echo "--- Done!"
echo " "
sleep 1
### Starting Domoticz service
echo "::: Starting Domoticz Service"
echo "---------------------------------------------------"
sleep 1
echo "--- Starting Domoticz Service"
echo "Please standby!"
sudo service domoticz.sh start
echo "--- Done!"
echo " "
sleep 1
### Last word
echo "::: Incase of a emergency"
echo "---------------------------------------------------"
echo "> New /rootfs has been set to root=/dev/mmcblk0p2"
echo "> Incase you screwed up you only need to put your Rsynced sdcard in your Pi sdcard slot"
echo "> And Reboot"
echo "> Have a nice day, Bye!"
echo " "
exit
Additional
If you like you can set this script as a cronjob so it rsyncs your OS daily,weekly or monthly
Links
Domoticz Thread: https://www.domoticz.com/forum/viewtopic.php?f=23&t=15086