NASutils Knowledge BaseUnofficialPolski

Bash · projectA backup wizard with dialog

This script is a backup wizard in four windows: you tick share folders, confirm, watch a gauge, and read how many files and KiB a backup would copy. It is a dry run: rsync -n lists the files, and nothing is copied. The windows come from dialog, which TurboShell brings; QTS has no dialog of its own.

Programming on the NAS › Bash in 30 lessons

 NAS backup wizard
 ──────────────────────────────────────────────────────────────────────────────



        ┌────────────────────── Share folders ─────────────────────────┐
        │ Space ticks a folder, Enter goes on.                         │
        │ The backup goes to backup.                                   │
        │ ┌──────────────────────────────────────────────────────────┐ │
        │ │         [*] Public      wizard/shares/Public             │ │
        │ │         [ ] Multimedia  wizard/shares/Multimedia         │ │
        │ │         [*] Web         wizard/shares/Web                │ │
        │ │                                                          │ │
        │ │                                                          │ │
        │ │                                                          │ │
        │ └──────────────────────────────────────────────────────────┘ │
        ├──────────────────────────────────────────────────────────────┤
        │                 <  OK  >         <Cancel>                    │
        └──────────────────────────────────────────────────────────────┘
The checklist with Public and Web ticked; the folder homes of the sample has no path that exists, so it is missing.
 NAS backup wizard
 ──────────────────────────────────────────────────────────────────────────────



        ┌───────────────────────── Confirm ────────────────────────────┐
        │ Check what a backup of these folders would copy?             │
        │                                                              │
        │   Public Web                                                 │
        │                                                              │
        │ to backup                                                    │
        │                                                              │
        │ This is a dry run (rsync -n): nothing is copied.             │
        │                                                              │
        │                                                              │
        ├──────────────────────────────────────────────────────────────┤
        │                 <Check >         <Cancel>                    │
        └──────────────────────────────────────────────────────────────┘
The confirmation with the two folders, the destination and the note that nothing is copied.
 NAS backup wizard
 ──────────────────────────────────────────────────────────────────────────────



        ┌─────────────────────── Dry run done ─────────────────────────┐
        │ rsync -n would copy to backup:                               │
        │                                                              │
        │   Folder         Files      Size                             │
        │   Public             3        51 KiB                         │
        │   Web                1         1 KiB                         │
        │                                                              │
        │ Nothing was copied. To copy, remove -n in the rsync line.    │
        │                                                              │
        │                                                              │
        │                                                              │
        ├──────────────────────────────────────────────────────────────┤
        │                           <  OK  >                           │
        └──────────────────────────────────────────────────────────────┘
The summary: Public would copy 3 files of 51 KiB, Web 1 file of 1 KiB.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/bash/wizard.sh

wizard.sh Download wizard.sh

#!/usr/bin/env bash
# wizard.sh: a backup wizard made with dialog. Pick share folders in a checklist, confirm, watch a gauge while
# rsync -n checks each folder, and read in a message box what a real backup would copy. It copies nothing.
# Run it: bash wizard.sh [DESTINATION]   (default /share/Backup; SMB_CONF may point at a sample smb.conf)
set -uo pipefail

conf=${SMB_CONF:-/etc/config/smb.conf}
dest=${1:-/share/Backup}
title="NAS backup wizard"
export NCURSES_NO_UTF8_ACS=1 # frames drawn with Unicode lines instead of the terminal's line set
DIALOGRC=$(mktemp) && export DIALOGRC
result=$(mktemp)
trap 'rm -f "$DIALOGRC" "$result"; clear' EXIT
cat >"$DIALOGRC" <<'EOF'
use_shadow = OFF
screen_color = (WHITE,BLACK,OFF)
dialog_color = (WHITE,BLACK,OFF)
title_color = (YELLOW,BLACK,ON)
border_color = (YELLOW,BLACK,OFF)
border2_color = (YELLOW,BLACK,OFF)
menubox_border_color = (YELLOW,BLACK,OFF)
menubox_border2_color = (YELLOW,BLACK,OFF)
button_active_color = (BLACK,YELLOW,ON)
button_inactive_color = (WHITE,BLACK,OFF)
button_key_active_color = (BLACK,YELLOW,ON)
button_key_inactive_color = (YELLOW,BLACK,ON)
button_label_active_color = (BLACK,YELLOW,ON)
button_label_inactive_color = (WHITE,BLACK,ON)
menubox_color = (WHITE,BLACK,OFF)
item_color = (WHITE,BLACK,OFF)
item_selected_color = (BLACK,YELLOW,ON)
check_color = (WHITE,BLACK,OFF)
tag_color = (YELLOW,BLACK,ON)
tag_selected_color = (BLACK,YELLOW,ON)
tag_key_color = (YELLOW,BLACK,ON)
tag_key_selected_color = (BLACK,YELLOW,ON)
check_selected_color = (BLACK,YELLOW,ON)
gauge_color = (YELLOW,BLACK,ON)
EOF

