RHEL / CentOS Linux FTP Cron Job for automatic ftp backup

by on December 6, 2007 · 18 comments· LAST UPDATED June 13, 2010

in , ,

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:

#!/bin/sh
USERNAME="your-ftp-user-name"
PASSWORD="your-ftp-password"
SERVER="your-ftp.server.com"
 
# local directory to pickup *.tar.gz file
FILE="/tmp/backup"
 
# remote server directory to upload backup
BACKUPDIR="/pro/backup/sql"
 
# login to remote server
ftp -n -i $SERVER <<EOF
user $USERNAME $PASSWORD
cd $BACKUPDIR
mput $FILE/*.tar.gz
quit
EOF

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

#!/bin/bash
### MySQL Server Login Info ###
MUSER="root"
MPASS="MYSQL-ROOT-PASSWORD"
# mysql server
MHOST="localhost"
 
### FTP SERVER Login info ###
FTPU="FTP-SERVER-USER-NAME"
FTPP="FTP-SERVER-PASSWORD"
FTPS="FTP-SERVER-IP-ADDRESS"
 
 
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAK="/backup/mysql"
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
do
 FILE=$BAK/$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
 
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.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 18 comments… read them below or add one }

1 Planet Malaysia December 6, 2007 at 7:23 am

I think this is not secure.

Reply

2 Wilfred February 24, 2008 at 4:43 pm

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

Reply

3 Internet Marketing Legacy March 4, 2008 at 6:31 pm

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

Reply

4 mcguillan November 25, 2008 at 12:33 pm

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

Reply

5 Mrtz January 14, 2009 at 5:53 am

very useful, thanks.

Reply

6 Web Guy January 17, 2009 at 7:36 am

Hey,

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

Reply

7 Jozef Sevcik January 24, 2009 at 2:59 pm

Thank you, it’s very helpful.

Reply

8 Jozef Sevcik January 25, 2009 at 7:18 pm

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

Reply

9 Ethan Russell October 19, 2009 at 7:07 pm

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

Reply

10 Ekram October 21, 2009 at 1:44 pm

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

Reply

11 Ekram October 22, 2009 at 3:32 am

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

Reply

12 Bishop December 18, 2009 at 3:46 pm

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.

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

HOST=’Server_IP’
USER=’User_ID’
PASSWD=’password’

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

Reply

13 Daniel June 13, 2010 at 9:49 pm

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

Reply

14 Jay October 20, 2011 at 9:48 pm

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

Reply

15 Sarah Hill December 29, 2011 at 4:59 pm

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

Reply

16 gireesh August 28, 2012 at 7:04 am

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”"

Reply

17 Ganapathi August 29, 2012 at 10:49 am

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).

Reply

18 JimmyGP January 22, 2013 at 6:58 pm

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

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: