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 $VSERVERINFO -q $vsname RUNNING
62 fatal "vserver $vsname is not running."
66 # create backup dirs, the vroot variable will be empty if no vsname was specified
67 # and will proceed to operate on the host
68 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
69 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
70 hotdir="$backupdir/hotcopy"
71 dumpdir="$backupdir/sqldump"
75 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
76 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
78 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
79 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
82 #######################################################################
86 # one of three authentication methods:
87 # 1. setting the user, so that /home/user/.my.cnf is used.
88 # 2. specifying the user and password in the handler config,
89 # which generates a temporary .my.cnf in /root/.my.cnf
90 # 3. specify the config file with --defaults-file
91 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
95 # only if dbusername and dbpassword specified.
96 # we create a tmp file because we don't want to
97 # specify the password on the command line.
100 if [ "$dbusername" != "" -a "$dbpassword" != "" ]; then
101 home=`getent passwd "root" | awk -F: '{print $6}'`
102 [ -d $home ] || fatal "Can't find root's home directory ($home)."
103 mycnf="$home/.my.cnf"
104 if [ -f $mycnf ]; then
106 tmpcnf="$home/my.cnf.disable"
107 debug "mv $mycnf $tmpcnf"
113 # auto generated backupninja mysql conf
116 password="$dbpassword"
120 password="$dbpassword"
124 password="$dbpassword"
127 defaultsfile="--defaults-file=$mycnf"
128 elif [ "$userset" == "false" ]; then
129 # if user is set, don't use $configfile
130 defaultsfile="--defaults-file=$configfile"
133 #######################################################################
136 if [ "$hotcopy" == "yes" ]; then
137 if [ "$databases" == "all" ]; then
140 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
142 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
144 debug "su $user -c '$execstr'"
146 output=`su $user -c "$execstr" 2>&1`
148 if [ "$code" == "0" ]; then
150 info "Successfully finished hotcopy of all mysql databases"
153 warning "Failed to hotcopy all mysql databases"
157 for db in $databases; do
160 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
162 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
164 debug "su $user -c '$execstr'"
166 output=`su $user -c "$execstr" 2>&1`
168 if [ "$code" == "0" ]; then
170 info "Successfully finished hotcopy of mysql database $db"
173 warning "Failed to hotcopy mysql database $db"
180 ##########################################################################
183 if [ "$sqldump" == "yes" ]; then
184 if [ "$databases" == "all" ]; then
187 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
189 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
193 for db in $databases; do
196 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
198 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
200 debug "su $user -c '$execstr'"
202 output=`su $user -c "$execstr" 2>&1`
204 if [ "$code" == "0" ]; then
206 info "Successfully finished dump of mysql database $db"
209 warning "Failed to dump mysql databases $db"
214 if [ "$compress" == "yes" ]; then
215 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
220 # clean up tmp config file
221 if [ "$dbusername" != "" ]; then
222 ## clean up tmp config file
225 if [ -f "$tmpcnf" ]; then
226 debug "mv $tmpcnf $mycnf"