# the share folders: each [Name] section of smb.conf with a path to a folder that exists
names=() paths=() section=''
while IFS= read -r line; do
  case $line in
    \[*\]) section=${line#[} section=${section%]} ;;
    path\ =\ *)
      path=${line#path = }
      [[ $section != global && -d $path ]] && names+=("$section") && paths+=("$path")
      ;;
  esac
done <"$conf"

items=()
for i in "${!names[@]}"; do items+=("${names[i]}" "${paths[i]}" off); done
picked=$(dialog --stdout --separate-output --backtitle "$title" --title " Share folders " \
  --checklist "Space ticks a folder, Enter goes on.\nThe backup goes to $dest." 14 64 6 "${items[@]}") || exit 0
[[ -n $picked ]] || exit 0
mapfile -t picked <<<"$picked" # one folder a line, so names may hold spaces

dialog --backtitle "$title" --title " Confirm " --yes-label "Check" --no-label "Cancel" \
  --no-collapse --yesno "Check what a backup of these folders would copy?\n\n  ${picked[*]}\n\nto $dest\n\nThis is a dry run (rsync -n): nothing is copied." 13 64 || exit 0

# the gauge reads numbers from the loop; between XXX lines it also gets a new text
for i in "${!picked[@]}"; do
  name=${picked[i]}
  for j in "${!names[@]}"; do [[ ${names[j]} == "$name" ]] && path=${paths[j]}; done
  printf 'XXX\n%d\nChecking %s ...\nXXX\n' $((i * 100 / ${#picked[@]})) "$name"
  files=0 bytes=0
  while read -r size file; do # rsync -n prints "SIZE NAME" for each file it would copy; folders end in /
    [[ $file == */ ]] && continue
    files=$((files + 1)) bytes=$((bytes + size))
  done < <(rsync -a -n --out-format='%l %n' "$path/" "$dest/$name/" 2>/dev/null)
  printf '  %-14s %5d %9d KiB\n' "$name" "$files" $(((bytes + 1023) / 1024)) >>"$result"
  sleep 0.4 # long enough to read the gauge
done | dialog --backtitle "$title" --title " Checking " --gauge "Starting ..." 8 64 0

table=$(<"$result")
dialog --backtitle "$title" --title " Dry run done " --no-collapse --msgbox \
  "rsync -n would copy to $dest:\n\n  Folder         Files      Size\n${table//$'\n'/\\n}\n\nNothing was copied. To copy, remove -n in the rsync line." 14 64

What you learn

  • dialog --checklist returns the ticked entries; --stdout --separate-output gives one per line to mapfile.
  • The exit status of dialog tells OK (0) from Cancel or Esc, so || exit 0 ends the wizard on Cancel.
  • A gauge reads percentages from a pipe; text between two XXX lines replaces its message.
  • A file named in DIALOGRC sets dialog's colours, here yellow frames and buttons on black.
  • rsync -a -n --out-format='%l %n' prints the size and the name of each file a real run would copy.

How it works

The script first reads QTS's /etc/config/smb.conf. Each share folder there is a section such as [Public] with a line path = /share/CACHEDEV1_DATA/Public. The script keeps the sections whose path is a folder that exists, and skips [global]. In the test, SMB_CONF points at a sample file whose paths lead into a small sample folder.

The checklist shows each share with its path. After Enter, a yes/no window names the ticked folders and the destination and says that this is a dry run. "Check" goes on, "Cancel" ends the script.

Then a loop runs rsync -n for each ticked folder and adds up the files and bytes from its output, skipping the lines of folders, which end in /. Before each folder it prints a percentage and a message in the XXX form, and the pipe takes them to dialog --gauge. The loop runs in a pipeline, so it writes its table into a temporary file, and the last window shows that file. The trap removes the temporary files and clears the screen when the script ends.

Notes to the lines

  • 10NCURSES_NO_UTF8_ACS=1 makes dialog draw its frames with Unicode lines, which every UTF-8 terminal shows.
  • 11–13Two temporary files, the colour file and the table, and the trap that removes them.
  • 14–39The colour file: each line names a part of the window and its colours (letters, background, bold).
  • 43–51[Name] lines set the section, path = lines give its folder; -d keeps only folders that exist.
  • 55–56The checklist: each entry is three words, the tag, the text and off, its start state.
  • 60–61--yes-label and --no-label rename the buttons; --no-collapse keeps the spaces in the text.
  • 67Percentage and message for the gauge, between XXX lines.
  • 69–72The dry run of one folder; $dest/$name/ is where a real run would put it.
  • 75Everything the loop prints goes to the gauge.
  • 77–79${table//$'\n'/\\n} turns the newlines of the table into \n, which dialog reads as line breaks.

Run it

bash wizard.sh /share/Backup

Space ticks a folder, the arrow keys move, Enter goes on, Esc or Cancel ends the wizard. To make a real backup, remove -n from the rsync line; rsync then copies the folders into the destination.

Try it yourself

Add --inputbox before the checklist to ask for the destination, with /share/Backup as its start text. Show the free space of the destination in the confirmation, from df -k.

More projects in Bash