Skip to content

Instantly share code, notes, and snippets.

@ChieftainY2k
Last active November 4, 2020 10:06
Show Gist options
  • Save ChieftainY2k/893d4974a1a008dc563e5ee0a0bcb97c to your computer and use it in GitHub Desktop.
Save ChieftainY2k/893d4974a1a008dc563e5ee0a0bcb97c to your computer and use it in GitHub Desktop.
Raspberry PI: Create system partition image to an external drive
#!/bin/bash
# Example: ./create_image.sh /mnt/raspios-$(date "+%Y%m%d").img
#helper function
log_message()
{
LOGPREFIX="[$(date '+%Y-%m-%d %H:%M:%S')][$(basename $0)]"
MESSAGE=$1
echo "$LOGPREFIX $MESSAGE"
}
#check for errors
check_errors()
{
EXITCODE=$1
if [[ ${EXITCODE} -ne 0 ]]; then
log_message "ERROR: Exit code ${EXITCODE} , check the ouput for details."
exit 1
fi
}
#TARGET="/media/pi/PEN 64GB/raspios-preinstalled-`date "+%Y%m%d"`.img.gz"
TARGET=$1
if [ $# -eq 0 ]
then
log_message "Usage: $0 /path/to/myimage.img"
exit 1
fi
log_message "Creating system partition backup to ${TARGET} ..."
set -o pipefail
dd bs=4M if=/dev/mmcblk0 conv=sync,noerror | pv -rabept --size $(lsblk -b | grep mmcblk0 | grep "disk" | awk '{ print $4 }') > "${TARGET}"
check_errors $?
#dd bs=4M if=/dev/mmcblk0 conv=sync,noerror | pv -rabept --size $(lsblk -b | grep mmcblk0 | grep "disk" | awk '{ print $4 }') | gzip > "${TARGET}"
log_message "finished."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment