Skip to content

Instantly share code, notes, and snippets.

@fabiosoft
Last active July 7, 2019 21:59
Show Gist options
  • Save fabiosoft/11216313 to your computer and use it in GitHub Desktop.
Save fabiosoft/11216313 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Descriprion:
# This simple script creates a local sd image backup of a remote raspberry pi in a local folder
# using ssh, dd, and the unix pipe
# Author: Fabio Nisci
# Date: 23/Apr/2014
# Requirements:
# - ssh
# - sshpass (auto installation)
#DEFAULTS
DFT_RASPBERRY_USER=pi # your raspberry pi username
DFT_RASPBERRY_PASS=raspberry # your raspberry pi password, the script assumes this user is in the sudoers file of the raspberry
DFT_RASPBERRY_ADDR=192.168.1.124 # IP address (or hostname if you're using a dynamic dns server)
DFT_BACKUP_FOLDER=/home/$USER/RB_BACKUP # local backup destination folder
DFT_BACKUP_NAME=$(date +"%Y%m%d_%H%M") # image filename
#------
#User asking data
read -p "Username on your Rpi (in sudoer)[pi]:" RASPBERRY_USER
RASPBERRY_USER=${RASPBERRY_USER:-$DFT_RASPBERRY_USER}
read -s -p "Password Rpi[raspberry]:" RASPBERRY_USER_PASS
RASPBERRY_PASS=${RASPBERRY_USER_PASS:-$DFT_RASPBERRY_PASS}
echo
read -p "Rpi host or IP [192.168.1.124]:" RASPBERRY_ADDR
RASPBERRY_ADDR=${RASPBERRY_ADDR:-$DFT_RASPBERRY_ADDR}
read -p "Local backup path[~/RB_BACKUP]:" BACKUP_FOLDER
BACKUP_FOLDER=${BACKUP_FOLDER:-$DFT_BACKUP_FOLDER}
read -p "Snapshot name[$DFT_BACKUP_NAME]:" BACKUP_NAME
BACKUP_NAME=${BACKUP_NAME:-$DFT_BACKUP_NAME}
#--------
BACKUP_IMAGE=$BACKUP_FOLDER/$BACKUP_NAME.img # absolute path to generated image
#Check is sshpass is installed
sshpass -V
if [ $? -ne 0 ]; then
echo "sshpass not installed"
echo "Installing..."
curl -O -L "http://downloads.sourceforge.net/project/sshpass/sshpass/1.05/sshpass-1.05.tar.gz"
tar xzf "sshpass-1.05.tar.gz"
cd "sshpass-1.05"
echo "Configuring"
./configure > /dev/null
echo "Making"
make > /dev/null
echo "Installing"
sudo make install > /dev/null
cd ..
rm -r "sshpass-1.05"
rm "sshpass-1.05.tar.gz"
sshpass -V
if [ $? -ne 0 ]; then
echo "Something went wrong, check sshpass installation manually. Then come back"
exit 1
fi
fi
# Notify user, script is starting
echo "RASPBERRY BACKUP: Starting backup process, raspberry pi: $RASPBERRY_ADDR"
# Try to ping raspberry, if you can't find it, exit now
ping -c 3 $RASPBERRY_ADDR > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
echo "RASPBERRY BACKUP: cannot ping raspberry at: $RASPBERRY_ADDR exiting"
exit 1
fi
# If destination dir does not exist, create it
if [ ! -d "$BACKUP_FOLDER" ]; then
mkdir -p $BACKUP_FOLDER
fi
echo "RASPBERRY BACKUP: Initiating SSH transfer, do not turn off your COMPUTER!!!"
echo "It could take a while (even 30min/1hour to complete. Just wait."
# This opens a ssh connection with raspberry, and using dd and the unix pipe creates a local device image
sshpass -p $RASPBERRY_PASS ssh $RASPBERRY_USER@$RASPBERRY_ADDR 'echo $RASPBERRY_PASS | sudo -S dd if=/dev/mmcblk0' | dd of=$BACKUP_IMAGE
# Check if everything is ok
if [ $? -ne 0 ]; then
echo "RASPBERRY BACKUP: Something went wrong with the ssh transmission, deleting temporary files, try again!"
rm $BACKUP_IMAGE
exit 1
fi
echo "RASPBERRY BACKUP: Transfer complete, compressing image..."
# Compress image
tar -pczf $BACKUP_FOLDER/$BACKUP_NAME.tar.gz $BACKUP_IMAGE
# Check compression result
if [ $? -ne 0 ]; then
echo "RASPBERRY BACKUP: Backup complete, BUT it wasn't possible to compress the image"
exit 1
fi
# Delete uncompressed files
rm $BACKUP_IMAGE
echo "RASPBERRY BACKUP: Backup Complete! Backup saved in $BACKUP_FOLDER"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment