Re: mount: Operation not permitted

From: Ronald F. Guilmette <rfg_at_tristatelogic.com>
Date: Thu, 02 Dec 2021 11:54:59 UTC
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

pname='mkbackup'

rsync='rsync'
opts='-v -t -axHAXS --delete --delete-excluded --delete-before --fileflags --force-change --exclude-from='

try_to_backup_partition ()
{
  output_partition="/dev/gpt/backup-$1"

  if [ \! -e $output_partition ] ; then
    return 0
  fi

  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`
  log="/root/backup-logs/$1/$now"

  echo $pname: mount $output_partition $output_directory 1>&2
  if mount $output_partition $output_directory ; then
    echo $pname: $rsync $opts$excludes $input_directory/ $output_directory \> $log 1>&2
    if $rsync $opts$excludes $input_directory/ $output_directory 2>&1 >$log; then
      sync
      echo $pname: gzip $log 1>&2
      gzip $log
      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
    df -k $input_directory $output_directory
    sync
    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
  sync
  echo $pname: Success backing up "$input_directory" to "$output_directory" 1>&2
  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
date