2 # mysql handler script for backupninja
5 getconf backupdir /var/backups/mysql
7 getconf dbhost localhost
17 getconf configfile /etc/mysql/debian.cnf
19 if [ "$user" == "" ]; then
24 userhome=`grep "^$user:" /etc/passwd | awk -F: '{print $6}'`
25 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
28 # If vservers are configured, decide if the handler should
29 # use them or if it should just operate on the host
31 if [ "$vservers" = "yes" ]
35 info "Using vserver '$vsname'"
38 info "No vserver name specified, actions will be performed on the host"
42 # If needed, make sure that the specified vserver exists and is running.
45 info "examining vserver '$vsname'"
47 vroot="$VROOTDIR/$vsname"
48 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
50 running=`$VSERVERINFO $vsname RUNNING`
51 [ $running = 1 ] || fatal "vserver $vsname is not running."
54 # create backup dirs, the vroot variable will be empty if no vsname was specified
55 # and will proceed to operate on the host
56 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
57 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
58 hotdir="$backupdir/hotcopy"
59 dumpdir="$backupdir/sqldump"
63 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
64 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
66 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
67 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
70 #######################################################################
74 # one of three authentication methods:
75 # 1. setting the user, so that /home/user/.my.cnf is used.
76 # 2. specifying the user and password in the handler config,
77 # which generates a temporary .my.cnf in /root/.my.cnf
78 # 3. specify the config file with --defaults-file
79 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
83 # only if dbusername and dbpassword specified.
84 # we create a tmp file because we don't want to
85 # specify the password on the command line.
88 if [ "$dbusername" != "" -a "$dbpassword" != "" ]; then
89 home=`grep '^root:' /etc/passwd | awk -F: '{print $6}'`
90 [ -d $home ] || fatal "Can't find root's home directory ($home)."
92 if [ -f $mycnf ]; then
94 tmpcnf="$home/my.cnf.disable"
95 debug "mv $mycnf $tmpcnf"
101 # auto generated backupninja mysql conf
104 password="$dbpassword"
108 password="$dbpassword"
112 password="$dbpassword"
115 defaultsfile="--defaults-file=$mycnf"
116 elif [ "$userset" == "false" ]; then
117 # if user is set, don't use $configfile
118 defaultsfile="--defaults-file=$configfile"
121 #######################################################################
124 if [ "$hotcopy" == "yes" ]; then
125 if [ "$databases" == "all" ]; then
128 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
130 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
132 debug "su $user -c '$execstr'"
134 output=`su $user -c "$execstr" 2>&1`
136 if [ "$code" == "0" ]; then
138 info "Successfully finished hotcopy of all mysql databases"
141 warning "Failed to hotcopy all mysql databases"
145 for db in $databases; do
148 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
150 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
152 debug "su $user -c '$execstr'"
154 output=`su $user -c "$execstr" 2>&1`
156 if [ "$code" == "0" ]; then
158 info "Successfully finished hotcopy of mysql database $db"
161 warning "Failed to hotcopy mysql database $db"
168 ##########################################################################
171 if [ "$sqldump" == "yes" ]; then
172 if [ "$databases" == "all" ]; then
175 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
177 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
181 for db in $databases; do
184 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $vroot$dumpdir/${db}.sql"
186 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
188 debug "su $user -c '$execstr'"
190 output=`su $user -c "$execstr" 2>&1`
192 if [ "$code" == "0" ]; then
194 info "Successfully finished dump of mysql database $db"
197 warning "Failed to dump mysql databases $db"
202 if [ "$compress" == "yes" ]; then
203 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
208 # clean up tmp config file
209 if [ "$dbusername" != "" ]; then
210 ## clean up tmp config file
213 if [ -f "$tmpcnf" ]; then
214 debug "mv $tmpcnf $mycnf"