summaryrefslogtreecommitdiff
path: root/localbackupconfig
blob: 2512f268f9d80fe86e223b25cb2ce6f2c7382e70 (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.9 2007-06-10 23:14:57 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. # TODO: Check md5sum of all conffiles found like this:
  17. # dpkg-query -W -f '${Conffiles}\n' '*' | grep -v '^$' | awk '{print $1}'
  18. #
  19. set -e
  20. PRG=$(basename "$0")
  21. TEMP="`getopt -s sh -o t:f -l target:,force -n "$PRG" -- "$@"`"
  22. if [ $? != 0 ] ; then echo >&2 "ERROR: Internal getopt error." ; exit 1 ; fi
  23. eval set -- "$TEMP"
  24. targetdir=''
  25. force='no'
  26. while true ; do
  27. case "$1" in
  28. -t|--target) targetdir="$2" ; shift 2 ;;
  29. -f|--force) force="yes" ; shift ;;
  30. --) shift ; break ;;
  31. *) echo >&2 "ERROR: Internal error resolving options." ; exit 1 ;;
  32. esac
  33. done
  34. basedir="/etc"
  35. includefile="/etc/local/localtweaks.include"
  36. excludefile="/etc/local/localtweaks.exclude"
  37. extensions=".dpkg-new .orig .old .bak ~"
  38. if [ -n "$targetdir" ]; then
  39. targetdirparent="$(dirname "$targetdir")"
  40. if ! [ -d "$targetdirparent" ]; then
  41. if [ "$force" = "yes" ]; then
  42. mkdir -p "$targetdirparent"
  43. else
  44. echo >&2 "Error: Parent directory for target directory does not exist."
  45. exit 1
  46. fi
  47. elif [ -e "$targetdir" ]; then
  48. if [ "$force" = "yes" ]; then
  49. rm -rf "$targetdir"
  50. else
  51. echo >&2 "Error: Target directory already exists."
  52. exit 1
  53. fi
  54. fi
  55. fi
  56. tempdir="$(mktemp -td localtweaks.XXXXXX)"
  57. for ext in $extensions; do
  58. extfiles="$(find $basedir -name "*$ext")"
  59. for extfile in $extfiles; do
  60. xextfile="${extfile%$ext}"
  61. files="$files ${xextfile#$basedir}"
  62. done
  63. done
  64. includes=""
  65. if [ -d "$(dirname "$includefile")" ] && [ -r "$includefile" ]; then
  66. includes="$(cat "$includefile")"
  67. for include in $includes; do
  68. files="$files ${include#$basedir}"
  69. done
  70. fi
  71. excludes=""
  72. if [ -d "$(dirname "$excludefile")" ] && [ -r "$excludefile" ]; then
  73. xexcludes="$(cat "$excludefile")"
  74. for exclude in $xexcludes; do
  75. excludes="$excludes ${exclude#$basedir}"
  76. done
  77. fi
  78. # process all files once each
  79. files="$(for x in $files; do echo "$x"; done | uniq | sort)"
  80. for file in $files; do
  81. # file were chosen based on backups - it may not exist itelf
  82. [ -s "$basedir/$file" ] || continue
  83. for exclude in $excludes; do
  84. [ "$file" = "$exclude" ] && continue 2
  85. done
  86. ext=""
  87. for xext in $extensions; do
  88. if [ -e $basedir/$file$xext ]; then
  89. ext="$xext"
  90. continue
  91. fi
  92. done
  93. newdir="$tempdir/$basedir/$(dirname "$file")"
  94. mkdir -p "$newdir"
  95. cp -af "$basedir/$file" "$newdir"
  96. # Only diff against existing and non-empty files.
  97. if [ -n "$ext" ] && [ -s "$basedir/$file$ext" ]; then
  98. diff -ruN -a "$basedir/$file$ext" "$basedir/$file" > "$tempdir/$basedir/$file.diff" || [ $? -lt 2 ]
  99. chown --reference="$basedir/$file" "$tempdir/$basedir/$file.diff"
  100. chmod --reference="$basedir/$file" "$tempdir/$basedir/$file.diff"
  101. chmod a-x "$tempdir/$basedir/$file.diff"
  102. fi
  103. done
  104. if [ -n "$targetdir" ]; then
  105. mv "$tempdir" "$targetdir"
  106. rm -rf "$tempdir"
  107. fi