#!/bin/bash # 2010-12-06 tyler AUTHOR="tyler AT tolaris DOT com" NAME=$(basename $0) VERSION="1.1.2" DESC="Rename a BackupPC host." # defaults BACKUPPC_CONF_DIR="/etc/backuppc" BACKUPPC_CONF_FILE="$BACKUPPC_CONF_DIR/config.pl" BACKUPPC_HOSTS_FILE="$BACKUPPC_CONF_DIR/hosts" die() { OUTPUT=$@ [ -n "$OUTPUT" ] && printf "$OUTPUT\n" >&2 exit 1 } VersionAndExit() { printf "$NAME version $VERSION, by $AUTHOR\n" exit 0 } UsageAndExit() { if [ -n "$2" ] ; then printf "$2\n\n" ; fi printf "\ Usage: $NAME [options] OLDNAME NEWNAME\n\ $DESC\n\ Options:\n\ -n dry run (don't act)\n\ -v verbose\n\ -h display help\n\ -V display version\n\ " exit $1 } while getopts ":hVnv" options; do case $options in h ) UsageAndExit 0;; V ) VersionAndExit 0;; n ) echo "Dry run, no changes will be made." ; DRYRUN="-n";; v ) VERBOSE="-v";; * ) UsageAndExit 1 "Option unknown: -$OPTARG";; esac done if [ -f "$BACKUPPC_CONF_FILE" ] ; then BACKUPPC_TOPDIR=$(grep '$Conf{TopDir}' "$BACKUPPC_CONF_FILE" | sed "s/^.*= '//;s/';$//") else die "$BACKUPPC_CONF_FILE not found" fi # Clear $@ of switches shift $(($OPTIND - 1)) if [ -z "$1" -o -z "$2" ] ; then UsageAndExit 1 fi # sanity checks if [ \! -f "$BACKUPPC_HOSTS_FILE" -o \! -w "$BACKUPPC_HOSTS_FILE" -o \! -r "$BACKUPPC_HOSTS_FILE" ] ; then die "Cannot read or write to $BACKUPPC_HOSTS_FILE, or it is not a file. Aborting." fi if [ \! -d "$BACKUPPC_TOPDIR" -o \! -w "$BACKUPPC_TOPDIR" -o \! -r "$BACKUPPC_TOPDIR" ] ; then die "Cannot read or write to $BACKUPPC_TOPDIR, or it is not a directory. Aborting." fi grep -q "^$1[ ]" "$BACKUPPC_HOSTS_FILE" || die "$1 not found in $BACKUPPC_HOSTS_FILE. Aborting." grep -q "^$2[ ]" "$BACKUPPC_HOSTS_FILE" && die "$2 already exists in $BACKUPPC_HOSTS_FILE. Aborting." cd "$BACKUPPC_CONF_DIR" || die "Cannot change to $BACKUPPC_CONF_DIR" # hosts - doesn't sort file [ -n "$VERBOSE" ] && echo "Replacing $1 with $2 in $BACKUPPC_HOSTS_FILE" if [ -z "$DRYRUN" ] ; then cp -af "$BACKUPPC_HOSTS_FILE" "$BACKUPPC_HOSTS_FILE.old" sed -i "s/^$1[ ]/$2 /" "$BACKUPPC_HOSTS_FILE" || die "sed replacement failed on $BACKUPPC_HOSTS_FILE" fi # per-host configs [ -n "$VERBOSE" ] && echo "Renaming any per-host configuration files in $BACKUPPC_CONF_DIR" if [ -f "$1.pl" ] ; then rename $VERBOSE $DRYRUN "s/$1/$2/" "$1.pl" || die "Can't rename $BACKUPPC_CONF_DIR/$1.pl" fi if [ -f "$1.pl.old" ] ; then rename $VERBOSE $DRYRUN "s/$1/$2/" "$1.pl.old" || die "Can't rename $BACKUPPC_CONF_DIR/$1.pl.old" fi # backup directory [ -n "$VERBOSE" ] && echo "Renaming directory in $BACKUPPC_TOPDIR/pc" if [ -z "$DRYRUN" ] ; then mv $VERBOSE "$BACKUPPC_TOPDIR/pc/$1" "$BACKUPPC_TOPDIR/pc/$2" || die "Can't move $BACKUPPC_TOPDIR/pc/$i to $2" fi