I figured out a way to do this. It involves Tasker, but the bulk of the work is done by another 3rd-party utility. It's a module called TeMeFI. I'll explain below how it is used.
First of all, all settings can be queried and set by running "settings list", "settings system put", "settings global put", and "settings custom put". These commands can be run via the shell in Android.
The basic procedure for backing up settings is to run "settings list" and then reformat the results into a group of "settings [system/global/custom] put" statements, one for each setting, and then save those results into a script file on the Android device.
Then, to restore, the generated script is run in the Android shell.
These commands must be run via root.
It turns out that the TeMeFI utility can supply already implemented code for performing the backup procedure that I describe above, and that code can be queried and saved. I have done so, and I have saved that backup code as a script on my device.
TeMeFI is described and can be downloaded from here:
https://forum.xda-developers.com/t/app-tool-temefi-comprehensive-system-administration.3806260/The way I use Tasker for this is as follows:
(1) Get TeMeFI to generate the save-settings script, and then copy that code to a file which I call /sdcard/do-backup-settings.sh
(2) Write a shell script which runs "/system/bin/sh /sdcard/do-backup-settings.sh" and redirects its stdout output to a file called /sdcard/restore-settings.sh (because the script from TeMeFI writes its output to stdout). This restore-settings.sh file will contain the "settings ... put" commands for the current value of each device setting.
(3) Write a Tasker task called BackupSettings which uses "Run Shell" to run "/system/bin/sh /sdcard/backup-settings.sh" as root. This creates an up-to-date version of /sdcard/restore-settings.sh
(4) Write a Tasker task called RestoreSettings which uses "Run Shell" to run "/system/bin/sh /sdcard/restore-settings.sh" as root. This will restore all of the previously saved settings.
(5) Export each of BackupSettings and RestoreSettings as an app. Then, these apps can then be run to backup and restore the settings.