summaryrefslogblamecommitdiffstats
path: root/mysql/mysql_backup.sh
blob: 632c010eec505c899da4f44f1d2c5a546b8b8b52 (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"

# 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

# backup all databases to file and reset binlog
mysqldump -h${MYSQL_SERVER} -u${MYSQL_USER} -p${MYSQL_PASSWORD} \
    --flush-logs --master-data=2 --all-databases --delete-master-logs \
    --lock-all-tables | bzip2 -zc > "${BACKUP_PREFIX}.sql.bz2"

# echo created log of file sizes
du -sch "${BACKUP_PREFIX}.sql.bz2" | _log
echo | _log

# move backup to ftp server
ncftpput -u"${FTP_USER}" -p"${FTP_PASSWORD}" -V -DD "${FTP_SERVER}" \
    "${FTP_TARGET}" "${BACKUP_PREFIX}.sql.bz2" >/dev/null

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

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