Automatic FTP backup using shell

Source: Internet
Author: User
Tags ftp client
Function:
Download all files in the FTP directory of the server,
Delete all files in the FTP directory of the server immediately after the download.
*********************

---------------------
Environment:

Both machines are centos4.5 _ x86
****************
1. FTP server host information
FTP Server Host Name Server
VFTP server: vsftp-2.0.1-5.EL4.5
By default, the FTP directory of the VFTP server is the directory of the System user.
The user here is ftptest, and the home directory is/home/ftptest
IP: 192.168.159
FTP directory:/home/ftptest
FTP user: ftptest
Ftptest User Password: 123456
****************
2. FTP client host information
FTP server host name Client
FTP Client: lftp-3.0.6-3
IP: 192.168.1.153
Storage Directory:/home/xiutuo/ftp
That is, the files downloaded from the server are stored in/home/xiutuo/FTP.
****************
3. Script Name: lftp_bak.sh
Stored in the/etc/directory of the Client
The script belongs to xiutuo and its Group xiutuo
# Cd/etc/
# Chown xiutuo: xiutuo lftp_bak.sh
You can set a scheduled task as needed.
Let the script be automatically executed at a certain time
****************
---------------------
For testing convenience,
Install the vsftp RPM package on the VFTP Server IP address 192.168.159.
Install the lftp RPM package on the FTP Client IP address 192.168.1.153.
++

On the client host
# Vi/etc/lftp_bak.sh
Add the following content:

#! /Bin/bash
# Specify the IP address of the FTP server
Serverip = 192.168.1.159
# Specify the FTP user of the FTP server
Serveruser = ftptest
# Specify the FTP user password for the FTP server
Serverpass = 123456
# Specify the directory where local files are downloaded from the client host.
Localdir =/home/xiutuo/ftp
# Specify the FTP directory of the server host
# Remotedir = ./
# Specify the Host Name of the server host
Host = 'hostname'
# Switch to the directory where the local downloaded file is stored
CD $ localdir
# Enter the backup start information
Echo "Starting FTP backup on" $ host
# Connecting to the FTP server
/Usr/bin/ftp-in <EOF
Open $ serverip
User $ serveruser $ serverpass
# Switch to the FTP directory of the server host
# Cd $ remotedir
Bin
# List the files in the FTP directory of the FTP server and put them in $ localdir of the client.
Ls. mulu.txt
Verbose
# Download all files in the FTP directory of the FTP server
Mget *.*.
# Print FTP status
Stat
# Exit the FTP server
Bye
EOF
Echo "Get the directory list OK, and get all the files once OK"
Echo "begin to delte the file in the mulu.txt"
Obtain the file name in mulu.txt in the client1_localdir.
And delete files in mulu.txt one by one.
List = 'cat mulu.txt | awk' {print $9 }''
For filename in $ list
Do
/Usr/bin/ftp-in <End
Open $ serverip
User $ serveruser $ serverpass
Bin
Verbose
Delete $ filename
Stat
Bye
End
Done
Echo "delete files in mulu.txt successful"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.