summaryrefslogtreecommitdiff
path: root/localbackupconfig
diff options
context:
space:
mode:
Diffstat (limited to 'localbackupconfig')
-rwxr-xr-xlocalbackupconfig125
1 files changed, 100 insertions, 25 deletions
diff --git a/localbackupconfig b/localbackupconfig
index ed745a5..43a33ad 100755
--- a/localbackupconfig
+++ b/localbackupconfig
@@ -1,9 +1,9 @@
#!/bin/sh
#
# /usr/local/sbin/localbackupconfig
-# Copyright 2001 Jonas Smedegaard <dr@jones.dk>
+# Copyright 2001-2007 Jonas Smedegaard <dr@jones.dk>
#
-# $Id: localbackupconfig,v 1.6 2002-10-17 17:20:56 jonas Exp $
+# $Id: localbackupconfig,v 1.7 2007-04-04 15:26:31 jonas Exp $
#
# .dpkg-new files appear when refusing to update a conffile on package update
#
@@ -11,36 +11,111 @@
#
# .old files are for customizing when you don't have the original (anymore)
#
-# .bak is... eh... just in case you felt like using that name now and then...
+# .bak and ~ is in case you forgot to rename properly as .orig or .old!
#
set -e
-FQDN=`hostname -f`
-BACKUPDIR=/var/local/backups/$FQDN
-LOCALCONFIGDIR=/etc/local-$FQDN
+PRG=$(basename "$0")
-if [ -e $LOCALCONFIGDIR ]; then
- mkdir -p $BACKUPDIR/$LOCALCONFIGDIR
- cp -fa $LOCALCONFIGDIR $BACKUPDIR/`dirname $LOCALCONFIGDIR`/
-fi
-changedfiles=`find /etc -name '*.bak' -o -name '*.old' -o -name '*.orig' -o -name '*.dpkg-new' | sed -e 's,\.\(bak\|old\|orig\|dpkg-new\)$,,' | grep -v "^$LOCALCONFIGDIR" | sort -u`
-for i in $changedfiles; do
- if [ -e $i ]; then
- if [ -e $i.dpkg-new ]; then
- orig="$i.dpkg-new"
- elif [ -e $i.orig ]; then
- orig="$i.orig"
- elif [ -e $i.old ]; then
- orig="$i.old"
- elif [ -e $i.bak ]; then
- orig="$i.bak"
+TEMP="`getopt -s sh -o t:f -l target:,force -n "$PRG" -- "$@"`"
+if [ $? != 0 ] ; then echo >&2 "ERROR: Internal getopt error." ; exit 1 ; fi
+eval set -- "$TEMP"
+
+targetdir=''
+force='no'
+while true ; do
+ case "$1" in
+ -t|--target) targetdir="$2" ; shift 2 ;;
+ -f|--force) force="yes" ; shift ;;
+ --) shift ; break ;;
+ *) echo >&2 "ERROR: Internal error resolving options." ; exit 1 ;;
+ esac
+done
+
+basedir="/etc"
+includefile="/etc/local/localtweaks.include"
+excludefile="/etc/local/localtweaks.exclude"
+extensions=".dpkg-new .orig .old .bak ~"
+
+if [ -n "$targetdir" ]; then
+ targetdirparent="$(dirname "$targetdir")"
+ if ! [ -d "$targetdirparent" ]; then
+ if [ "$force" = "yes" ]; then
+ mkdir -p "$targetdirparent"
else
- echo "What the f... Original for \"$i\" has disappeared?!?"
+ echo >&2 "Error: Parent directory for target directory does not exist."
exit 1
fi
- mkdir -p $BACKUPDIR/`dirname $i`
- cp -fa $i $BACKUPDIR/`dirname $i`/
- diff -ruN $orig $i > $BACKUPDIR/$i.diff || true
+ elif [ -e "$targetdir" ]; then
+ if [ "$force" = "yes" ]; then
+ rm -rf "$targetdir"
+ else
+ echo >&2 "Error: Target directory already exists."
+ exit 1
+ fi
+ fi
+fi
+
+tempdir="$(mktemp -td localtweaks.XXXXXX)"
+
+for ext in $extensions; do
+ extfiles="$(find $basedir -name "*$ext")"
+ for extfile in $extfiles; do
+ xextfile="${extfile%$ext}"
+ files="$files ${xextfile#$basedir}"
+ done
+done
+
+includes=""
+if [ -d "$(dirname "$includefile")" ] && [ -r "$includefile" ]; then
+ includes="$(cat "$includefile")"
+ for include in $includes; do
+ files="$files ${include#$basedir}"
+ done
+fi
+
+excludes=""
+if [ -d "$(dirname "$excludefile")" ] && [ -r "$excludefile" ]; then
+ xexcludes="$(cat "$excludefile")"
+ for exclude in $xexcludes; do
+ excludes="$excludes ${exclude#$basedir}"
+ done
+fi
+
+# process all files once each
+files="$(for x in $files; do echo "$x"; done | uniq | sort)"
+for file in $files; do
+
+ # file were chosen based on backups - it may not exist itelf
+ [ -s "$basedir/$file" ] || continue
+
+ for exclude in $excludes; do
+ [ "$file" = "$exclude" ] && continue 2
+ done
+
+ ext=""
+ for xext in $extensions; do
+ if [ -e $basedir/$file$xext ]; then
+ ext="$xext"
+ continue
+ fi
+ done
+
+ newdir="$tempdir/$basedir/$(dirname "$file")"
+ mkdir -p "$newdir"
+ cp -af "$basedir/$file" "$newdir"
+
+ # Only diff against existing and non-empty files.
+ if [ -n "$ext" ] && [ -s "$basedir/$file$ext" ]; then
+ diff -ruN "$basedir/$file$ext" "$basedir/$file" > "$tempdir/$basedir/$file.diff" || [ $? -lt 2 ]
+ chown --reference="$basedir/$file" "$tempdir/$basedir/$file.diff"
+ chmod --reference="$basedir/$file" "$tempdir/$basedir/$file.diff"
+ chmod a-x "$tempdir/$basedir/$file.diff"
fi
done
+
+if [ -n "$targetdir" ]; then
+ mv "$tempdir" "$targetdir"
+ rm -rf "$tempdir"
+fi