2004-12-09 05:37:12 +01:00
|
|
|
#
|
|
|
|
# mysql handler script for backupninja
|
|
|
|
#
|
|
|
|
|
|
|
|
getconf backupdir /var/backups/mysql
|
|
|
|
getconf databases all
|
2005-09-15 20:36:23 +02:00
|
|
|
getconf ignores
|
2004-12-09 05:37:12 +01:00
|
|
|
getconf dbhost localhost
|
2005-06-02 00:44:04 +02:00
|
|
|
getconf hotcopy no
|
2004-12-09 05:37:12 +01:00
|
|
|
getconf sqldump no
|
2005-06-02 00:44:04 +02:00
|
|
|
getconf compress yes
|
2005-05-24 21:27:43 +02:00
|
|
|
getconf vsname
|
2004-12-09 05:37:12 +01:00
|
|
|
|
2005-06-02 00:44:04 +02:00
|
|
|
# authentication:
|
|
|
|
getconf user
|
|
|
|
getconf dbusername
|
|
|
|
getconf dbpassword
|
|
|
|
getconf configfile /etc/mysql/debian.cnf
|
|
|
|
|
2005-09-15 20:36:23 +02:00
|
|
|
|
2005-05-24 21:27:43 +02:00
|
|
|
# If vservers are configured, decide if the handler should
|
|
|
|
# use them or if it should just operate on the host
|
2005-06-01 20:50:22 +02:00
|
|
|
|
2005-06-13 21:37:26 +02:00
|
|
|
if [ "$vservers" = "yes" ]
|
2005-05-24 21:27:43 +02:00
|
|
|
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
|
2004-12-09 05:37:12 +01:00
|
|
|
|
2005-08-27 02:19:50 +02:00
|
|
|
# If needed, make sure that the specified vserver exists and is running.
|
2005-05-24 21:27:43 +02:00
|
|
|
if [ $usevserver ]
|
|
|
|
then
|
2005-12-27 18:35:42 +01:00
|
|
|
info "Examining vserver '$vsname'"
|
2005-09-15 20:36:23 +02:00
|
|
|
# does it exist ?
|
2005-05-24 21:27:43 +02:00
|
|
|
vroot="$VROOTDIR/$vsname"
|
|
|
|
[ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
|
2005-09-15 20:36:23 +02:00
|
|
|
# is it running ?
|
2005-12-27 18:35:42 +01:00
|
|
|
$VSERVERINFO -q $vsname RUNNING
|
2005-12-27 18:04:33 +01:00
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
fatal "vserver $vsname is not running."
|
|
|
|
fi
|
2005-05-24 21:27:43 +02:00
|
|
|
fi
|
2005-12-27 18:36:21 +01:00
|
|
|
|
|
|
|
if [ "$user" == "" ]; then
|
|
|
|
userset=false;
|
|
|
|
user=root;
|
|
|
|
else
|
|
|
|
userset=true;
|
2005-12-27 18:36:32 +01:00
|
|
|
if [ $usevserver ]
|
|
|
|
then
|
|
|
|
userhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
|
|
|
|
if [ $? -eq 2 ]
|
|
|
|
then
|
|
|
|
fatal "User $user not found in /etc/passwd"
|
|
|
|
fi
|
|
|
|
userhome="$VROOTDIR/$vsname$userhome"
|
|
|
|
info "User home set to: $userhome"
|
|
|
|
[ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
|
|
|
|
else
|
|
|
|
userhome=`getent passwd "$user" | awk -F: '{print $6}'`
|
|
|
|
if [ $? -eq 2 ]
|
|
|
|
then
|
|
|
|
fatal "User $user not found in /etc/passwd"
|
|
|
|
fi
|
|
|
|
info "User home set to: $userhome"
|
|
|
|
[ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
|
|
|
|
fi
|
2005-12-27 18:36:21 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
## Prepare ignore part of the command
|
|
|
|
## This only works for mysqldump at the moment
|
|
|
|
|
|
|
|
ignore=''
|
|
|
|
for i in $ignores; do
|
|
|
|
ignore="$ignore --ignore-table=$i"
|
|
|
|
done
|
2005-05-24 21:27:43 +02:00
|
|
|
|
2005-12-27 18:35:54 +01:00
|
|
|
# create backup dirs, vroot variable will be empty if no vsname was specified
|
2005-05-24 21:27:43 +02:00
|
|
|
# and will proceed to operate on the host
|
|
|
|
[ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
|
|
|
|
[ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
|
2004-12-09 05:37:12 +01:00
|
|
|
hotdir="$backupdir/hotcopy"
|
|
|
|
dumpdir="$backupdir/sqldump"
|
2005-05-24 21:27:43 +02:00
|
|
|
|
|
|
|
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
|
2004-12-09 05:37:12 +01:00
|
|
|
|
2005-06-02 00:44:04 +02:00
|
|
|
#######################################################################
|
|
|
|
## AUTHENTICATION
|
|
|
|
|
|
|
|
#
|
|
|
|
# one of three authentication methods:
|
|
|
|
# 1. setting the user, so that /home/user/.my.cnf is used.
|
|
|
|
# 2. specifying the user and password in the handler config,
|
|
|
|
# which generates a temporary .my.cnf in /root/.my.cnf
|
|
|
|
# 3. specify the config file with --defaults-file
|
|
|
|
# (this option DOESN'T WORK WITH MYSQLHOTCOPY)
|
|
|
|
#
|
|
|
|
|
2004-12-09 05:37:12 +01:00
|
|
|
# create .my.cnf
|
2005-06-01 20:50:22 +02:00
|
|
|
# only if dbusername and dbpassword specified.
|
2005-06-02 00:44:04 +02:00
|
|
|
# we create a tmp file because we don't want to
|
2005-06-01 20:50:22 +02:00
|
|
|
# specify the password on the command line.
|
2004-12-09 05:37:12 +01:00
|
|
|
|
2005-06-01 20:50:22 +02:00
|
|
|
defaultsfile=""
|
2005-12-27 18:35:54 +01:00
|
|
|
if [ "$dbusername" != "" -a "$dbpassword" != "" ]
|
|
|
|
then
|
|
|
|
if [ $usevserver ]
|
|
|
|
then
|
|
|
|
home=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
|
|
|
|
else
|
|
|
|
home=`getent passwd "root" | awk -F: '{print $6}'`
|
|
|
|
fi
|
|
|
|
[ -d $home ] || fatal "Can't find root's home directory ($home)."
|
|
|
|
mycnf="$home/.my.cnf"
|
2005-12-27 18:36:07 +01:00
|
|
|
if [ -f $mycnf ]
|
|
|
|
then
|
2005-12-27 18:35:54 +01:00
|
|
|
# rename temporarily
|
|
|
|
tmpcnf="$home/my.cnf.disable"
|
|
|
|
debug "mv $mycnf $tmpcnf"
|
|
|
|
mv $mycnf $tmpcnf
|
|
|
|
fi
|
|
|
|
oldmask=`umask`
|
|
|
|
umask 077
|
|
|
|
cat > $mycnf <<EOF
|
2004-12-09 05:37:12 +01:00
|
|
|
# auto generated backupninja mysql conf
|
|
|
|
[mysql]
|
|
|
|
user=$dbusername
|
2005-06-09 22:02:28 +02:00
|
|
|
password="$dbpassword"
|
2004-12-09 05:37:12 +01:00
|
|
|
|
|
|
|
[mysqldump]
|
|
|
|
user=$dbusername
|
2005-06-09 22:02:28 +02:00
|
|
|
password="$dbpassword"
|
2004-12-09 05:37:12 +01:00
|
|
|
|
|
|
|
[mysqlhotcopy]
|
|
|
|
user=$dbusername
|
2005-06-09 22:02:28 +02:00
|
|
|
password="$dbpassword"
|
2004-12-09 05:37:12 +01:00
|
|
|
EOF
|
|
|
|
umask $oldmask
|
2005-06-02 00:44:04 +02:00
|
|
|
defaultsfile="--defaults-file=$mycnf"
|
|
|
|
elif [ "$userset" == "false" ]; then
|
|
|
|
# if user is set, don't use $configfile
|
|
|
|
defaultsfile="--defaults-file=$configfile"
|
2004-12-09 05:37:12 +01:00
|
|
|
fi
|
2005-06-01 20:50:22 +02:00
|
|
|
|
2005-06-02 00:44:04 +02:00
|
|
|
#######################################################################
|
2004-12-09 05:37:12 +01:00
|
|
|
## HOT COPY
|
|
|
|
|
|
|
|
if [ "$hotcopy" == "yes" ]; then
|
|
|
|
if [ "$databases" == "all" ]; then
|
2005-05-24 21:27:43 +02:00
|
|
|
if [ $usevserver ]
|
|
|
|
then
|
2005-06-02 00:44:04 +02:00
|
|
|
execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
|
2005-05-24 21:27:43 +02:00
|
|
|
else
|
2005-06-02 00:44:04 +02:00
|
|
|
execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
|
2005-05-24 21:27:43 +02:00
|
|
|
fi
|
2005-01-03 23:22:58 +01:00
|
|
|
debug "su $user -c '$execstr'"
|
2004-12-09 05:37:12 +01:00
|
|
|
if [ ! $test ]; then
|
|
|
|
output=`su $user -c "$execstr" 2>&1`
|
|
|
|
code=$?
|
|
|
|
if [ "$code" == "0" ]; then
|
2005-01-03 23:22:58 +01:00
|
|
|
debug $output
|
|
|
|
info "Successfully finished hotcopy of all mysql databases"
|
2004-12-09 05:37:12 +01:00
|
|
|
else
|
2005-01-03 23:22:58 +01:00
|
|
|
warning $output
|
|
|
|
warning "Failed to hotcopy all mysql databases"
|
2004-12-09 05:37:12 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
for db in $databases; do
|
2005-05-24 21:27:43 +02:00
|
|
|
if [ $usevserver ]
|
|
|
|
then
|
2005-06-02 00:44:04 +02:00
|
|
|
execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
|
2005-05-24 21:27:43 +02:00
|
|
|
else
|
2005-06-02 00:44:04 +02:00
|
|
|
execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
|
2005-05-24 21:27:43 +02:00
|
|
|
fi
|
2005-01-03 23:22:58 +01:00
|
|
|
debug "su $user -c '$execstr'"
|
2004-12-09 05:37:12 +01:00
|
|
|
if [ ! $test ]; then
|
|
|
|
output=`su $user -c "$execstr" 2>&1`
|
|
|
|
code=$?
|
|
|
|
if [ "$code" == "0" ]; then
|
2005-01-03 23:22:58 +01:00
|
|
|
debug $output
|
|
|
|
info "Successfully finished hotcopy of mysql database $db"
|
2004-12-09 05:37:12 +01:00
|
|
|
else
|
2005-01-03 23:22:58 +01:00
|
|
|
warning $output
|
|
|
|
warning "Failed to hotcopy mysql database $db"
|
2004-12-09 05:37:12 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
2005-06-02 00:44:04 +02:00
|
|
|
|
|
|
|
##########################################################################
|
2004-12-09 05:37:12 +01:00
|
|
|
## SQL DUMP
|
|
|
|
|
|
|
|
if [ "$sqldump" == "yes" ]; then
|
|
|
|
if [ "$databases" == "all" ]; then
|
2005-05-24 21:27:43 +02:00
|
|
|
if [ $usevserver ]
|
|
|
|
then
|
2005-06-01 20:50:22 +02:00
|
|
|
databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
|
2005-12-27 18:35:54 +01:00
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
|
|
|
|
fi
|
2005-05-24 21:27:43 +02:00
|
|
|
else
|
2005-06-01 20:50:22 +02:00
|
|
|
databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
|
2005-12-27 18:35:54 +01:00
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
|
|
|
|
fi
|
2005-05-24 21:27:43 +02:00
|
|
|
fi
|
2004-12-09 05:37:12 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
for db in $databases; do
|
2005-05-24 21:27:43 +02:00
|
|
|
if [ $usevserver ]
|
|
|
|
then
|
2005-09-15 20:36:23 +02:00
|
|
|
execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
|
2005-05-24 21:27:43 +02:00
|
|
|
else
|
2005-09-15 20:36:23 +02:00
|
|
|
execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
|
2005-05-24 21:27:43 +02:00
|
|
|
fi
|
2005-01-03 23:22:58 +01:00
|
|
|
debug "su $user -c '$execstr'"
|
2004-12-09 05:37:12 +01:00
|
|
|
if [ ! $test ]; then
|
|
|
|
output=`su $user -c "$execstr" 2>&1`
|
|
|
|
code=$?
|
|
|
|
if [ "$code" == "0" ]; then
|
2005-01-03 23:22:58 +01:00
|
|
|
debug $output
|
|
|
|
info "Successfully finished dump of mysql database $db"
|
2004-12-09 05:37:12 +01:00
|
|
|
else
|
2005-01-03 23:22:58 +01:00
|
|
|
warning $output
|
|
|
|
warning "Failed to dump mysql databases $db"
|
2004-12-09 05:37:12 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$compress" == "yes" ]; then
|
2005-05-24 21:27:43 +02:00
|
|
|
output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
|
2005-01-03 23:22:58 +01:00
|
|
|
debug $output
|
2004-12-09 05:37:12 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2005-06-01 20:50:22 +02:00
|
|
|
# clean up tmp config file
|
2005-06-02 00:44:04 +02:00
|
|
|
if [ "$dbusername" != "" ]; then
|
|
|
|
## clean up tmp config file
|
2005-01-03 23:22:58 +01:00
|
|
|
debug "rm $mycnf"
|
2004-12-09 05:37:12 +01:00
|
|
|
rm $mycnf
|
2005-06-02 00:44:04 +02:00
|
|
|
if [ -f "$tmpcnf" ]; then
|
|
|
|
debug "mv $tmpcnf $mycnf"
|
|
|
|
mv $tmpcnf $mycnf
|
|
|
|
fi
|
2004-12-09 05:37:12 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|