Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Databases»MariaDB»MySQL Database Backup to FTP Server – Shell Script

    MySQL Database Backup to FTP Server – Shell Script

    RahulBy RahulFebruary 21, 20131 Min ReadUpdated:April 20, 2022

    As a system administrator, you need to take backup on daily basis. Backups are very useful to recover data from any crashes or corruption. I have written a simple script to take database backup from MySQL server and upload it to FTP server. Being a system administrator, I recommend keeping a remote copy of your every backup. You can also try our new advance script for MySQL databases backup and upload to remote locations.

    Create a shell script file and copy the below script. Then update all the required values and execute.

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    #!/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.tecadmin.net"
    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 – Edit the above script for the following variable as per your system environment. Place all the values correctly to make the script work properly.

    • LOCAL_BACKUP_DIR => Local direction path to store backup
    • DB_NAME => database name
    • DB_USER => database adminitrator user name
    • DB_PASSWORD => database administrator password
    • FTP_SERVER => ftp server ip for hostname
    • FTP_USERNAME => ftp username
    • FTP_PASSWORD => ftp password
    • FTP_UPLOAD_DIR => ftp server backup path
    • LOG_FILE => log file name and location

    I hope this script will help you for taking database backup over the FTP server.

    database backup to ftp MySQL mysql backup mysql database backup mysql database ftp backup upload backup to ftp
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleLinux Server Security Tips
    Next Article How to Keep Systems Up to Date – CentOS / RHEL / Fedora

    Related Posts

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How To Install MySQL Server on Ubuntu 22.04

    Updated:April 6, 20224 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    How To Install MariaDB on Debian 11

    4 Mins Read

    How to Rename MySQL Database

    Updated:July 26, 20213 Mins Read

    How To Change MySQL User Password

    2 Mins Read

    3 Comments

    1. Umar on August 5, 2020 7:01 am

      Hello,

      How can we delete remote ftp backups older than X days to keep storage costs low.

      Reply
    2. Olek on July 14, 2016 10:00 am

      Nice script, thanks. But why not use MySQLBackupFTP (http://mysqlbackupftp.com/). This tool makes MySQL database backups according to your schedule and sends them to FTP Server automatically. By the way, it has a free plan.

      Reply
    3. Sobhan on July 7, 2016 5:30 pm

      Thanks for the great script, saved my time.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.