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 # Check to make sure that the specified vserver exists
45 vroot="$VROOTDIR/$vsname"
46 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
49 # create backup dirs, the vroot variable will be empty if no vsname was specified
50 # and will proceed to operate on the host
51 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
52 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
53 hotdir="$backupdir/hotcopy"
54 dumpdir="$backupdir/sqldump"
58 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
59 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
61 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
62 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
65 #######################################################################
69 # one of three authentication methods:
70 # 1. setting the user, so that /home/user/.my.cnf is used.
71 # 2. specifying the user and password in the handler config,
72 # which generates a temporary .my.cnf in /root/.my.cnf
73 # 3. specify the config file with --defaults-file
74 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
78 # only if dbusername and dbpassword specified.
79 # we create a tmp file because we don't want to
80 # specify the password on the command line.
83 if [ "$dbusername" != "" -a "$dbpassword" != "" ]; then
84 home=`grep '^root:' /etc/passwd | awk -F: '{print $6}'`
85 [ -d $home ] || fatal "Can't find root's home directory ($home)."
87 if [ -f $mycnf ]; then
89 tmpcnf="$home/my.cnf.disable"
90 debug "mv $mycnf $tmpcnf"
96 # auto generated backupninja mysql conf
99 password="$dbpassword"
103 password="$dbpassword"
107 password="$dbpassword"
110 defaultsfile="--defaults-file=$mycnf"
111 elif [ "$userset" == "false" ]; then
112 # if user is set, don't use $configfile
113 defaultsfile="--defaults-file=$configfile"
116 #######################################################################
119 if [ "$hotcopy" == "yes" ]; then
120 if [ "$databases" == "all" ]; then
123 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
125 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
127 debug "su $user -c '$execstr'"
129 output=`su $user -c "$execstr" 2>&1`
131 if [ "$code" == "0" ]; then
133 info "Successfully finished hotcopy of all mysql databases"
136 warning "Failed to hotcopy all mysql databases"
140 for db in $databases; do
143 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
145 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
147 debug "su $user -c '$execstr'"
149 output=`su $user -c "$execstr" 2>&1`
151 if [ "$code" == "0" ]; then
153 info "Successfully finished hotcopy of mysql database $db"
156 warning "Failed to hotcopy mysql database $db"
163 ##########################################################################
166 if [ "$sqldump" == "yes" ]; then
167 if [ "$databases" == "all" ]; then
170 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
172 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
176 for db in $databases; do
179 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $vroot$dumpdir/${db}.sql"
181 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
183 debug "su $user -c '$execstr'"
185 output=`su $user -c "$execstr" 2>&1`
187 if [ "$code" == "0" ]; then
189 info "Successfully finished dump of mysql database $db"
192 warning "Failed to dump mysql databases $db"
197 if [ "$compress" == "yes" ]; then
198 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
203 # clean up tmp config file
204 if [ "$dbusername" != "" ]; then
205 ## clean up tmp config file
208 if [ -f "$tmpcnf" ]; then
209 debug "mv $tmpcnf $mycnf"