container-file-auto-restore 781 B

1234567891011121314151617181920212223242526
  1. #!/usr/bin/env bash
  2. set -o pipefail ## trace ERR through pipes
  3. set -o errtrace ## trace ERR through 'time command' and other functions
  4. set -o nounset ## set -u : exit the script if you try to use an uninitialised variable
  5. set -o errexit ## set -e : exit the script if any statement returns a non-true return value
  6. if [[ "$#" -ne 1 ]]; then
  7. echo "Usage: $0 <file>"
  8. exit 1
  9. fi
  10. SOURCE_FILE="$1"
  11. BACKUP_FILE="$(dirname "$1")/.$(basename "$1").bak"
  12. if [[ -f "$BACKUP_FILE" ]]; then
  13. ## Backup file exists
  14. ## -> container was restarted
  15. ## -> restoring configuration
  16. cp -a -- "$BACKUP_FILE" "$SOURCE_FILE"
  17. else
  18. ## Backup file DOESN'T exists
  19. ## -> container first startup
  20. ## -> backup configuration
  21. cp -a -- "$SOURCE_FILE" "$BACKUP_FILE"
  22. fi