Backup with Hanoi StrategyShell script to assist with branch management for tasks in gitFizz having an argument with BuzzScript to record an inventory of a backupReplacing values in json file with jqGoodgame Empire coin collector with random offsets in (almost-POSIX) Bash

Happy pi day, everyone!

Where is the 1/8 CR apprentice in Volo's Guide to Monsters?

How is the Swiss post e-voting system supposed to work, and how was it wrong?

How to explain that I do not want to visit a country due to personal safety concern?

Does this property of comaximal ideals always holds?

Know when to turn notes upside-down(eighth notes, sixteen notes, etc.)

Bash: What does "masking return values" mean?

What is this large pipe coming out of my roof?

How to generate globally unique ids for different tables of the same database?

When do we add an hyphen (-) to a complex adjective word?

Employee lack of ownership

Make a transparent 448*448 image

Making a sword in the stone, in a medieval world without magic

Instead of Universal Basic Income, why not Universal Basic NEEDS?

Is having access to past exams cheating and, if yes, could it be proven just by a good grade?

How do I hide Chekhov's Gun?

Calculus II Professor will not accept my correct integral evaluation that uses a different method, should I bring this up further?

Why are there 40 737 Max planes in flight when they have been grounded as not airworthy?

Possible Leak In Concrete

Sword in the Stone story where the sword was held in place by electromagnets

It's a yearly task, alright

How to deal with taxi scam when on vacation?

Does the statement `int val = (++i > ++j) ? ++i : ++j;` invoke undefined behavior?

Life insurance that covers only simultaneous/dual deaths



Backup with Hanoi Strategy


Shell script to assist with branch management for tasks in gitFizz having an argument with BuzzScript to record an inventory of a backupReplacing values in json file with jqGoodgame Empire coin collector with random offsets in (almost-POSIX) Bash













0












$begingroup$


I want your suggestions about this script. I use it to backup configuration of softwares installed on my home media server.



What improvements would you do?



thanks you for any inputs.



#!/bin/bash

# ==================================================================================
# Description: BackUp script following a Hanoi scheme using rsync.
# Author: Hugo Lapointe Di Giacomo
# ==================================================================================

set -o errexit
set -o nounset
#set -o xtrace

# ----------------------------------------------------------------------------------
# Options
# ----------------------------------------------------------------------------------
setsMax=16 # ................................ Maximum number of sets bakcup.
cmdPrefix="" # .............................. "echo" if dry-run enable, "" otherwise.
srcDir="/appdata" # ......................... Source directory to backup.
dstDir="/mnt/backup/snapshots" # ............ Destination directory to backup in.
logDir="/mnt/backup/logs" # ................. Logs directory.


# ----------------------------------------------------------------------------------
# Constants
# ----------------------------------------------------------------------------------
SEC_PER_DAY=86400; # ........................ Number of seconds per day.
LATEST_SNAP="$dstDir/latest"; # ............. The "latest" snap.
LATEST_LOG="$logDir/latest"; # .............. The "latest" log.
DATE_FORMAT="%Y-%m-%d"; # ................... The format of a date.
HOUR_FORMAT="%H:%M"; # ...................... The format of a hour.


# ----------------------------------------------------------------------------------
# Show usage of this script.
# ----------------------------------------------------------------------------------
function showUsage()
echo "Usage: $0 [OPTIONS]..."
echo ""
echo "Options available:"
echo " -h, --help Show usage of the script."
echo " -m, --max Maximum sets of backup."
echo " -s, --src Source directory to backup."
echo " -d. --dst Destination directory to backup in."
echo " -l, --log Log directory."
echo " -r, --dry Enable dry-run."



# ----------------------------------------------------------------------------------
# Parse arguments.
# ----------------------------------------------------------------------------------
function parseArgs() --help)
showUsage $@
exit 0
;;

-m


# ----------------------------------------------------------------------------------
# Create a backup with rsync.
# ----------------------------------------------------------------------------------
function createBackUp() tee "$logFile"


# ----------------------------------------------------------------------------------
# Update the latest links.
# ----------------------------------------------------------------------------------
function updateLatestLinks()
$cmdPrefix rm -f "$LATEST_SNAP"
$cmdPrefix ln -s "$snapDir" "$LATEST_SNAP"

$cmdPrefix rm -f "$LATEST_LOG"
$cmdPrefix ln -s "$logFile" "$LATEST_LOG"


# ----------------------------------------------------------------------------------
# Calculate the previous move in the cycle.
# https://en.wikipedia.org/wiki/Backup_rotation_scheme
# ----------------------------------------------------------------------------------
function calculateDaysFromBackupOfSameSet()
local todayInSec=$(date +%s)
local todayInDay=$(($todayInSec / $SEC_PER_DAY))

local daysToBackup=$((2 ** ($setsMax - 1)))

