s243a

update_save_back

Apr 29th, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #IFS=$'\0' #https://stackoverflow.com/questions/8677546/reading-null-delimited-strings-through-a-bash-loop
  3. #IFS='' #https://stackoverflow.com/questions/6563979/how-to-use-null-0-as-the-delimiter-in-gnu-sort
  4. #IFS='  '
  5. #IFS=''
  6. OUT="/tmp/Udate_save/"
  7.  
  8. mkdir -p "$OUT"
  9. decode_id() {
  10.   if [ -f "$OUT"'BLKIDOUT' ]; then
  11.      DECODE_ID_RTN=`cat "$OUT"'BLKIDOUT'`
  12.   else
  13.     BLKIDOUT="$(blkid)"
  14.     DECODE_ID_RTN="$(echo "$BLKIDOUT" | grep -m1 -E " LABEL=.${1}| UUID=.${1}" | cut -f1 -d: | cut -f3 -d/)" #is LABEL or UUID
  15.     echo "$DECODE_ID_RTN" > "$OUT"'BLKIDOUT'
  16.   fi
  17.   echo "$DECODE_ID_RTN"
  18. }
  19.  
  20. ###################### Initial Parmaters ############
  21.  
  22. #ROOT_UUI="fab3b011-746f-448b-85a3-5f7fb7f8e7f6" #sdd4 (Toshiba)
  23. ROOT_UUI="ebeda76b-c7e8-d201-a020-a46bc7e8d201" #sdd6
  24.  
  25.  
  26. #ROOT_REL_PATH  #Variable not yet defined
  27.  
  28. decode_id "$ROOT_UUI"
  29. SAVE_ROOT_DRV="$DECODE_ID_RTN"
  30. ROOT_REL_PATH="/tahrsave"
  31. SAVE_ROOT="/mnt/$SAVE_ROOT_DRV$ROOT_REL_PATH"
  32. RW_ROOT=`readlink /initrd/pup_rw`
  33.  
  34. #taken from line 222 if initrd/init tahrpup and trimmed extensively
  35.  
  36. ###################### Functions ############
  37.  
  38. save_dir_array(){ #Was bind_and_link_array in map_save
  39.   local -n one=${1} # https://stackoverflow.com/questions/16461656/how-to-pass-array-as-an-argument-to-a-function-in-bash
  40.   #declair -a one=$one1
  41.   if [ $# -lt 2 ]; then
  42.     SR="$SAVE_ROOT"
  43.   else
  44.     SR=$2
  45.   fi
  46.   one=${one[*]} #https://stackoverflow.com/questions/3348443/a-confusion-about-array-versus-array-in-the-context-of-a-bash-comple?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
  47.   one=${one#'('} #https://stackoverflow.com/questions/16623835/remove-a-fixed-prefix-suffix-from-a-string-in-bash?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
  48.   one=${one%')#'}
  49.   echo "Ln 49 one=${one[@]}"   
  50.   IFS_OLD=$IFS
  51.   IFS=$'\n'
  52.   for a in ${one[@]}; do
  53.      echo "LN#53 save_file $a"
  54.      save_file "$a" "$SR"
  55.   done
  56.   IFS=IFS_OLD
  57. }
  58.  
  59.  
  60. save_dir_files(){ #Was bind_and_link_dir in map_save
  61.   #local -n one=$1
  62.   if [ $# -lt 2 ]; then
  63.     SR="$SAVE_ROOT"
  64.   else
  65.     SR=$2
  66.   fi   
  67.   one="$1"
  68.   echo "LN#68 one=$one"  
  69.   [ ! `mountpoint -q "$one"` ] || return _
  70.   mkdir -p "$SAVE_ROOT$one"
  71.   DIRS2=( $( find $RW_ROOT$one -mindepth 1 -maxdepth 1 -print0 -name '*'  | removePrefix "$RW_ROOT" ) )#
  72.   echo "LN#72 DIRS2=${DIRS2[@]}"
  73.   save_dir_array DIRS2 $2
  74. }
  75. save_file(){ #Was bind_and_link in map_save
  76.   f="$1"
  77.   if [ $# -gt 1 ]; then
  78.     SR="$2"
  79.   else
  80.     SR="$SAVE_ROOT"
  81.   fi
  82.   echo "LN#82 save_file $RW_ROOT$f"
  83.   if [ -L "$f" ]; then #Don't copy if the link links back to the save file
  84.     [[ `readlink $f` =~ ^$SAVE_ROOT ]] && return _ #https://stackoverflow.com/questions/4132510/how-to-test-that-a-variable-starts-with-a-string-in-bash
  85.   elif [ -d "$RW_ROOT$f" ]; then
  86.     save_dir_files "$f"
  87.     return _
  88.   fi
  89.   #Copy only if knewer.
  90.   cp -a -u "$RW_ROOT$f" "$SR$f"  #we reversed the from to direction in copy from map_save
  91. }
  92. #echo $(dirname $(readlink -f "$fname")) # get directory name https://stackoverflow.com/questions/6121091/get-file-directory-path-from-file-path
  93. ######################### MAIN ###########################
  94. declare -a  dirs_to_link_and_bind=\
  95. ( "/etc" "/DEBIAN" "/bin" "/etc" "/lib" "/lib64" \
  96.    "/opt" "/root" "/sbin" "/var" "/usr" "/var" \
  97. )
  98.  
  99. for dir in ${dirs_to_link_and_bind[@]}; do
  100.   mkdir -p $SAVE_ROOT$dir
  101.   save_dir_files $dir
  102. done
Advertisement
Add Comment
Please, Sign In to add comment