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:

#!/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:
15 30 * * * /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.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 8 comments… read them below or add one }

1 Planet Malaysia 12.06.07 at 7:23 am

I think this is not secure.

2 Wilfred 02.24.08 at 4:43 pm

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

3 Internet Marketing Legacy 03.04.08 at 6:31 pm

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

4 mcguillan 11.25.08 at 12:33 pm

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

5 Mrtz 01.14.09 at 5:53 am

very useful, thanks.

6 Web Guy 01.17.09 at 7:36 am

Hey,

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

7 Jozef Sevcik 01.24.09 at 2:59 pm

Thank you, it’s very helpful.

8 Jozef Sevcik 01.25.09 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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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

Previous post: Find the file permission without using ls -l command

Next post: How to: Open Ssh Port 22 on Linux APF Firewall under CentOS / RHEL