Trending News
By using our website, you agree to the use of our cookies.

Blog

Backup MySQL Database to FTP Server – Shell Script Easy Guide
Database

Backup MySQL Database to FTP Server – Shell Script Easy Guide 

Backup MySQL Database to FTP Server

As a server administrator, you must back up the data on a regular basis. Backups are extremely useful for recovering data in the event of a crash or corruption. I created a quick script to create database backups from MySQL and upload them to an FTP server. 

As a system administrator, I suggest that you maintain a remote copy of each backup. You may also use our new advanced script to backup and export MySQL databases to remote locations.

Copy the script below into a shell script file. Then execute after updating all of the necessary values.

				
					#!/bin/bash

######################################################
#  Script Written by : Rahul Kumar
#  Date: Feb 21, 2013
######################################################

DATE=`date +%d%b%y`
LOCAL_BACKUP_DIR="/backup/"
DB_NAME="test"
DB_USER="root"
DB_PASSWORD="your password"
FTP_SERVER="ftp.yehiweb.com"
FTP_USERNAME="ftp user name"
FTP_PASSWORD="ftp user password"
FTP_UPLOAD_DIR="/backup/"
LOG_FILE=/backup/backup-DATE.log

############### Local Backup  ########################

mysqldump -u $DB_USER  -p$DB_PASSWORD $DB_NAME | gzip  > $LOCAL_BACKUP_DIR/$DB_NAME-$DATE.sql.gz

############### UPLOAD to FTP Server  ################

ftp -n $FTP_SERVER << EndFTP
user "$FTP_USERNAME" "$FTP_PASSWORD"
binary
hash
cd $FTP_UPLOAD_DIR
#pwd
lcd $LOCAL_BACKUP_DIR
put "$DB_NAME-$DATE.sql.gz"
bye
EndFTP

if test $? = 0
then
    echo "Database Successfully Uploaded to Ftp Server
        File Name $DB_NAME-$DATE.sql.gz " > $LOG_FILE
else
    echo "Error in database Upload to Ftp Server" > $LOG_FILE
fi
				
			

Setup Details – Change the following variable in the above script to match your device setting. To make the script work, make sure all of the values are right.

  • LOCAL_BACKUP_DIR => Local Direction Path For Storing Backups
  • DB_NAME => Name Of Database
  • DB_USER => Name Of Database Administrator
  • DB_PASSWORD => Password For Database Administrator
  • FTP_SERVER => Hostname’s FTP Server Ip
  • FTP_USERNAME => Username For FTP
  • FTP_PASSWORD => Password For FTP
  • FTP_UPLOAD_DIR => Backup Path for FTP Server
  • LOG_FILE => Name And Location Of The Log File

I hope this script will aid you in backing up your database to an FTP server.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on vk
Share on tumblr
Share on skype
Share on stumbleupon
Share on whatsapp
Share on email

Related posts

Leave a Reply