Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am using MySQL as the database system for my application on a Linux system. Every week I update the system and take backups (mysqldump) of the databases changed (2 databases). I then .tar.gz them and ftp the resulting file to a remote Windows server, after which I remove the original backups and tar.gz files from the Linux server. Being a complete novice when it comes to Unix systems, I would like to know if it is possible to write a script which would do all this automatically, i.e. perform the following steps.

1) Backup database A to A.sql (mysqldump) 2) Backup database B to B.sql (mysqldump) 3) tar -cvzf dest.tar.gz A.sql B.sql 4) ftp dest.tar.gz to ftp@remoteserver.com 5) Delete A.sql, B.sql, dest.tar from local server

It would be great if I could get some steps in the right direction! Thanks in advance, Tim

share|improve this question
add comment

1 Answer

I would not recommend using FTP for any files you care about (particularly database backups if that database contains customer information), but that is ultimately up to you.

To start with, script the creation of your backups on the Linux server with something like the following:

#!/bin/bash
# 
# Call script with:
# /cron-mysql_bkup.sh mysql_hostname mysql_username mysql_password /backup/directory
# 
if [ -n "$1" ]; then
 MYSQL_HOST="$1"
else
 echo "MYSQL_HOST not specified."
 exit 1
fi
if [ -n "$2" ]; then
 MYSQL_USER="$2"
else
 echo "MYSQL_USER not specified."
 exit 1
fi
if [ -n "$3" ]; then
 MYSQL_PASS="$3"
else
 echo "MYSQL_PASS not specified."
 exit 1
fi
if [ -n "$4" ]; then
 DIR_BKUP="$4"
 mkdir -p ${DIR_BKUP}
else
 echo "DIR_BKUP not specified."
 exit 1
fi
# 
# Generate filename
# 
BACKUP_FILENAME=`date "+%Y-%m-%d"`".sql"
# 
# Dump all databases
# 
mysqldump -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS} --all-databases > ${DIR_BKUP}/${MYSQL_HOST}/${BACKUP_FILENAME}
# 
# Write status of the operation to the mysql.log file in the backup directory
# 
if [ $? -ne 0 ]; then
 echo `date`" - ERROR creating backups of host "${MYSQL_HOST} >> ${DIR_BKUP}/mysql.log
else
 echo `date`" - MySQL Backup complete for "${MYSQL_HOST} >> ${DIR_BKUP}/mysql.log
 BACKUP_TAR_NAME=${BACKUP_FILENAME}".tar.gz"
 tar czf ${DIR_BKUP}/${MYSQL_HOST}/${BACKUP_TAR_NAME} ${DIR_BKUP}/${MYSQL_HOST}/${BACKUP_FILENAME} && rm ${DIR_BKUP}/${MYSQL_HOST}/${BACKUP_FILENAME}
fi

This script will:

  1. Check to see whether all the required parameters were set
  2. Dump all MySQL databases to a file named YYYY-mm-dd.sql under the backup directory specified (this directory will be created if it does not exist)
  3. GZIP the file and, if the GZIP operation succeeds, delete the original SQL file

This script should be fairly straightforward for you to customize if you wish to - see the Advanced Bash Scripting Guide to learn more about doing so.

... once your databases are backed up and can be found in a specific location, why not automate backup retrieval with WinSCP instead of transferring your files over FTP?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.