rotate_functions 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Modalità "rotate"
  2. # Variabili e funzioni di supporto
  3. # a modalità rotate
  4. # Fai un backup compresso del db file
  5. # Assegna il nome del file di backup
  6. # alla variabile attesa come primo
  7. # argomento.
  8. bkcomp_dbfile(){
  9. local COMPBK_NAME="${SHIFT_FILE}.bk$(date +%F_%T).gz"
  10. gzip -c9 "$SHIFT_FILE" > "$COMPBK_NAME" 2>/dev/null ||\
  11. echoerr "Failed to backup shifts file ${SHIFT_FILE}."
  12. chgrp "$OPGROUP" "$COMPBK_NAME" &>/dev/null ||\
  13. echoerr "Failed to change group ownership of backed up shifts file $COMPBK_NAME to $OPGROUP."
  14. chmod g=wr,o= "$COMPBK_NAME" &>/dev/null ||\
  15. echoerr "Failed to change mode of backed up shifts file $COMPBK_NAME."
  16. eval $1="$COMPBK_NAME"
  17. return 0
  18. }
  19. # Chiede conferma della rotazione.
  20. ask_rotate(){
  21. echo -e "\n\e[0;33mWarning\e[0m: rotation of shifts file clears current shifts records."
  22. echo "This operation can only be reverted manually."
  23. echo "Proceed at your own risk."
  24. echo -n 'Enter "YES" to proceed, anything else to abort: '
  25. local ANSWER
  26. read ANSWER
  27. [ "$ANSWER" == "YES" ] ||\
  28. { echo -e "\nRotation aborted. You don't make me spin. :("; exit 0; }
  29. }
  30. # Ruota il file dei turni
  31. rotate_dbfile(){
  32. # Check esistenza
  33. check_dbfile exist ||\
  34. echoerr "There's nothing to rotate: $SHIFT_FILE does not exist."
  35. # Conferma
  36. ask_rotate
  37. # Backup
  38. local SHIFT_FILE_COMPBK
  39. bkcomp_dbfile SHIFT_FILE_COMPBK
  40. # Init nuovo file turni
  41. initialize_dbfile
  42. # Notifica
  43. echo -e "\n\e[0;32mRotation successful!\e[0m I feel dizzy. O.o"
  44. echo "Old shifts file backed up as ${SHIFT_FILE_COMPBK}."
  45. }