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.
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> │ └──────────────────────────────────────────────────────────────┘
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> │ └──────────────────────────────────────────────────────────────┘
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 > │ └──────────────────────────────────────────────────────────────┘
Download it on the NAS
curl -fsSLO https://kb.nasutils.net/projects/bash/wizard.shwizard.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 --checklistreturns the ticked entries;--stdout --separate-outputgives one per line tomapfile.- The exit status of
dialogtells OK (0) from Cancel or Esc, so|| exit 0ends the wizard on Cancel. - A gauge reads percentages from a pipe; text between two
XXXlines replaces its message. - A file named in
DIALOGRCsets 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
- 10
NCURSES_NO_UTF8_ACS=1makes 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;-dkeeps only folders that exist. - 55–56The checklist: each entry is three words, the tag, the text and
off, its start state. - 60–61
--yes-labeland--no-labelrename the buttons;--no-collapsekeeps the spaces in the text. - 67Percentage and message for the gauge, between
XXXlines. - 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/BackupSpace 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
- A framed menu in pure BashA Bash script draws a menu in a double frame, moves a highlighted bar with the arrow keys and opens boxes with the NAS's memory, volumes, uptime and load. You learn cursor moves, colours and key reading without any extra tool.
- A greeting at the SSH loginA Bash script greets you by the time of day and shows the NAS model, uptime, CPU, load, the free space of the data volumes and a tip of the day in one framed banner. You learn to frame text with escapes and how to show it at every SSH login on QTS.
- A live dashboard of the NASA Bash script redraws the CPU, load, temperatures, memory, data volumes and network rates of the NAS every 2 seconds until you press q. You learn to wait for a key with a timeout and to compute a rate from two readings of a counter.
- Blink: a memory game with drive-bay LEDsIn this Bash game the four drive-bay LEDs of a drawn NAS light up in a sequence that grows by one each round, and you repeat it with the keys 1 to 4. You learn timed drawing, seeded random numbers and how to ignore keys typed at the wrong moment.
- Copy a folder with a progress barA Bash script copies a folder block by block and shows a progress bar, the bytes copied, the speed and the time left, with an optional speed limit. You learn traps for Ctrl+C, redrawing lines in place and reading keys while a job runs.
- A backup wizard with dialogA Bash script uses dialog to show a checklist of the NAS's share folders, a confirmation, a progress gauge and a summary of what rsync would copy. You learn dialog's boxes, its colour file and how to feed a gauge from a loop.