Re: mount: Operation not permitted
- In reply to: Ronald F. Guilmette: "Re: mount: Operation not permitted"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 03 Dec 2021 04:42:58 UTC
On 12/2/21 3:54 AM, Ronald F. Guilmette wrote: > In message <db098159-02d2-22a5-bbd7-3b81d870c59c@gmail.com>, > Graham Perrin <grahamperrin@gmail.com> wrote: > >> On 29/11/2021 21:33, Ronald F. Guilmette wrote: >>> I have a shell script … >> >> Can you attach it? > > I can append it. See below. I am not sure how much sense this will > make without a lot of context however. Suffice it to say that I have a > number of partitions/filesystems on my primary drive, and that there is > an exactly corresponding number of partitions/filesystems on each of > the (2) drives that I use to make rotating backups on this primary drive > and that all of these (backup drive) partitions/filesystems have been > nicely pre-labeled by me with nice GPT labels so that they will all show up > underneath /dev/gpt/ when I have either of the two backup drives actually > installed into my hot-swap rack, ready to be used to perform a backup. > > The "sleep 4" command and the two "sync"s around it are recent additions to > this script. > > I always and only ever run this script as root and only from ~root. > > The script is designed and intended to fail gracefully if I have failed > for any reason to insert one of my actual primary-drive backup drives > into my hot-swap rack. (I don't want to go overwriting the Wrong output > partition/filesystem after all!) > > P.S. Yes, I even back up /tmp. Please do not laugh or give me a hard > time about that. I do occasionally have stuff in there that I may need > to keep around, at least for awhile. > > > ----------------------------------------------------------------------- > #!/usr/local/bin/bash I prefer to use the standard shell (/bin/sh). I set errexit and nounset at the top of my scripts. > pname='mkbackup' I set a variable with basename and $0 at the top of my scripts: basename=$(basename $0) > rsync='rsync' I would eliminate this variable, or use an absolute path. > opts='-v -t -axHAXS --delete --delete-excluded --delete-before --fileflags --force-change --exclude-from=' -t is unnecessary: -a includes it. --delete is unnecessary: --delete-before includes it. > try_to_backup_partition () I decompose my overall solution into multiple scripts, each as simple as possible, and organize them all into an invocation dependency hierarchy. This makes it easier to test. (My shell scripting has not progressed to the point of libraries of functions; with matching test suites.) > { > output_partition="/dev/gpt/backup-$1" > > if [ \! -e $output_partition ] ; then > return 0 > fi Is it necessary to escape the exclamation mark? I would put double quotes around $output_partition in the test condition. I echo a warning message if the destination directory is missing. > input_directory="$2" > > output_directory="/backup-mnt/$1" > > excludes="/root/excludes/$1.txt" > > if [ \! -e "$excludes" ] ; then > echo $pname: $excludes: Missing excludes file 1>&2 > return 1 > fi > > now=`date +%Y%m%d%H%M%S` I put a dash between the day and hour. > log="/root/backup-logs/$1/$now" > > echo $pname: mount $output_partition $output_directory 1>&2 I prefer to set xtrace, let the shell do the echoing, and then unset xtrace. I have yet to find a way to not echo the unset xtrace command. > if mount $output_partition $output_directory ; then I would put a sleep(1) invocation after the above line and before the next line. > echo $pname: $rsync $opts$excludes $input_directory/ $output_directory \> $log 1>&2 > if $rsync $opts$excludes $input_directory/ $output_directory 2>&1 >$log; then I would put the '--exclude-file=$excludes' into $opt. I typically include $@ in the primary invocation so that I can add options during testing (such as -n, --stats, --progress, etc.). Missing and/or mismatched trailing directory separators (slashes) on rsync(1) sources and destinations make me confused. Your $input_directory has a trailing slash, but your $output_directory does not. I assume you are doing this to create the destination directory if it does not exist? I tend to create the directory explicitly, and use the trailing slash on both the source and destination. Is it necessary to escape the greater than symbol? I do not understand the redirection '\> $log 1>&2'. Please clarify. > sync Have you found that sync(8) is required here and below? > echo $pname: gzip $log 1>&2 > gzip $log I would pipe the rsync(1) output through gzip(1) and create $log.gz, unless the redirection prevents it. > sync > else > echo $pname: rsync failure: $output_partition $output_directory 1>&2 > echo $pname: umount $output_directory 1>&2 > umount $output_directory > return 2 > fi I run commands directly and let errexit stop the script if an error occurs. > df -k $input_directory $output_directory I would limit the script to rsync(1), and write another script for df(1). > sync I would put another sleep(1) invocation after the above line and before the next line. > echo $pname: umount $output_directory 1>&2 > if umount $output_directory ; then > true > else > echo $pname: Umount failure: $output_partition $output_directory 1>&2 > return 2 > fi > else > echo $pname: Mount failure: $output_partition $output_directory 1>&2 > return 2 > fi > sync > sleep 4 I would put the 4 into a variable at the top of the script. If you add the other sleep(1) commands, re-tune the time(s). > sync > echo $pname: Success backing up "$input_directory" to "$output_directory" 1>&2 I would add a --verbose option and a conditional around that message, the date(1) invocations (below), etc. > return 0 > } > > date > try_to_backup_partition root / > try_to_backup_partition var /var > try_to_backup_partition varftp /var/ftp > try_to_backup_partition tmp /tmp > try_to_backup_partition usr /usr > try_to_backup_partition home /home > try_to_backup_partition v /v I would put this information into a configuration file and make the script data-driven. This would facilitate testing and/or multiple scripts. > date David