pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


Quantron backup script, 3rd generation
Posted by Anonymous on Wed 15th Jan 2020 07:19
raw | new post
modification of post by Anonymous (view diff)

  1. #!/bin/bash
  2.  
  3. export PATH='/sbin:/usr/sbin:/bin:/usr/bin'
  4. typeset -r usbstick_mountpoint='/media/usb0'
  5.  
  6. typeset -r backupscript_version='V2.0.6'
  7.  
  8. printf '#\n'
  9. printf '# Quantron backup script STARTING...\n'
  10.  
  11. # stop codesys application
  12. # we assume this returns only after codesys is no longer running
  13. systemctl stop 'codesys3-rt.service'
  14.  
  15. # send all output to the quantron framebuffer console
  16. export TERM='linux'
  17. exec 0<>'/dev/tty0' 1>&0 2>&0
  18. clear
  19.  
  20. # NASTY HACK:
  21. # systemd/udev has a timeout running which will send SIGKILL to
  22. # all processes in a cggroup. Since this backup script is interactive
  23. # the only way to avoid getting killed by that timeout is to move
  24. # the current shell process into another cggroup.
  25. # This hack is WRONG on so many levels that it hurts, but given
  26. # the time constraints and the stupidity how our system is currently
  27. # designed there was to other way.
  28. # We do this here, after redirecting output to the framebuffer terminal,
  29. # so that we can see whether this fails or not.
  30. printf '%d' $$ >'/sys/fs/cgroup/systemd/system/cgroup.procs'
  31.  
  32. color_printf()
  33. {
  34.         typeset color=$1
  35.         shift
  36.         typeset ttycmd=''
  37.  
  38.         case "$color" in
  39.                 'black')        ttycmd=$'\E[30m' ;;
  40.                 'red')          ttycmd=$'\E[31m' ;;
  41.                 'green')        ttycmd=$'\E[32m' ;;
  42.                 'yellow')       ttycmd=$'\E[33m' ;;
  43.                 'blue')         ttycmd=$'\E[34m' ;;
  44.                 'magenta')      ttycmd=$'\E[35m' ;;
  45.                 'cyan')         ttycmd=$'\E[36m' ;;
  46.                 'white')        ttycmd=$'\E[37m' ;;
  47.         esac
  48.  
  49.         printf '%s' "$ttycmd"
  50.         printf "$@"
  51.         printf '%s' $'\E[0m'
  52. }
  53.  
  54.  
  55. is_usbstick_mounted()
  56. {
  57.         grep -q -E \
  58.                 "[[:space:]]+${usbstick_mountpoint}[[:space:]]+(fat|vfat|ntfs|ext4)[[:space:]]+(rw|ro)," \
  59.                 '/proc/self/mounts'
  60.         return $?
  61. }
  62.  
  63. disable_console_screensaver()
  64. {
  65.         # make sure the framebuffer console screenblanker is NOT active
  66.         printf '0\n' >'/sys/devices/platform/imx21-fb.0/graphics/fb0/blank'
  67.  
  68.         # disable screen blanking (we have to do this via terminal codes
  69.         # since /sys/module/kernel/parameters/consoleblank is read-only)
  70.         printf '\E[9;0]'
  71.  
  72.         return 0
  73. }
  74.  
  75. do_menu()
  76. {
  77.         typeset key
  78.  
  79.         #
  80.         # main menu
  81.         #
  82.         printf '\n#\n'
  83.         printf '# Welcome to the '
  84.         color_printf 'red' 'QUANTRON'
  85.         printf ' /var/dyn\n# backup tool (version %s)!\n' "${backupscript_version}"
  86.         printf '#\n\n'
  87.  
  88.         color_printf 'yellow' ' * Menu:\n'
  89.         color_printf 'yellow' ' - Press <ESC> key for abort\n\t(system shutdown)\n'     # key="A"
  90.         color_printf 'yellow' ' - Press <T> key for backup\n'                           # key="C"
  91.         color_printf 'yellow' ' - Press <L%%/R%%> key for restore\n'                    # key="D"
  92.  
  93.         for ((;;)) ; do
  94.                 disable_console_screensaver
  95.  
  96.                 key=''
  97.  
  98.                 #
  99.                 # wait for a key press...
  100.                 #
  101.                 # (warning: Quantron V3.1.9 has bash3.2.48 which does
  102.                 # NOT support -N, only -n is supported!)
  103.                 #
  104.                 IFS='' read -e -r -u0 -n1 key
  105.  
  106.                 case "$key" in
  107.                         'A')
  108.                                 color_printf 'cyan' '#### Aborting, system shutting down...\n'
  109.                                 sleep 3
  110.                                 reboot -p
  111.                                 exit 0
  112.                                 ;;
  113.                         'C')
  114.                                 color_printf 'cyan' '#### Starting backup...\n'
  115.                                 sleep 3
  116.                                 do_backup
  117.                                 exit 0
  118.                                 ;;
  119.                         'D')
  120.                                 color_printf 'cyan' '#### Starting restore...\n'
  121.                                 sleep 3
  122.                                 do_restore
  123.                                 exit 0
  124.                                 ;;
  125.                         *)
  126.                                 # beep ?
  127.                                 ;;
  128.                 esac
  129.         done
  130.  
  131.         # notreached
  132. }
  133.  
  134. do_backup()
  135. {
  136.         typeset -i backuperr=0
  137.  
  138.         if ! is_usbstick_mounted ; then
  139.                 color_printf 'red' '# ERROR: No USB stick mounted, aborting\n'
  140.                 return 1
  141.         fi
  142.  
  143.         if ! /sbin/eag_usbstick mount_writeable ; then
  144.                 color_printf 'red' '# ERROR: Cannot mount stick writeable, aborting\n'
  145.                 return 1
  146.         fi
  147.  
  148.         mkdir -p "${usbstick_mountpoint}/quantron_backup"
  149.         rm -f "${usbstick_mountpoint}/quantron_backup/quantron_var_dyn_backup.tar.bz2.md5sum"
  150.  
  151.         color_printf 'cyan' '\n# Saving files...\n'
  152.         set -o pipefail
  153.  
  154.         (cd / && tar -cvf - var/dyn/* ) | \
  155.                 bzip2 -9 | \
  156.                 tee \
  157.                         >(md5sum '/dev/stdin' | sed 's/\/dev\/stdin/quantron_var_dyn_backup.tar.bz2/' >"${usbstick_mountpoint}/quantron_backup/quantron_var_dyn_backup.tar.bz2.md5sum") \
  158.                         >"${usbstick_mountpoint}/quantron_backup/quantron_var_dyn_backup.tar.bz2"
  159.         (( backuperr+=$? ))
  160.  
  161.         sync ; sync # UNIX tradition!
  162.  
  163.         /sbin/eag_usbstick mount_readonly
  164.         (( backuperr+=$? ))
  165.  
  166.         # verifying backup
  167.         color_printf 'cyan' '\n# Verifying backup data...\n'
  168.         (cd "${usbstick_mountpoint}/quantron_backup" && md5sum -c 'quantron_var_dyn_backup.tar.bz2.md5sum')
  169.         if (( $? != 0 )) ; then
  170.                 (( backuperr++ ))
  171.                 color_printf 'red' '# ERROR: Wrong MD5 hash sum of written data\n'
  172.  
  173.                 # remove backup archive so the user cannot restore broken data
  174.                 /sbin/eag_usbstick mount_writeable >'/dev/null' 2>&1
  175.                 rm -f \
  176.                         "${usbstick_mountpoint}/quantron_backup/quantron_var_dyn_backup.tar.bz2" \
  177.                         "${usbstick_mountpoint}/quantron_backup/quantron_var_dyn_backup.tar.bz2.md5sum"
  178.                 /sbin/eag_usbstick mount_readonly >'/dev/null' 2>&1
  179.         fi
  180.  
  181.         if (( backuperr == 0 )) ; then
  182.                 color_printf 'green' '\n#\n# Backup success.\n#\n'
  183.         else
  184.                 color_printf 'red' '\n#\n# Backup FAILED!!\n#\n'
  185.         fi
  186.  
  187.         disable_console_screensaver
  188.  
  189.         color_printf 'yellow' '\n\nPlease remove USB stick and reboot...\n'
  190.         return $(( backuperr?1:0 ))
  191. }
  192.  
  193. do_restore()
  194. {
  195.         typeset -i restoreerr=0
  196.  
  197.         if [[ ! -r "${usbstick_mountpoint}/quantron_backup/quantron_var_dyn_backup.tar.bz2" ]] ; then
  198.                 color_printf 'red' '# ERROR: Cannot read backup file %s\n' "${usbstick_mountpoint}/quantron_backup/quantron_var_dyn_backup.tar.bz2"
  199.                 return 1
  200.         fi
  201.  
  202.         color_printf 'cyan' '\n# Verifying backup data...\n'
  203.         (cd "${usbstick_mountpoint}/quantron_backup" && md5sum -c 'quantron_var_dyn_backup.tar.bz2.md5sum')
  204.         if (( $? != 0 )) ; then
  205.                 color_printf 'red' '# ERROR: Backup archive does not have the correct hash sum.\n'
  206.                 color_printf 'red' '\n#\n# Restore FAILED!!\n#\n'
  207.                 return 1
  208.         fi
  209.  
  210.         color_printf 'cyan' '\n# Restoring files...\n'
  211.         set -o pipefail
  212.         bunzip2 -c <"${usbstick_mountpoint}/quantron_backup/quantron_var_dyn_backup.tar.bz2" |
  213.                 (cd / && tar -xvf -)
  214.         (( restoreerr+=$? ))
  215.  
  216.         sync ; sync # UNIX tradition!
  217.  
  218.         if (( restoreerr == 0 )) ; then
  219.                 color_printf 'green' '\n#\n# Restore success.\n#\n'
  220.         else
  221.                 color_printf 'red' '\n#\n# Restore FAILED!!\n#\n'
  222.         fi
  223.  
  224.         disable_console_screensaver
  225.  
  226.         color_printf 'yellow' '\n\nPlease remove USB stick and reboot...\n'
  227.         return $(( restoreerr?1:0 ))
  228. }
  229.  
  230. # main
  231. do_menu
  232.  
  233. # EOF.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at