From 1e410a6d9574c56b7d3a3633b82220eacf782d06 Mon Sep 17 00:00:00 2001
From: Micah Anderson <micah@riseup.net>
Date: Tue, 24 May 2005 19:27:43 +0000
Subject: [PATCH] Updates to handle vservers

---
 README                     |  1 +
 backupninja                |  9 +++++
 etc/backup.d/example.mysql |  5 ++-
 etc/backup.d/example.svn   |  3 ++
 etc/backupninja.conf       |  4 ++
 handlers/mysql             | 78 ++++++++++++++++++++++++++++++--------
 handlers/rdiff             | 29 +++++++++++++-
 handlers/svn               | 47 ++++++++++++++++++-----
 handlers/sys               | 48 +++++++++++++++++++++--
 9 files changed, 194 insertions(+), 30 deletions(-)

diff --git a/README b/README
index 51e807ae..732a6b76 100644
--- a/README
+++ b/README
@@ -23,6 +23,7 @@ Features:
  - backup actions can be scheduled
  - you can choose when status report emails are mailed to you
    (always, on warning, on error, never).
+ - works with linux-vservers
  
 The following options are available:
 -h, --help           This usage message
diff --git a/backupninja b/backupninja
index 687571bf..a105a5bf 100755
--- a/backupninja
+++ b/backupninja
@@ -426,6 +426,10 @@ getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
 getconf MYSQLDUMP /usr/bin/mysqldump
 getconf GZIP /bin/gzip
 getconf RSYNC /usr/bin/rsync
+getconf vservers no
+getconf VSERVERINFO /usr/sbin/vserver-info
+getconf VSERVER /usr/sbin/vserver
+getconf VROOTDIR `$VSERVERINFO info SYSINFO |grep vserver-Rootdir | awk '{print $2}'`
 
 if [ ! -d "$configdirectory" ]; then
 	echo "Configuration directory '$configdirectory' not found."
@@ -439,6 +443,11 @@ if [ "$UID" != "0" ]; then
 	exit 1
 fi
 
+if [ "$VSERVERS" = "yes" -a ! -d $VROOTDIR ]; then
+        echo "vservers option set in config, but $VROOTDIR is not a directory!"
+	fatal "vservers option set in config, but $VROOTDIR is not a directory!"
+fi
+
 ## Process each configuration file
 
 # by default, don't make files which are world or group readable.
diff --git a/etc/backup.d/example.mysql b/etc/backup.d/example.mysql
index 1c08fd31..a8f9540d 100644
--- a/etc/backup.d/example.mysql
+++ b/etc/backup.d/example.mysql
@@ -5,7 +5,7 @@ databases	= all
 backupdir	= /var/backups/mysql
 hotcopy		= yes
 sqldump		= yes
-compress    = yes
+compress    	= yes
 
 #
 # user = <user>
@@ -44,4 +44,7 @@ compress    = yes
 # compress = < yes | no >
 # if yes, compress the sqldump output. 
 #
+# vsname = <vserver>
+# what vserver to operate on, only used if vserver = yes in /etc/backupninja.conf
+# if you do not specify a vsname the host will be operated on
 
diff --git a/etc/backup.d/example.svn b/etc/backup.d/example.svn
index 78919445..e8497069 100644
--- a/etc/backup.d/example.svn
+++ b/etc/backup.d/example.svn
@@ -18,3 +18,6 @@
 
 ## the hotbackup program to use.
 # HOTBACKUP = /usr/lib/subversion/hot-backup.py
+
+## the name of the vserver containing svn, if using vservers
+# vsname =
\ No newline at end of file
diff --git a/etc/backupninja.conf b/etc/backupninja.conf
index 6f432499..97af0628 100644
--- a/etc/backupninja.conf
+++ b/etc/backupninja.conf
@@ -43,3 +43,7 @@ usecolors = yes
 
 # default value for 'when'
 when = everyday at 01:00
+
+# if running vservers, set to yes
+VSERVERS = no
+
diff --git a/handlers/mysql b/handlers/mysql
index 6a8c3dd9..b552ddeb 100644
--- a/handlers/mysql
+++ b/handlers/mysql
@@ -11,25 +11,53 @@ getconf dbhost localhost
 getconf hotcopy no
 getconf sqldump no
 getconf user root
+getconf vsname
 
-# create backup dirs
+# If vservers are configured, decide if the handler should
+# use them or if it should just operate on the host
+if [ "$VSERVERS" = "yes" ]
+then
+	if [ ! -z $vsname ]
+	then		
+		info "Using vserver '$vsname'"
+		usevserver=1
+	else
+		info "No vserver name specified, actions will be performed on the host"
+	fi
+fi
 
