small-package/luci-app-amlogic/root/usr/sbin/openwrt-ddbr

146 lines
5.5 KiB
Plaintext
Raw Normal View History

2022-03-25 12:20:39 +08:00
#!/bin/bash
#===========================================================================
2022-05-26 23:45:45 +08:00
#
2022-03-25 12:20:39 +08:00
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
#
# This file is a part of the make OpenWrt for Amlogic s9xxx tv box
# https://github.com/ophub/luci-app-amlogic
#
# Description: Backup and restore the system in emmc
# Copyright (C) 2017- The function borrowed from armbian/ddbr, Author: xXx
# Copyright (C) 2021- https://github.com/unifreq/openwrt_packit
# Copyright (C) 2021- https://github.com/ophub/luci-app-amlogic
#
# Command: openwrt-ddbr
2022-05-26 23:45:45 +08:00
#
#========================= Set default parameters ===========================
#
2022-03-25 12:20:39 +08:00
# Set font color
blue_font_prefix="\033[34m"
purple_font_prefix="\033[35m"
green_font_prefix="\033[32m"
yellow_font_prefix="\033[33m"
red_font_prefix="\033[31m"
font_color_suffix="\033[0m"
INFO="[${blue_font_prefix}INFO${font_color_suffix}]"
STEPS="[${purple_font_prefix}STEPS${font_color_suffix}]"
SUCCESS="[${green_font_prefix}SUCCESS${font_color_suffix}]"
OPT="[${yellow_font_prefix}OPT${font_color_suffix}]"
ERROR="[${red_font_prefix}ERROR${font_color_suffix}]"
2022-05-26 23:45:45 +08:00
#
# Check the output path
out_path="/ddbr"
2022-03-25 12:20:39 +08:00
# File name for backup/restore
ddbr_image="BACKUP-arm-64-emmc.img.gz"
# Need remaining space, unit: GB
need_space="2"
2022-05-26 23:45:45 +08:00
#
#===========================================================================
2022-03-25 12:20:39 +08:00
2022-05-26 23:45:45 +08:00
# Encountered a serious error, abort the script execution
error_msg() {
echo -e "${ERROR} ${1}"
2022-03-25 12:20:39 +08:00
exit 1
2022-05-26 23:45:45 +08:00
}
2022-03-25 12:20:39 +08:00
2022-05-26 23:45:45 +08:00
# Check emmc
do_checkemmc() {
# Get device name
mydevice_name="$(cat /proc/device-tree/model | tr -d '\000')"
echo -e "${INFO} The device name: [ ${mydevice_name} ]"
2022-03-25 12:20:39 +08:00
2022-05-26 23:45:45 +08:00
# Find the partition where root is located
root_ptname="$(df / | tail -n1 | awk '{print $1}' | awk -F '/' '{print $3}')"
2024-01-30 16:20:22 +08:00
if [[ -z "${root_ptname}" ]]; then
root_ptname="$(df /overlay | tail -n1 | awk '{print $1}' | awk -F '/' '{print $3}')"
fi
2022-05-26 23:45:45 +08:00
[[ -z "${root_ptname}" ]] && error_msg "Cannot find the partition corresponding to the root file system!"
2022-03-25 12:20:39 +08:00
2022-12-26 20:16:47 +08:00
# Find the EMMC drive
emmc="$(lsblk -l -o NAME | grep -oE "mmcblk[0-9]boot0" | sort | uniq | sed "s/boot0//g")"
# Find emmc disk, find emmc that does not contain the boot0 partition
[[ -z "${emmc}" ]] && emmc="$(lsblk -l -o NAME | grep -oE '(mmcblk[0-9]?)' | grep -vE ^${root_ptname:0:-2} | uniq)"
# Check if emmc exists
[[ -z "${emmc}" ]] && error_msg "The eMMC storage not found in this device!"
# Show the emmc name
echo -e "${INFO} The device eMMC name: [ /dev/${emmc} ]"
2022-05-26 23:45:45 +08:00
# Find the disk where the partition is located, only supports mmcblk?p? sd?? hd?? vd?? and other formats
case "${root_ptname}" in
mmcblk?p[1-4])
disk_name="$(echo ${root_ptname} | awk '{print substr($1, 1, length($1)-2)}')"
if lsblk -l -o NAME | grep "${disk_name}boot0" >/dev/null; then
error_msg "The current system is running on emmc. Please perform backup/restore operation in [ SD/TF/USB ]!"
fi
link_ptname="p"
;;
[hsv]d[a-z][1-4])
disk_name="$(echo ${root_ptname} | awk '{print substr($1, 1, length($1)-1)}')"
link_ptname=""
;;
2022-12-26 20:16:47 +08:00
nvme?n?p[1-4])
disk_name="$(echo ${root_ptname} | awk '{print substr($1, 1, length($1)-2)}')"
link_ptname="p"
;;
2022-05-26 23:45:45 +08:00
*)
error_msg "Unable to recognize the disk type of ${root_ptname}!"
;;
esac
# Set check parameters
out_path="/mnt/${disk_name}${link_ptname}4"
dev_intsize="$(fdisk -s /dev/${emmc})"
[[ -z "$(echo "${dev_intsize}" | sed -n "/^[0-9]\+$/p")" ]] && error_msg "Unable to get EMMC size." && exit 1
echo -e "${INFO} The device EMMC size: [ $(($dev_intsize / 1024 / 1024))GB ]"
# check directory
[[ -d "${out_path}" ]] || mkdir -p ${out_path}
echo -e "${INFO} The ddbr file path: [ ${out_path}/${ddbr_image} ]\n"
}
2022-03-25 12:20:39 +08:00
# Check the remaining space
do_checkspace() {
2022-12-28 20:17:27 +08:00
remaining_space="$(df -Tk ${out_path} | grep '/dev/' | awk '{print $5}' | echo $(($(xargs) / 1024 / 1024)))"
2022-05-26 23:45:45 +08:00
if [[ -z "$(echo "${remaining_space}" | sed -n "/^[0-9]\+$/p")" ]]; then
error_msg "The path is not available, the remaining space cannot be obtained."
2022-03-25 12:20:39 +08:00
fi
if [[ "${remaining_space}" -lt "${need_space}" ]]; then
2022-05-26 23:45:45 +08:00
error_msg "The remaining space is [ ${remaining_space} ] Gb, and more than [ ${need_space} ] Gb space is required."
2022-03-25 12:20:39 +08:00
fi
}
# Backup the emmc system
do_backup() {
echo -e "${STEPS} Start to backup the system in emmc."
do_checkspace
echo -e "Saving and Compressing [ /dev/${emmc} ] to [ ${out_path}/${ddbr_image} ], Please wait..."
rm -f ${out_path}/${ddbr_image} 2>/dev/null && sync
dd if=/dev/${emmc} | pv -s ${dev_intsize}"K" | gzip >${out_path}/${ddbr_image}
2022-05-26 23:45:45 +08:00
[[ "${?}" -eq "0" ]] && sync && echo -e "${SUCCESS} Backup is complete."
2022-03-25 12:20:39 +08:00
}
# Restore the emmc system
do_restore() {
echo -e "${STEPS} Start to restore the system in emmc."
2022-05-26 23:45:45 +08:00
[[ ! -f ${out_path}/${ddbr_image} ]] && error_msg "The [ ${out_path}/${ddbr_image} ] File not found." && exit 1
2022-03-25 12:20:39 +08:00
echo -e "Restoring [ ${out_path}/${ddbr_image} ] to [ /dev/${emmc} ], Please wait..."
gunzip -c ${out_path}/${ddbr_image} | pv -s ${dev_intsize}"K" | dd of=/dev/${emmc}
2022-05-26 23:45:45 +08:00
[[ "${?}" -eq "0" ]] && sync && echo -e "${SUCCESS} Restore is complete."
2022-03-25 12:20:39 +08:00
}
echo -e "${STEPS} Welcome to use the EMMC system backup/restore service."
2022-05-26 23:45:45 +08:00
# Check script permission
2022-09-15 20:26:22 +08:00
[[ -x "/usr/sbin/openwrt-ddbr" ]] || error_msg "Please grant execution permission: [ chmod +x /usr/sbin/openwrt-ddbr ]"
2022-05-26 23:45:45 +08:00
# Check emmc
do_checkemmc
2022-03-25 12:20:39 +08:00
# Prompt the user to select backup/restore
echo -ne "${OPT} Do you want to backup or restore? Backup=(b) Restore=(r): "
read br
2022-05-26 23:45:45 +08:00
case "${br}" in
2024-01-30 16:20:22 +08:00
b | B | backup) do_backup ;;
r | R | restore) do_restore ;;
*) exit 0 ;;
2022-03-25 12:20:39 +08:00
esac