summaryrefslogtreecommitdiff
path: root/localbackupconfig
blob: 33bde0c19a87917e1695d08141c1d5d25a712f96 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/sbin/localbackupconfig
  4. # Copyright 2001-2007 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localbackupconfig,v 1.8 2007-05-30 18:45:45 jonas Exp $
  7. #
  8. # .dpkg-new files appear when refusing to update a conffile on package update
  9. #
  10. # .orig files you should save yourself before customizing a configfile
  11. #
  12. # .old files are for customizing when you don't have the original (anymore)
  13. #
  14. # .bak and ~ is in case you forgot to rename properly as .orig or .old!
  15. #
  16. set -e
  17. PRG=$(basename "$0")
  18. TEMP="`getopt -s sh -o t:f -l target:,force -n "$PRG" -- "$@"`"
  19. if [ $? != 0 ] ; then echo >&2 "ERROR: Internal getopt error." ; exit 1 ; fi
  20. eval set -- "$TEMP"
  21. targetdir=''
  22. force='no'
  23. while true ; do
  24. case "$1" in
  25. -t|--target) targetdir="$2" ; shift 2 ;;
  26. -f|--force) force="yes" ; shift ;;
  27. --) shift ; break ;;
  28. *) echo >&2 "ERROR: Internal error resolving options." ; exit 1 ;;
  29. esac
  30. done
  31. basedir="/etc"
  32. includefile="/etc/local/localtweaks.include"
  33. excludefile="/etc/local/localtweaks.exclude"
  34. extensions=".dpkg-new .orig .old .bak ~"
  35. if [ -n "$targetdir" ]; then
  36. targetdirparent="$(dirname "$targetdir")"
  37. if ! [ -d "$targetdirparent" ]; then
  38. if [ "$force" = "yes" ]; then
  39. mkdir -p "$targetdirparent"
  40. else
  41. echo >&2 "Error: Parent directory for target directory does not exist."
  42. exit 1
  43. fi
  44. elif [ -e "$targetdir" ]; then
  45. if [ "$force" = "yes" ]; then
  46. rm -rf "$targetdir"
  47. else
  48. echo >&2 "Error: Target directory already exists."
  49. exit 1
  50. fi
  51. fi
  52. fi
  53. tempdir="$(mktemp -td localtweaks.XXXXXX)"
  54. for ext in $extensions; do
  55. extfiles="$(find $basedir -name "*$ext")"
  56. for extfile in $extfiles; do
  57. xextfile="${extfile%$ext}"
  58. files="$files ${xextfile#$basedir}"
  59. done
  60. done
  61. includes=""
  62. if [ -d "$(dirname "$includefile")" ] && [ -r "$includefile" ]; then
  63. includes="$(cat "$includefile")"
  64. for include in $includes; do
  65. files="$files ${include#$basedir}"
  66. done
  67. fi
  68. excludes=""
  69. if [ -d "$(dirname "$excludefile")" ] && [ -r "$excludefile" ]; then
  70. xexcludes="$(cat "$excludefile")"
  71. for exclude in $xexcludes; do
  72. excludes="$excludes ${exclude#$basedir}"
  73. done
  74. fi
  75. # process all files once each
  76. files="$(for x in $files; do echo "$x"; done | uniq | sort)"
  77. for file in $files; do
  78. # file were chosen based on backups - it may not exist itelf
  79. [ -s "$basedir/$file" ] || continue
  80. for exclude in $excludes; do
  81. [ "$file" = "$exclude" ] && continue 2
  82. done
  83. ext=""
  84. for xext in $extensions; do
  85. if [ -e $basedir/$file$xext ]; then
  86. ext="$xext"
  87. continue
  88. fi
  89. done
  90. newdir="$tempdir/$basedir/$(dirname "$file")"
  91. mkdir -p "$newdir"
  92. cp -af "$basedir/$file" "$newdir"
  93. # Only diff against existing and non-empty files.
  94. if [ -n "$ext" ] && [ -s "$basedir/$file$ext" ]; then
  95. diff -ruN -a "$basedir/$file$ext" "$basedir/$file" > "$tempdir/$basedir/$file.diff" || [ $? -lt 2 ]
  96. chown --reference="$basedir/$file" "$tempdir/$basedir/$file.diff"
  97. chmod --reference="$basedir/$file" "$tempdir/$basedir/$file.diff"
  98. chmod a-x "$tempdir/$basedir/$file.diff"
  99. fi
  100. done
  101. if [ -n "$targetdir" ]; then
  102. mv "$tempdir" "$targetdir"
  103. rm -rf "$tempdir"
  104. fi