-[ -d $backupdir ] || mkdir -p $backupdir
-[ -d $backupdir ] || fatal "Backup directory '$backupdir'"
+# Check to make sure that the specified vserver exists
+if [ $usevserver ]
+then
+	vroot="$VROOTDIR/$vsname"
+	[ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
+fi
+	
+# create backup dirs, the vroot variable will be empty if no vsname was specified
+# and will proceed to operate on the host
+[ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
+[ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
 hotdir="$backupdir/hotcopy"
 dumpdir="$backupdir/sqldump"
-[ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
-[ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
+
+if [ $usevserver ]
+then
+	[ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
+	[ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
+else
+	[ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
+	[ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
+fi
 
 # create .my.cnf
- # (we do this because we don't want to have to specify the password on the command line
- # because then anyone would be able to see it with a 'ps aux'. instead, we create a 
- # temporary ~/.my.cnf in root's home directory).
+# (we do this because we don't want to have to specify the password on the command line
+# because then anyone would be able to see it with a 'ps aux'. instead, we create a 
+# temporary ~/.my.cnf in root's home directory).
 
 if [ "$dbusername" != "" ]; then
-	home=`grep '^root:' /etc/passwd | awk -F: '{print $6}'`
+	home=`grep '^root:' $vroot/etc/passwd | awk -F: '{print $6}'`
 	[ -d $home ] || fatal "Can't find root's home directory ($home)."
-	mycnf="$home/.my.cnf"
+	mycnf="$vroot$home/.my.cnf"
 	if [ -f $mycnf ]; then
 		# rename temporarily
 		tmpcnf="$home/my.cnf.disable"
@@ -59,7 +87,12 @@ fi
 
 if [ "$hotcopy" == "yes" ]; then 
 	if [ "$databases" == "all" ]; then
-		execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
+		if [ $usevserver ]
+		then
+			execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
+		else
+			execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
+		fi
 		debug "su $user -c '$execstr'"
 		if [ ! $test ]; then
 			output=`su $user -c "$execstr" 2>&1`
@@ -74,7 +107,12 @@ if [ "$hotcopy" == "yes" ]; then
 		fi
 	else	
 		for db in $databases; do
-			execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
+			if [ $usevserver ]
+			then
+				execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
+			else
+				execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
+			fi
 			debug "su $user -c '$execstr'"
 			if [ ! $test ]; then
 				output=`su $user -c "$execstr" 2>&1`
@@ -95,11 +133,21 @@ fi
 
 if [ "$sqldump" == "yes" ]; then
 	if [ "$databases" == "all" ]; then
-		databases=`echo 'show databases' | su $user -c "$MYSQL" | grep -v Database`
+		if [ $usevserver ]
+		then
+			databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL" | grep -v Database`
+		else
+			databases=`echo 'show databases' | su $user -c "$MYSQL" | grep -v Database`
+		fi
 	fi
 
 	for db in $databases; do
-		execstr="$MYSQLDUMP --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
+		if [ $usevserver ]
+		then
+			execstr="$VSERVER $vsname exec $MYSQLDUMP --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $vroot$dumpdir/${db}.sql"
+		else
+			execstr="$MYSQLDUMP --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
+		fi
 		debug "su $user -c '$execstr'"
 		if [ ! $test ]; then
 			output=`su $user -c "$execstr" 2>&1`
@@ -115,7 +163,7 @@ if [ "$sqldump" == "yes" ]; then
 	done
 	
 	if [ "$compress" == "yes" ]; then
-		output=`$GZIP -f $dumpdir/*.sql 2>&1`
+		output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
 		debug $output
 	fi
 fi
diff --git a/handlers/rdiff b/handlers/rdiff
index 61eb7f5c..ca97fa4d 100644
--- a/handlers/rdiff
+++ b/handlers/rdiff
@@ -12,6 +12,7 @@ getconf type; sourcetype=$type
 getconf label
 getconf keep 60
 getconf include
+getconf vsinclude
 getconf exclude
 
 ### DESTINATION ###
@@ -24,6 +25,18 @@ getconf type; desttype=$type
 getconf user; destuser=$user
 getconf host; desthost=$host
 
+# See if vservers are configured
+if [ "$VSERVERS" = "yes" ]
+then
+	if [ ! -d $VROOTDIR ]
+	then
+		fatal "vservers enabled, but $VROOTDIR does not exist!"
+	else
+		info "vserver method enabled"
+		usevserver=1
+	fi
+fi
+
 [ "$destdir" != "" ] || fatal "Destination directory not set"
 
 if [ "$desttype" == "remote" ]; then
@@ -54,7 +67,8 @@ fi
 
 [ "$label" != "" ] || fatal "Source missing label"
 [ "$sourcetype" == "local" ] || fatal "Only local source type supported"
-[ "$include" != "" ] || fatal "No source includes specified"
+[ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
+#TODO should I test for vsinclude if usevservers=1?
 
 execstr_clientpart="/"
 	
@@ -101,6 +115,19 @@ for i in $include; do
 	execstr="${execstr}--include '$str' "
 done
 
+# vsinclude
+if [ $usevserver ] 
+then
+	for vserver in `ls $VROOTDIR|grep -v lost+found`
+	do
+		for vi in $vsinclude
+		do
+			str="${vi//__star__/*}"
+			execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
+		done
+	done
+fi
+
 # exclude everything else
 execstr="${execstr}--exclude '/*' "
 		
diff --git a/handlers/svn b/handlers/svn
index d0e36480..cd3cc2e9 100644
--- a/handlers/svn
+++ b/handlers/svn
@@ -6,42 +6,69 @@ getconf src /var/lib/svn
 getconf dest /var/backups/svn
 getconf tmp /var/backups/svn.tmp
 getconf HOTBACKUP /usr/lib/subversion/hot-backup.py
+getconf vsname	
 
 error=0
-cd $src
+
+# If vservers are configured, decide if the handler should
+# use them or if it should just operate on the host
+if [ "$VSERVERS" = "yes" ]
+then
+	if [ ! -z $vsname ]
+	then		
+		info "Using vserver '$vsname'"
+		usevserver=1
+	else
+		info "No vserver name specified, actions will be performed on the host"
+	fi
+fi
+
+# Check to make sure that the specified vserver exists
+if [ $usevserver ]
+then
+	vroot="$VROOTDIR/$vsname"
+	[ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
+fi
+
+cd $vroot$src
 for repo in `find . -name svnserve.conf`
 do
     repo=`dirname $repo`
     repo=`dirname $repo`
 
-    ret=`mkdir -p $tmp/$repo 2>&1`
+    ret=`mkdir -p $vroot$tmp/$repo 2>&1`
     code=$?
     if [ "$ret" ]; then
        debug "$ret"
     fi
     if [ $code != 0 ]; then   
-       error "command failed mkdir -p $tmp/$repo"
+       error "command failed mkdir -p $vroot$tmp/$repo"
     fi
 
-    ret=`$HOTBACKUP $src/$repo $tmp/$repo 2>&1`
+    if [ $usevserver ]
+    then
+	ret=`$VSERVER $vsname exec $HOTBACKUP $src/$repo $tmp/$repo 2>&1`
+    else
+	ret=`$HOTBACKUP $src/$repo $tmp/$repo 2>&1`
+    fi
     code=$?
     if [ "$ret" ]; then
        debug "$ret"
     fi
     if [ $code != 0 ]; then
-       error "command failed -- $HOTBACKUP $src/$repo $tmp/$repo"
+       error "command failed -- $HOTBACKUP $vroot$src/$repo $vroot$tmp/$repo"
        error=1
     fi
 done
 
 if [ $error -eq 1 ]; then
-    echo "Error: because of earlier errors, we are leaving svn backups in $tmp instead of $dest"
+    echo "Error: because of earlier errors, we are leaving svn backups in $vroot$tmp instead of $vroot$dest"
 else
-    if [ -d $dest -a -d $tmp ]; then
-        rm -rf $dest
+    if [ -d $vroot$dest -a -d $vroot$tmp ]; then
+        rm -rf $vroot$dest
     fi
-    if [ -d $tmp ]; then
-        mv $tmp $dest
+    if [ -d $vroot$tmp ]; then
+        mv $vroot$tmp $vroot$dest
     fi
 fi
 
diff --git a/handlers/sys b/handlers/sys
index fe780b5c..f111097e 100755
--- a/handlers/sys
+++ b/handlers/sys
@@ -26,10 +26,40 @@ getconf partitionsfile /var/backups/partitions.__star__.txt
 getconf hardware yes
 getconf hardwarefile /var/backups/hardware.txt
 
+# See if vservers are configured
+if [ "$VSERVERS" = "yes" ]
+then
+	if [ ! -d $VROOTDIR ]
+	then
+		fatal "vservers enabled, but $VROOTDIR does not exist!"
+	else
+		info "vserver method enabled"
+		usevserver=1
+	fi
+fi
+
 if [ "$packages" == "yes" ]; then
-	if [ ! -x "`which dpkg`" ]; then
-		warning "can't find dpkg, skipping installed packages report."
-		packages="no"
+	if [ $usevserver ]
+	then
+		for vserver in `ls $VROOTDIR |grep -v lost+found`
+		do
+			running=`vserver-info $vserver RUNNING`
+			if [ $running = 1]; then
+			    if [ ! -x "`$VSERVER $vserver exec which dpkg`" ]; then
+				warning "can't find dpkg in vserver $vserver, skipping installed packages report."
+				nodpkg="$nodpkg $vserver"
+			    fi
+			else
+			    warning "vserver $vserver is not running, skipping installed packages report."
+			    nodpkg="$nodpkg $vserver"
+			fi
+
+		done
+	else
+		if [ ! -x "`which dpkg`" ]; then
+			warning "can't find dpkg, skipping installed packages report."
+			packages="no"
+		fi
 	fi
 fi
 
@@ -57,10 +87,22 @@ fi
 # here we grab a list of the packages installed and removed.
 #
 
+if [ $usevserver ]
+then
+	for vserver in `ls $VROOTDIR | grep -v $nodpkg | grep -v lost+found`
+	do
+		debug "$VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile"
+		$VSERVER $vserver exec dpkg --get-selections > $VROOTDIR/$vserver$packagesfile
+	done
+fi
+
+# We want to perform this on the host as well
 if [ "$packages" == "yes" ]; then
+	debug "dpkg --get-selections > $packagesfile"
 	dpkg --get-selections > $packagesfile
 fi
 
+
 ## PARTITIONS #############################
 
 # here we use sfdisk to dump a listing of all the partitions. 
-- 
GitLab