local daysElapsed=0
local i=1

while [ $i -lt $(($daysToBackup / 2)) ]; do
local rotation=$(($todayInDay & $i))

if [ $rotation -eq 0 ]; then
daysElapsed=$(($i * 2))
break
fi

daysElapsed=$(($daysToBackup / 2))
i=$((2 * $i))
done

echo $daysElapsed



# ----------------------------------------------------------------------------------
# Main function.
# ----------------------------------------------------------------------------------
function main()
parseArgs $@

local todayDatetime=$(date +$DATE_FORMAT.$HOUR_FORMAT)

if createBackUp $todayDatetime; then
updateLatestLinks

local daysElapsed=$(calculateDaysFromBackupOfSameSet)
local expiredDay=$(date -d "$daysElapsed days ago" +$DATE_FORMAT)
$cmdPrefix rm -frv "$dstDir/$expiredDay."*
fi


main $@








share









$endgroup$
















    0












    $begingroup$


    I want your suggestions about this script. I use it to backup configuration of softwares installed on my home media server.



    What improvements would you do?



    thanks you for any inputs.



    #!/bin/bash

    # ==================================================================================
    # Description: BackUp script following a Hanoi scheme using rsync.
    # Author: Hugo Lapointe Di Giacomo
    # ==================================================================================

    set -o errexit
    set -o nounset
    #set -o xtrace

    # ----------------------------------------------------------------------------------
    # Options
    # ----------------------------------------------------------------------------------
    setsMax=16 # ................................ Maximum number of sets bakcup.
    cmdPrefix="" # .............................. "echo" if dry-run enable, "" otherwise.
    srcDir="/appdata" # ......................... Source directory to backup.
    dstDir="/mnt/backup/snapshots" # ............ Destination directory to backup in.
    logDir="/mnt/backup/logs" # ................. Logs directory.


    # ----------------------------------------------------------------------------------
    # Constants
    # ----------------------------------------------------------------------------------
    SEC_PER_DAY=86400; # ........................ Number of seconds per day.
    LATEST_SNAP="$dstDir/latest"; # ............. The "latest" snap.
    LATEST_LOG="$logDir/latest"; # .............. The "latest" log.
    DATE_FORMAT="%Y-%m-%d"; # ................... The format of a date.
    HOUR_FORMAT="%H:%M"; # ...................... The format of a hour.


    # ----------------------------------------------------------------------------------
    # Show usage of this script.
    # ----------------------------------------------------------------------------------
    function showUsage()
    echo "Usage: $0 [OPTIONS]..."
    echo ""
    echo "Options available:"
    echo " -h, --help Show usage of the script."
    echo " -m, --max Maximum sets of backup."
    echo " -s, --src Source directory to backup."
    echo " -d. --dst Destination directory to backup in."
    echo " -l, --log Log directory."
    echo " -r, --dry Enable dry-run."



    # ----------------------------------------------------------------------------------
    # Parse arguments.
    # ----------------------------------------------------------------------------------
    function parseArgs() --help)
    showUsage $@
    exit 0
    ;;

    -m


    # ----------------------------------------------------------------------------------
    # Create a backup with rsync.
    # ----------------------------------------------------------------------------------
    function createBackUp() tee "$logFile"


    # ----------------------------------------------------------------------------------
    # Update the latest links.
    # ----------------------------------------------------------------------------------
    function updateLatestLinks()
    $cmdPrefix rm -f "$LATEST_SNAP"
    $cmdPrefix ln -s "$snapDir" "$LATEST_SNAP"

    $cmdPrefix rm -f "$LATEST_LOG"
    $cmdPrefix ln -s "$logFile" "$LATEST_LOG"


    # ----------------------------------------------------------------------------------
    # Calculate the previous move in the cycle.
    # https://en.wikipedia.org/wiki/Backup_rotation_scheme
    # ----------------------------------------------------------------------------------
    function calculateDaysFromBackupOfSameSet()
    local todayInSec=$(date +%s)
    local todayInDay=$(($todayInSec / $SEC_PER_DAY))

    local daysToBackup=$((2 ** ($setsMax - 1)))

    local daysElapsed=0
    local i=1

    while [ $i -lt $(($daysToBackup / 2)) ]; do
    local rotation=$(($todayInDay & $i))

    if [ $rotation -eq 0 ]; then
    daysElapsed=$(($i * 2))
    break
    fi

    daysElapsed=$(($daysToBackup / 2))
    i=$((2 * $i))
    done

    echo $daysElapsed



    # ----------------------------------------------------------------------------------
    # Main function.
    # ----------------------------------------------------------------------------------
    function main()
    parseArgs $@

    local todayDatetime=$(date +$DATE_FORMAT.$HOUR_FORMAT)

    if createBackUp $todayDatetime; then
    updateLatestLinks

    local daysElapsed=$(calculateDaysFromBackupOfSameSet)
    local expiredDay=$(date -d "$daysElapsed days ago" +$DATE_FORMAT)
    $cmdPrefix rm -frv "$dstDir/$expiredDay."*
    fi


    main $@








    share









    $endgroup$














      0












      0








      0





      $begingroup$


      I want your suggestions about this script. I use it to backup configuration of softwares installed on my home media server.



      What improvements would you do?



      thanks you for any inputs.



      #!/bin/bash

      # ==================================================================================
      # Description: BackUp script following a Hanoi scheme using rsync.
      # Author: Hugo Lapointe Di Giacomo
      # ==================================================================================

      set -o errexit
      set -o nounset
      #set -o xtrace

      # ----------------------------------------------------------------------------------
      # Options
      # ----------------------------------------------------------------------------------
      setsMax=16 # ................................ Maximum number of sets bakcup.
      cmdPrefix="" # .............................. "echo" if dry-run enable, "" otherwise.
      srcDir="/appdata" # ......................... Source directory to backup.
      dstDir="/mnt/backup/snapshots" # ............ Destination directory to backup in.
      logDir="/mnt/backup/logs" # ................. Logs directory.


      # ----------------------------------------------------------------------------------
      # Constants
      # ----------------------------------------------------------------------------------
      SEC_PER_DAY=86400; # ........................ Number of seconds per day.
      LATEST_SNAP="$dstDir/latest"; # ............. The "latest" snap.
      LATEST_LOG="$logDir/latest"; # .............. The "latest" log.
      DATE_FORMAT="%Y-%m-%d"; # ................... The format of a date.
      HOUR_FORMAT="%H:%M"; # ...................... The format of a hour.


      # ----------------------------------------------------------------------------------
      # Show usage of this script.
      # ----------------------------------------------------------------------------------
      function showUsage()
      echo "Usage: $0 [OPTIONS]..."
      echo ""
      echo "Options available:"
      echo " -h, --help Show usage of the script."
      echo " -m, --max Maximum sets of backup."
      echo " -s, --src Source directory to backup."
      echo " -d. --dst Destination directory to backup in."
      echo " -l, --log Log directory."
      echo " -r, --dry Enable dry-run."



      # ----------------------------------------------------------------------------------
      # Parse arguments.
      # ----------------------------------------------------------------------------------
      function parseArgs() --help)
      showUsage $@
      exit 0
      ;;

      -m


      # ----------------------------------------------------------------------------------
      # Create a backup with rsync.
      # ----------------------------------------------------------------------------------
      function createBackUp() tee "$logFile"


      # ----------------------------------------------------------------------------------
      # Update the latest links.
      # ----------------------------------------------------------------------------------
      function updateLatestLinks()
      $cmdPrefix rm -f "$LATEST_SNAP"
      $cmdPrefix ln -s "$snapDir" "$LATEST_SNAP"

      $cmdPrefix rm -f "$LATEST_LOG"
      $cmdPrefix ln -s "$logFile" "$LATEST_LOG"


      # ----------------------------------------------------------------------------------
      # Calculate the previous move in the cycle.
      # https://en.wikipedia.org/wiki/Backup_rotation_scheme
      # ----------------------------------------------------------------------------------
      function calculateDaysFromBackupOfSameSet()
      local todayInSec=$(date +%s)
      local todayInDay=$(($todayInSec / $SEC_PER_DAY))

      local daysToBackup=$((2 ** ($setsMax - 1)))

      local daysElapsed=0
      local i=1

      while [ $i -lt $(($daysToBackup / 2)) ]; do
      local rotation=$(($todayInDay & $i))

      if [ $rotation -eq 0 ]; then
      daysElapsed=$(($i * 2))
      break
      fi

      daysElapsed=$(($daysToBackup / 2))
      i=$((2 * $i))
      done

      echo $daysElapsed



      # ----------------------------------------------------------------------------------
      # Main function.
      # ----------------------------------------------------------------------------------
      function main()
      parseArgs $@

      local todayDatetime=$(date +$DATE_FORMAT.$HOUR_FORMAT)

      if createBackUp $todayDatetime; then
      updateLatestLinks

      local daysElapsed=$(calculateDaysFromBackupOfSameSet)
      local expiredDay=$(date -d "$daysElapsed days ago" +$DATE_FORMAT)
      $cmdPrefix rm -frv "$dstDir/$expiredDay."*
      fi


      main $@








      share









      $endgroup$




      I want your suggestions about this script. I use it to backup configuration of softwares installed on my home media server.



      What improvements would you do?



      thanks you for any inputs.



      #!/bin/bash

      # ==================================================================================
      # Description: BackUp script following a Hanoi scheme using rsync.
      # Author: Hugo Lapointe Di Giacomo
      # ==================================================================================

      set -o errexit
      set -o nounset
      #set -o xtrace

      # ----------------------------------------------------------------------------------
      # Options
      # ----------------------------------------------------------------------------------
      setsMax=16 # ................................ Maximum number of sets bakcup.
      cmdPrefix="" # .............................. "echo" if dry-run enable, "" otherwise.
      srcDir="/appdata" # ......................... Source directory to backup.
      dstDir="/mnt/backup/snapshots" # ............ Destination directory to backup in.
      logDir="/mnt/backup/logs" # ................. Logs directory.


      # ----------------------------------------------------------------------------------
      # Constants
      # ----------------------------------------------------------------------------------
      SEC_PER_DAY=86400; # ........................ Number of seconds per day.
      LATEST_SNAP="$dstDir/latest"; # ............. The "latest" snap.
      LATEST_LOG="$logDir/latest"; # .............. The "latest" log.
      DATE_FORMAT="%Y-%m-%d"; # ................... The format of a date.
      HOUR_FORMAT="%H:%M"; # ...................... The format of a hour.


      # ----------------------------------------------------------------------------------
      # Show usage of this script.
      # ----------------------------------------------------------------------------------
      function showUsage()
      echo "Usage: $0 [OPTIONS]..."
      echo ""
      echo "Options available:"
      echo " -h, --help Show usage of the script."
      echo " -m, --max Maximum sets of backup."
      echo " -s, --src Source directory to backup."
      echo " -d. --dst Destination directory to backup in."
      echo " -l, --log Log directory."
      echo " -r, --dry Enable dry-run."



      # ----------------------------------------------------------------------------------
      # Parse arguments.
      # ----------------------------------------------------------------------------------
      function parseArgs() --help)
      showUsage $@
      exit 0
      ;;

      -m


      # ----------------------------------------------------------------------------------
      # Create a backup with rsync.
      # ----------------------------------------------------------------------------------
      function createBackUp() tee "$logFile"


      # ----------------------------------------------------------------------------------
      # Update the latest links.
      # ----------------------------------------------------------------------------------
      function updateLatestLinks()
      $cmdPrefix rm -f "$LATEST_SNAP"
      $cmdPrefix ln -s "$snapDir" "$LATEST_SNAP"

      $cmdPrefix rm -f "$LATEST_LOG"
      $cmdPrefix ln -s "$logFile" "$LATEST_LOG"


      # ----------------------------------------------------------------------------------
      # Calculate the previous move in the cycle.
      # https://en.wikipedia.org/wiki/Backup_rotation_scheme
      # ----------------------------------------------------------------------------------
      function calculateDaysFromBackupOfSameSet()
      local todayInSec=$(date +%s)
      local todayInDay=$(($todayInSec / $SEC_PER_DAY))

      local daysToBackup=$((2 ** ($setsMax - 1)))

      local daysElapsed=0
      local i=1

      while [ $i -lt $(($daysToBackup / 2)) ]; do
      local rotation=$(($todayInDay & $i))

      if [ $rotation -eq 0 ]; then
      daysElapsed=$(($i * 2))
      break
      fi

      daysElapsed=$(($daysToBackup / 2))
      i=$((2 * $i))
      done

      echo $daysElapsed



      # ----------------------------------------------------------------------------------
      # Main function.
      # ----------------------------------------------------------------------------------
      function main()
      parseArgs $@

      local todayDatetime=$(date +$DATE_FORMAT.$HOUR_FORMAT)

      if createBackUp $todayDatetime; then
      updateLatestLinks

      local daysElapsed=$(calculateDaysFromBackupOfSameSet)
      local expiredDay=$(date -d "$daysElapsed days ago" +$DATE_FORMAT)
      $cmdPrefix rm -frv "$dstDir/$expiredDay."*
      fi


      main $@






      sh





      share












      share










      share



      share










      asked 4 mins ago









      hlapointehlapointe

      148216




      148216




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215473%2fbackup-with-hanoi-strategy%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215473%2fbackup-with-hanoi-strategy%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

          Why is a white electrical wire connected to 2 black wires?How to wire a light fixture with 3 white wires in box?How should I wire a ceiling fan when there's only three wires in the box?Two white, two black, two ground, and red wire in ceiling box connected to switchWhy is there a white wire connected to multiple black wires in my light box?How to wire a light with two white wires and one black wireReplace light switch connected to a power outlet with dimmer - two black wires to one black and redHow to wire a light with multiple black/white/green wires from the ceiling?Ceiling box has 2 black and white wires but fan/ light only has 1 of eachWhy neutral wire connected to load wire?Switch with 2 black, 2 white, 2 ground and 1 red wire connected to ceiling light and a receptacle?

          चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि