2 # mysql handler script for backupninja
5 getconf backupdir /var/backups/mysql
8 getconf dbhost localhost
18 getconf configfile /etc/mysql/debian.cnf
20 if [ "$user" == "" ]; then
25 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
26 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
29 ## Prepare ignore part of the command
30 ## This only works for mysqldump at the moment
34 ignore="$ignore --ignore-table=$i"
37 # If vservers are configured, decide if the handler should
38 # use them or if it should just operate on the host
40 if [ "$vservers" = "yes" ]
44 info "Using vserver '$vsname'"
47 info "No vserver name specified, actions will be performed on the host"
51 # If needed, make sure that the specified vserver exists and is running.
54 info "examining vserver '$vsname'"
56 vroot="$VROOTDIR/$vsname"
57 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
59 running=`$VSERVERINFO $vsname RUNNING`
60 [ $running = 1 ] || fatal "vserver $vsname is not running."
63 # create backup dirs, the vroot variable will be empty if no vsname was specified
64 # and will proceed to operate on the host
65 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
66 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
67 hotdir="$backupdir/hotcopy"
68 dumpdir="$backupdir/sqldump"
72 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
73 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
75 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
76 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
79 #######################################################################
83 # one of three authentication methods:
84 # 1. setting the user, so that /home/user/.my.cnf is used.
85 # 2. specifying the user and password in the handler config,
86 # which generates a temporary .my.cnf in /root/.my.cnf
87 # 3. specify the config file with --defaults-file
88 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
92 # only if dbusername and dbpassword specified.
93 # we create a tmp file because we don't want to
94 # specify the password on the command line.
97 if [ "$dbusername" != "" -a "$dbpassword" != "" ]; then
98 home=`getent passwd "root" | awk -F: '{print $6}'`
99 [ -d $home ] || fatal "Can't find root's home directory ($home)."
100 mycnf="$home/.my.cnf"
101 if [ -f $mycnf ]; then
103 tmpcnf="$home/my.cnf.disable"
104 debug "mv $mycnf $tmpcnf"
110 # auto generated backupninja mysql conf
113 password="$dbpassword"
117 password="$dbpassword"
121 password="$dbpassword"
124 defaultsfile="--defaults-file=$mycnf"
125 elif [ "$userset" == "false" ]; then
126 # if user is set, don't use $configfile
127 defaultsfile="--defaults-file=$configfile"
130 #######################################################################
133 if [ "$hotcopy" == "yes" ]; then
134 if [ "$databases" == "all" ]; then
137 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
139 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
141 debug "su $user -c '$execstr'"
143 output=`su $user -c "$execstr" 2>&1`
145 if [ "$code" == "0" ]; then
147 info "Successfully finished hotcopy of all mysql databases"
150 warning "Failed to hotcopy all mysql databases"
154 for db in $databases; do
157 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
159 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
161 debug "su $user -c '$execstr'"
163 output=`su $user -c "$execstr" 2>&1`
165 if [ "$code" == "0" ]; then
167 info "Successfully finished hotcopy of mysql database $db"
170 warning "Failed to hotcopy mysql database $db"
177 ##########################################################################
180 if [ "$sqldump" == "yes" ]; then
181 if [ "$databases" == "all" ]; then
184 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
186 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
190 for db in $databases; do
193 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
195 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
197 debug "su $user -c '$execstr'"
199 output=`su $user -c "$execstr" 2>&1`
201 if [ "$code" == "0" ]; then
203 info "Successfully finished dump of mysql database $db"
206 warning "Failed to dump mysql databases $db"
211 if [ "$compress" == "yes" ]; then
212 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
217 # clean up tmp config file
218 if [ "$dbusername" != "" ]; then
219 ## clean up tmp config file
222 if [ -f "$tmpcnf" ]; then
223 debug "mv $tmpcnf $mycnf"