Announcement

Collapse
No announcement yet.

Automatic Backup Strategy - thoughts?

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    why is "/bin/ls" better than "stat"
    I have a vague feeling that stat is newish, as it wasn't a standard Unix command. Also, stat's man page describes the field as "time ... human-readable", whereas that for ls --full-time explicitly says ISO dates. See my earlier remarks about date formatting and pain. I see "human-readable" and think "subject to change".

    $(printf "%(%F)T" $(stat -Lc %Y "$newdistrosnap") )

    This I don't get either. Please explain "%(%F)T"
    printf here is a bash built-in that is like the C standard library printf, that all C programmers are all too familiar with. The bash printf has this extra sequence %(format)T where format is a strftime format, which many C and scripting language programmers have used. It's a way to get a date explicitly, definitely, formatted in ISO format, though has the huge drawback of being obscure. One would have to find it in the bash manual, which I often find confusingly structured. I like using https://www.gnu.org/software/bash/manual/bash.html where all the doc is on one long web page, so that page searches look through it all. That expression uses nested execution, something you can't do easily with the `...` construct.

    I figured out what you meant by "backticks" - I've not heard that word before and should have been more specific in my questions of that comment. You meant the "grave" key. You see, I speak "US English"...
    I thought "backtick" was US English. I think I first encountered that term reading about perl. I thought in ASCII it was some kind of quote character, though I read now that was a misuse of the grave accent. That name seems wrong to me because the accent isn't a standalone character, it always appears over another character, like a combining character in Unicode. Anyway, in shell scripts it has several problems, some of which are avoided by using $(...) instead; I've seen $(...) recommended over `...` often.
    Regards, John Little

    Comment


      #17
      This did not work, so I must be doing it wrong:
      Code:
      if [[ -d "$newdistrosnap" ]]; then
          if [[ (stat -c "%y" "$newdistrosnap" | cut -b 1-10) == "$today" ]]
          then
              exit 0
          fi
      fi
      output was:
      conditional binary operator expected
      expected `)'
      syntax error near `-c'
      ` if [[ (stat -c "%y" "$newdistrosnap" | cut -b 1-10) == "$today" ]]'

      but this totally works:
      Code:
      if [[ -d "$newdistrosnap" ]]; then
          if [[ `stat -c "%y" "$newdistrosnap" | cut -b 1-10` == "$today" ]]
          then
              exit 0
          fi
      fi
      Last edited by oshunluvr; Apr 18, 2018, 06:19 AM.

      Please Read Me

      Comment


        #18
        Here's the full script as it is right now. I added notification and incorporated some of what JLittle suggested:
        Code:
        #!/bin/bash
        
        # Log the script activity
        exec 1> >(logger -s -t $(basename $0)) 2>&1
        
        
        ## Set the variables
        ## Edit this section as needed
        distro_sub="@KDEneon"       # Distro subvolume to snapshot
        home_sub="@KDEneon_home"    # Home subvolume to snapshot
        source_mount="/subvol/"     # Root file system mount location
        addname="_daily_"           # This is added to the subvolume name of the snapshots
        oldsnapnum="7"              # Snapshot sequence number for removal
        
        
        ## Assemble the above variables into single variables and a few additional needed variables
        today=`date +%Y-%m-%d`                                                  # Today's date in a format that match the 'stat' command output below
        source_UUID=`lsblk -no UUID $source_device`                             # The UUID of the mount point in case it's needed
        distro_source="$source_mount""$distro_sub"                              # The root mount and distro subvolume name combined
        home_source="$source_mount""$home_sub"                                  # The root mount and home subvolume name combined
        newdistrosnap="$distro_source""$addname""1"                             # New snapshot names are the source name with 
        newhomesnap="$home_source""$addname""1"                                 # the contents of addname and 1 appended to the end 
        olddistrosnap="$distro_source""$addname""$oldsnapnum"                   # Distro subvolume to remove
        oldhomesnap="$home_source""$addname""$oldsnapnum"                       # Home subvolume to remove
        
        
        ## Begin process
        # Verify the root file system is mounted and mount it if not;
        if ! mountpoint -q "$source_mount"
            then 
                mount "$source_mount"
        fi
        
        
        # Has a snapshot already occurred today? Quit if yes
        if [[ -d "$newdistrosnap" ]]; then
            if [[ `stat -c "%y" "$newdistrosnap" | cut -b 1-10` == "$today" ]]
            then
                exit 0
            fi
        fi
        
        
        # Delete the oldest snapshot if it exists
        if [[ -d "$olddistrosnap" ]]
            then 
              btrfs su de -c "$olddistrosnap"
        fi
        
        
        # Roll the current snapshot names by adding 1 to each trailing number
        for i in {6..1} 
            do
                mv "$distro_source""$addname"$i "$distro_source""$addname"$(($i+1))
                mv "$home_source""$addname"$i "$home_source""$addname"$(($i+1))
            done
            
        # Take new read-write snapshots;
        btrfs su sn "$distro_source" "$newdistrosnap"
        btrfs su sn "$home_source" "$newhomesnap"
        
        
        # Touch these new snapshots to set the file date as today;
        touch "$newdistrosnap"
        touch "$newhomesnap"
        
        
        # Notify the user that the job is done
        su - $USER -c "notify-send -i package-install -t 0 'Daily snapshots:' 'Daily snapshot operation complete.'"
        
        
        # Script complete
        exit 0
        A couple things missing or need work:

        1) If a snapshot is missing from the sequence of seven, an error is generated (although harmless to function) and the missing snapshot is left out of the "roll". In other words, if @KDEneon_daily_2 is missing, the next day @KDEneon_daily_3 is missing, then ..._4, etc. This is not the end of the world as it wouldn't occur without manually deleting a snapshot - so the user would expect the error if they did that. I'm not sure at this point if this is a problem or actually the preferred behavior. One would assume if the user removed a snapshot manually, there would be a reason for it. I guess the actually preferred behavior might be to stop the "roll" of the snapshots at the missing number. So if ...daily_2 is missing, stop the roll there so the empty spot in the sequence is filled and the missing snapshot is replaced leaving seven remaining at all times. This shouldn't be hard.

        2) I'm not sure the notification is useful as-is if I run the script before log-in or as root rather than using sudo as I am now for testing.

        3) I haven't figured out to mount the root btrfs filesystem without having it defined in fstab. If it's in fstab, no problem, but how do I determine the UUID of a specific location without knowing the UUID or the device name? This is a problem because device names may change depending on your system at the time of boot - at least mine does. For know, I require the mount to be properly define in fstab for this script to work.

        Please Read Me

        Comment


          #19
          I might need help with this logic:

          If say, snapshot #3 is missing from the sequence, I want to:
          • Not delete #7
          • Not "roll" #6 through #4
          • Roll #2 to #3 and roll #1 to #2
          • Take a new #1

          This gets me back to seven snapshots.

          Seems I need to:
          • Determine if a snapshot is missing and it's number in the sequence
          • Skip the delete command of #7
          • Start the "roll" process at one number before the missing snapshot

          and then continue with the script.

          If the logic is correct, then I need to figure out how to determine the missing number. And what if more than one is missing? Ouch, my head is beginning to hurt...

          ...maybe I'll leave well enough alone!

          Please Read Me

          Comment


            #20
            Just quickly, chores beckon...
            Originally posted by oshunluvr View Post
            This did not work, so I must be doing it wrong:
            Code:
            ...
             if [[ (stat -c "%y" "$newdistrosnap" | cut -b 1-10) == "$today" ]]
            Yes, there needs to be a "$". `...` is replaced with $(...).

            The existence test you've used is "-d", a directory exists. I suppose the btrfs subvolume snapshot commands always use a directory, so that's appropriate.
            Regards, John Little

            Comment


              #21
              Originally posted by jlittle View Post
              Just quickly, chores beckon...

              Yes, there needs to be a "$". `...` is replaced with $(...).

              The existence test you've used is "-d", a directory exists. I suppose the btrfs subvolume snapshot commands always use a directory, so that's appropriate.
              Thanks, and correct. At the user level a subvolume appears acts as a directory and is detected as such. Think of it as a directory with REALLY AWESOME features! LOL

              In fact, rather like the mountpoint¹ command, this helps me in my scripts, because if I attempt to act on a file with btrfs subvolume actions, it obviously fails. So the directory check is a great sanity check.



              ¹The mountpoint command tests a folder to see if something is mounted there or not. A quick and clean way to verify your desired file system is actually mounted before sending files to it.

              Please Read Me

              Comment


                #22
                Please consider the NUMBER OF TOTAL HOURS involved in this thread.

                Let us say...fifty hours total.

                with a minimum wage averaged betwen a "dipariging term" country and the U.S. let us say... $2.00 USD per hour...

                or one hundred dollars USD

                purchase a five (5) dollar USD usb pen drive...

                plug it in

                wen saving "something" save the default path to the pen drive.

                it will, thereafterr, save to the pen drive...

                Get your girl/boy friend and tell she or he that in between chasing her/him around the couch to end up on the fauz fur rug you will have to drag and drop your files on the USB to...

                an external hard drive...

                average cost of an external HD in the U.S. that is less than "terrabytes"...is...

                dunno...fifty bucks USD...

                take she or he to a computer store and purchase said drive and then go to a "bistro" of choice...

                go back to the apartment or house and make out on the couch...

                next moring drag the stuff on the USB to the HD...

                ummmm Dolphin will ASK ..."do you want to replace"...

                if you are not TOO addled from the night before make a decision...

                and guess what...

                NO HACKER will get "at" that HD...

                and you get to make out on the faux rug!

                woodlikesfauxrugsandchampagnesmoke

                Comment


                  #23
                  Woody, wake up! You’re day-dreaming again!
                  "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                  – John F. Kennedy, February 26, 1962.

                  Comment

                  Working...
                  X