summaryrefslogblamecommitdiffstats
path: root/mysql/mysql_backup.sh
blob: 6ce5281579fafd499ce5f547077afedf11037312 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                                      



                                          












                                                                   
                                                          
 
                                             



                       


                                    
 
                 
                                         
 

                                                              

                      
                         


                                              










                                                                     

                                
                

           





                                                                   

                                                              
 


                    
#!/bin/bash
# this script will backup all mysql databases from the server to a ftp
# server

# mysql server informations
MYSQL_SERVER="localhost"
MYSQL_USER="backup"
MYSQL_PASSWORD="password"

# databases to exclude, seperated by space
# TODO
#MYSQL_EXCLUDE_DB=""

# ftp server data
FTP_SERVER="backup"
FTP_USER="ftp"
FTP_PASSWORD="password"

# target directory on the ftp (if no / at the beginning, relativ to
# the first directory after login)
FTP_TARGET="mysql"

# temporary directory
TMP_DIRECTORY="/tmp"

# backup name
BACKUP_PREFIX="$(hostname -f)_${MYSQL_SERVER}_$(date +%F)"

# logging (0 = enabled, all other = disabled)
LOGGING="0"

## end of configuration

# load common stuff
BACKUP_ROOT=".."
source ${BACKUP_ROOT}/common/init.sh

# check for tools
_check_tools mysqlshow mysqldump ncftpput

echo "Started backup of mysql databases ($(date +%c)):" | _log
echo | _log

# create temporary dir
mkdir -p ${TMP_DIRECTORY}
TMP_DIR=$(mktemp -d --tmpdir=${TMP_DIRECTORY})
pushd ${TMP_DIR} >/dev/null 2>&1

# enumerate all mysql databases
MYSQL_DATABASES=$(mysqlshow -h${MYSQL_SERVER} -u${MYSQL_USER} \
    -p${MYSQL_PASSWORD} | awk '{ print $2 }' | grep -v "^$" | sed \
    '1d')

# backup all databases to file
for db in ${MYSQL_DATABASES}
do
    mysqldump -h${MYSQL_SERVER} -u${MYSQL_USER} -p${MYSQL_PASSWORD} \
	${db} | bzip2 -zc > "${BACKUP_PREFIX}_${db}.sql.bz2"
done

# echo created log of file sizes
du -sch * | _log
echo | _log

# move backups to ftp server
for file in *
do
    ncftpput -u${FTP_USER} -p${FTP_PASSWORD} -V -DD ${FTP_SERVER} \
	${FTP_TARGET} ${file} >/dev/null
done

echo "Finished backup of mysql databases ($(date +%c))" | _log

# restore old cwd
popd >/dev/null 2>&1
rmdir ${TMP_DIR}