RHEL / CentOS Linux FTP Cron Job for automatic ftp backup

I need help with a cron job. A automatic ftp backup will pickup sql.tar.gz and upload to remote ftp server under CentOS Linux. How do I automate entire procedure to upload file /tmp/backup/sql.tar.gz?

You can use standard ftp client for File transfer protocol. The program allows a user to transfer files to and from a remote network site. However, modern FTP client follows little different syntax to upload file.

Here is a quick script to upload file:

# local directory to pickup *.tar.gz file
# remote server directory to upload backup
# login to remote server
ftp -n -i $SERVER <<EOF
mput $FILE/*.tar.gz

Make sure script has executable permissions:
$ chmod +x /path/to/ftp.backup.script.sh

Setup a cron job to run script at 15:30 (24 hr clock time) times:
30 15 * * * /path/to/ftp.backup.script.sh

Above script should work with all modern ftp client under any Linux / UNIX version.

A shell script to dump all mysql database and upload them via lftp program

Make sure you have lftp client installed:
# yum install lftp

### MySQL Server Login Info ###
# mysql server
### FTP SERVER Login info ###
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
NOW=$(date +"%d-%m-%Y")
[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/*
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
 FILE=$BAK/$db.$NOW-$(date +"%T").gz
lftp -u $FTPU,$FTPP -e "mkdir /mysql/$NOW;cd /mysql/$NOW; mput /backup/mysql/*; quit" $FTPS

Set a cron job:
# crontab -e
Run mysql backup ftp script everyday at midnight:
@midnight /path/to/mysql.backup.sh >/dev/null 2>&1
Related: Increase productivity with FTP autologin and macros and backup related shell script.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 19 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
19 comments… add one
  • Planet Malaysia Dec 6, 2007 @ 7:23

    I think this is not secure.

  • Wilfred Feb 24, 2008 @ 16:43

    Where does it say it’s secure?
    It does the job, period.
    FTP itself isn’t secure

  • Internet Marketing Legacy Mar 4, 2008 @ 18:31

    Hey, this is very helpful. Thank for the script.

  • mcguillan Nov 25, 2008 @ 12:33

    It would be better to encrypt at least the user and pass with md5…

  • Mrtz Jan 14, 2009 @ 5:53

    very useful, thanks.

  • Web Guy Jan 17, 2009 @ 7:36


    It is very helpful. Though have not tried it yet, I believe it will work like charm.

  • Jozef Sevcik Jan 24, 2009 @ 14:59

    Thank you, it’s very helpful.

  • Jozef Sevcik Jan 25, 2009 @ 19:18

    One note, it’s useful to run ftp with ‘-i’ option also, so I use ‘ftp -ni’.
    It will disable confirmation at mput for all files and also enable you to transfer more all files you filter (*.tar.gz in article.)
    My $0.02

  • Ethan Russell Oct 19, 2009 @ 19:07

    Thanks very much for the script, worked like a champ with minimal modifications needed.

  • Ekram Oct 21, 2009 @ 13:44

    Hi the problem is mput line.
    if i give the full path its give 550 error which is seems permission related.
    I just then give the file name for example mput filename works for me.

    what is the problem with
    mput path/of/filename

  • Ekram Oct 22, 2009 @ 3:32

    one more thing i solve the permission problem but i think mput full path name not supported?
    please help me

  • Bishop Dec 18, 2009 @ 15:46

    Ekram, try changing directory either befor you enter FTP or once in. That way, it will default to looking for your file in that directory. Don’t forget the / at the beginning of your path (/path/of/filename or //path/of/filename). Note: I am going over a secure network.

    # —– auto ftp DRAFT!!!
    cd /path/of/filename


    ftp -n $HOST <<END_SCRIPT
    quote USER $USER
    quote PASS $PASSWD
    put filename.txt /path/of/out_file/test_ftp.txt

  • Daniel Jun 13, 2010 @ 21:49

    just a note that your cron cmd is wrong: crontab lines are minute, hour.

  • Jay Oct 20, 2011 @ 21:48

    How can I get this to show the output from ftp so I can verify the file transferred correctly?

  • Sarah Hill Dec 29, 2011 @ 16:59

    Wilfred, I disagree….Maytech is far more secure than most ftp file transfer services.

  • gireesh Aug 28, 2012 @ 7:04

    any idea about this error:
    when i tried the same from a RHEL5.5 machine to Argosoft server on deafult port “21”

    ERROR: “”KERBEROS_V4 rejected as an authentication type””

  • Ganapathi Aug 29, 2012 @ 10:49

    I have an some files in cent os.I need to schedule auto backup that files to the another file storage server directory. I need that script.will you please anyone post that script.

    Source : Cent Os
    Destination : Samba File storage server( another directory).

  • JimmyGP Jan 22, 2013 @ 18:58

    Thanks for your help, that is exactly for my work!!!

  • Ahmed Oct 10, 2015 @ 8:26


    I have tried this script, but when i try to excute the script I am getting “The system cannot find the path specified.”

    cd /tmp/TEST/
    # local directory to pickup *.tar.gz file

    # remote server directory to upload backup–Windows FTP

    # login to remote server
    pftp -n -i $SERVER <<EOF
    mput $FILE/test.text


    Actually I am using Centos box to WIndows FTP Server, Please help

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum