Senin, 29 Agustus 2022

FTP-script upload backup

 

Shell Script to Put File in a FTP Server

  • https://www.geeksforgeeks.org/shell-script-to-put-file-in-a-ftp-server/

The File Transfer Protocol also called FTP is used to transfer files from client to server and vice-versa. It mainly uses port 21 for communication.

Here we can simplify the process of uploading files using FTP. But, before we write a script, let’s look at how to get/put files onto an ftp server directly using commands.

Basic FTP Command:

The commands to start an FTP communication and end it is shown below:

ftp server #Prompts for login details and connects to server
#Example: 
ftp 192.168.0.104

bye           #Terminates the ftp connection and exits ftp

Example:

Shell Script to Put File in a FTP Server

After typing “FTP hostname”, you will be asked for a username and password. If login is successful after entering the details, we start in the FTP user’s home directory on the server. Any file you upload now gets uploaded to this directory. If you have to upload a file in some other directory on the server, you first have to change to that directory using the “cd” command. Note that, using the cd command in an FTP prompt only changes directory on the server, i.e. we will still be in the same directory on our local computer.

Commands in FTP help us to navigate the server’s directories, fetch and upload files from and to the server. To get single or multiple files, we can use commands “get” and “mget” respectively. Similarly, to put single or multiple files, we can use commands “put” and “mput” respectively.

Some important ftp commands:

ls                   #Lists files in server
cd dir                #Change directory in server
get file1.c           #Downloads file1.c
put file.txt       #Uploads file.txt
mput *.c file.txt  #Uploads all c files and file.txt

Example of putting a file in server through FTP prompt:

Shell Script to Put File in a FTP Server

Shell Script to put the file in an FTP server:

#!/bin/bash

# The 3 variables below store server and login details
HOST="192.168.0.104"
USER="user1"
PASSWORD="1234"


# $1 is the first argument to the script
# We are using it as upload directory path
# If it is '.', file is uploaded to current directory.
DESTINATION=$1


# Rest of the arguments are a list of files to be uploaded.
# ${@:2} is an array of arguments without first one.
ALL_FILES="${@:2}"


# FTP login and upload is explained in paragraph below
ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd $DESTINATION
mput $ALL_FILES
bye
EOF

Shell Script to Put File in a FTP Server

The above script requires the following data:

  1. Server’s hostname
  2. Server user’s login details
  3. The directory in which to upload files on the server (passed as an argument to the script)
  4. The list of files to be uploaded to the server (passed as an argument to script)

After logging in to the server, we need to enter FTP commands manually, but by using input redirection we can supply the commands directly in the script. “<<” is used for input redirection and “EOF” is used to mark the beginning and end of the FTP input. 

The “mput” command has been used to upload files as mput can upload either a single file or multiple files.

If login is successful and the files given as input to the script are available, all the files should have been put in the server along with a success message displayed for each file.

The options -inv can also be written as -i -n -v and their functions are explained in the below table:

OptionMeaning
-iDisable interactive mode, so that FTP will not ask for confirmation of each file while using mput command etc. We are using this for convenience while uploading or downloading files
-nDisable auto-login. We have to do this, so we can manually log in using “user” command inside the script
-vEnables verbose mode. This helps us to see the server responses after executing each FTP command

To execute the script supply the upload directory and also a list of files:

./script_name.sh  path_to_upload file1 file2 file3

File Uploading Example (Put all .c files and f1.txt in the current directory of server):

Shell Script to Put File in a FTP Server

Tidak ada komentar:

Posting Komentar