Created
January 17, 2022 21:10
-
-
Save Justin42/f71f75bceb12878717e27985573769b7 to your computer and use it in GitHub Desktop.
Backup some eMMC devices. Default device set matches NanoPC T-4 running Armbian Bullseye from SD Card. Uses dd, gzip.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| backtitle="Armbian EMMC backup script, http://www.armbian.com | Author: nopnop2002, Justin42" | |
| title="Backup from EMMC to Image File" | |
| logfile="/tmp/dd.log" | |
| imgdir="/var/images" | |
| sleeptime=1 | |
| debugout="/tmp/debug.log" | |
| emmc_devices=("mmcblk2boot0" "mmcblk2boot1" "mmcblk2") | |
| function DebugEcho() { | |
| if [ $1 = '-n' ]; then | |
| echo -n $2 >> ${debugout} | |
| else | |
| echo $1 >> ${debugout} | |
| fi | |
| } | |
| # Get copied size | |
| function GetCopied() { | |
| pid=`ps -ef | grep "dd if=" | grep -v grep | awk '{print $2}'` | |
| # echo -n "pid=" | |
| # echo $pid | |
| kill -USR1 $pid | |
| # tail -1 /tmp/dd.log | |
| # echo -n "/tmp/dd.log=" | |
| if [ -s ${logfile} ]; then | |
| # tail -1 /tmp/dd.log | awk '{print $1}' | |
| # copied=`tail -1 /tmp/dd.log | awk '{print $1}'` | |
| copied=`tail -1 ${logfile} | awk '{print $1}'` | |
| else | |
| copied=0 | |
| fi | |
| # DebugEcho -n "copied=" | |
| # DebugEcho $copied | |
| } | |
| # Get progress | |
| function GetProgress() { | |
| DebugEcho -n "copied=" | |
| DebugEcho $copied | |
| DebugEcho -n "emmcbytes=" | |
| DebugEcho $emmcbytes | |
| progress=`expr $(($copied * 100 / $emmcbytes))` | |
| if [ ${progress} -gt 100 ]; then | |
| progress=100 | |
| fi | |
| } | |
| # Check root user | |
| if [ "$UID" -ne 0 ]; then | |
| dialog --title "$title" --backtitle "$backtitle" \ | |
| --infobox "\nMust be root" 5 60 | |
| exit 1 | |
| fi | |
| begin_date=`date +%s` | |
| # Process Devices | |
| for device in "${emmc_devices[@]}"; do | |
| imgfile="${imgdir}/${device}.img.gz" | |
| # Check EMMC partition | |
| emmc=`lsblk -ablp | grep -m 1 -c "$device.*disk"` | |
| if [ $emmc == 0 ]; then | |
| dialog --title "$title" --backtitle "$backtitle" \ | |
| --infobox "\n/dev/$device not found" 5 60 | |
| exit 1 | |
| fi | |
| # Check image file exist | |
| if [ -f ${imgfile} ]; then | |
| dialog --title "$title" --backtitle "$backtitle" \ | |
| --yes-label "Ok" --no-label "No" --yesno \ | |
| "\n${imgfile} is already exist. purge ok?" 7 60 | |
| if [ $? -eq 1 ]; then exit 1; fi | |
| rm ${imgfile} | |
| fi | |
| # Get EMMC size | |
| emmcblksize=`stat /dev/${device} --file-system --format %S` | |
| emmcblks=`stat /dev/${device} --file-system --format %b` | |
| emmcbytes=`expr $(($emmcblks * $emmcblksize))` | |
| readblksize=`stat /dev/${device} --file-system --format %s` | |
| emmcmegabyte=`expr $(($emmcbytes / 125000))` | |
| # Create working directory | |
| if [ ! -d $imgdir ]; then | |
| mkdir $imgdir | |
| fi | |
| # Delete log file | |
| if [ -f ${logfile} ]; then | |
| rm ${logfile} | |
| fi | |
| # Start dd with background | |
| #(dd if=/dev/$device bs=$readblksize | 7zr a -bd -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on -si ${imgfile}) &> /tmp/dd.log & | |
| (dd if=/dev/$device bs=$readblksize | gzip > $imgfile) &> /tmp/dd.log & | |
| # Display progress screen | |
| active=1 | |
| while [ $active == 1 ] | |
| do | |
| # ps -ef | grep "dd if=" | grep -v grep | wc -l | |
| active=`ps -ef | grep "dd if=" | grep -v grep | wc -l` | |
| DebugEcho -n "active=" | |
| DebugEcho $active | |
| if [ $active == 1 ]; then | |
| GetCopied | |
| GetProgress | |
| DebugEcho -n "progress=" | |
| DebugEcho $progress | |
| DebugEcho -n "device=" | |
| DebugEcho $device | |
| speed=`tail -1 /tmp/dd.log | awk '{split($0,i,","); printf "%s %s\n", i[3], i[4]}'` | |
| echo $progress | dialog --title "$title" --backtitle "$backtitle" \ | |
| --gauge "\n\nCreating EMMC backup image (${emmcmegabyte} Mb). Please wait!\n/dev/${device} -> $imgfile\n$speed" 10 80 | |
| fi | |
| sleep ${sleeptime} | |
| done | |
| done | |
| # Finish | |
| end_date=`date +%s` | |
| total=`expr $end_date - $begin_date` | |
| min=`expr $total / 60` | |
| sec=`expr $total % 60` | |
| dialog --title "$title" --backtitle "$backtitle" \ | |
| --msgbox "\nAll done.Elapsed time (${min}Min.${sec}Sec.)" 7 60 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment