0

I have got the command working locally using normal commands. I now need it to run while using ftp.

I understand I need to use '!' And instead of copy I will need to use put so it works on the lcd, but I am not sure where to position the '!'. Could anyone point me towards a good tutorial or point me in the right direction?

And instead of Copy I will need to use put.

Here is my code which works locally but not within FTP:

#THE DIRECTORY THE ORIGINAL FILE IS STORED IN
MYDIR="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original"

#THE DIRECTORY THE BACKUP FILE WILL BE STORED IN
DEST="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup1"



#FIND ALL DIRECTORIES AND NAME TEMP DIRECTORY AS CURRENT DIRECTORY UP TO MAX DEPTH (1 = 1 LEVEL DOWN)
find "${MYDIR}" -maxdepth 3 -type d | while read CURRENTDIRECTORY;
    do 

        #FINDS THE NEWEST ZIP FILE IN T HE DIRECTORY
        NEWESTFILE=`ls -dtr1 "${CURRENTDIRECTORY}"/*.zip | tail -n 1`

        #CHECK IF FILE IS PRESENT
        if [ -z "${NEWESTFILE}" ] ; then
            echo "No file to copy"

        #CHECK IF FILE IS A DIRECTORY
        elif [ -d "${NEWESTFILE}" ] ; then
            echo "Newest file is a directory"

        #COPY NEWEST FILE TO DEST
        else
            dir=$(dirname "$CURRENTDIRECTORY")
            mkdir -p "${DEST}$dir"
            echo "Copying ${NEWESTFILE} --> ${DEST}$CURRENTDIRECTORY"
            cp -p "${NEWESTFILE}" "${DEST}$CURRENTDIRECTORY/"
        fi

    done

#SORT FILES IN ORDER NEWEST FIRST AND IGNORE FIRST 2
ls -t1 "${DEST}"* | tail -n +3 | while read filename
        do
            #REMOVE THE REST
            echo "remove $filename"
            rm "$filename"
    done

Here is the FTP code which is where I want to embed the above:

#ftp the file to another server

#FTP SERVER
HOST='server'

#USERNAME
USER='username'

#PASSWORD
PASSWD='password'

ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt
cd backup

lcd $MYDIR
lcd Original

#BY HERE I WANT TO RUN THE OTHER CODE


bye
EOT

I get a 5507 Segmentation fault: 11 which points to EOT on the final line

  • Your question is not clear. What “other code” do you want to run? Do you want that code to act on local files or on remote files? – Gilles Apr 10 '15 at 22:22
  • Sorry I was busy all weekend, I want the top section of code to act on the local file. The script will be located on the live server. I want to back up to a remote server. – smj2393 Apr 13 '15 at 8:05
1

mount ftp resource locally with:

curlftpfs [user@]host:[dir] mountpoint [options]

and do whatever you like like a local filesystem

  • According to this post on Stackoverflow curlftpfs doesn't work well with the last version of Ubuntu. See here – smj2393 Apr 13 '15 at 9:55
0

Copy your commands and put them in a script, call it something like filecheck.sh. So the script will look something like this:

#!bin/bash
#THE DIRECTORY THE ORIGINAL FILE IS STORED IN
MYDIR="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original"
#THE DIRECTORY THE BACKUP FILE WILL BE STORED IN
DEST="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup1"
#FIND ALL DIRECTORIES AND NAME TEMP DIRECTORY AS CURRENT DIRECTORY UP TO MAX DEPTH (1 = 1 LEVEL DOWN)
find "${MYDIR}" -maxdepth 3 -type d | while read CURRENTDIRECTORY;
    do 
        #FINDS THE NEWEST ZIP FILE IN T HE DIRECTORY
        NEWESTFILE=`ls -dtr1 "${CURRENTDIRECTORY}"/*.zip | tail -n 1`
        #CHECK IF FILE IS PRESENT
        if [ -z "${NEWESTFILE}" ] ; then
            echo "No file to copy"
        #CHECK IF FILE IS A DIRECTORY
        elif [ -d "${NEWESTFILE}" ] ; then
            echo "Newest file is a directory"
        #COPY NEWEST FILE TO DEST
        else
            dir=$(dirname "$CURRENTDIRECTORY")
            mkdir -p "${DEST}$dir"
            echo "Copying ${NEWESTFILE} --> ${DEST}$CURRENTDIRECTORY"
            cp -p "${NEWESTFILE}" "${DEST}$CURRENTDIRECTORY/"
        fi
    done
#SORT FILES IN ORDER NEWEST FIRST AND IGNORE FIRST 2
ls -t1 "${DEST}"* | tail -n +3 | while read filename
        do
            #REMOVE THE REST
            echo "remove $filename"
            rm "$filename"
    done

Make it executable with chmod +x filecheck.sh and you can run this script remotely with ssh [USER-NAME]@[REMOTE-HOST] filecheck.sh We are going to use lftp, so install it with whatever you need (you haven't specified your OS, but for CentOS use sudo yum install lftp or Ubuntu/Mint use sudo apt-get install lftp Create a new script called something like upload.sh and populate it as follows:

    #!/bin/bash
    # Create lftp script
    [[ -d lftp ]]||mkdir lftp
    script="ftp/html-update.scp"
    hostname=your.hostname.com
    user="Your_ftpuser_ID"
    pass="Your_ftpuser_password"
    echo "debug -o lftp/lftp-upload-debug.txt 5" > $script
    echo "open -u $user,$pass $hostname" >> $script
      rdir="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup1"
      ldir="local/path/where/you/want/the/copy/"
      echo "cd $rdir" >> $script
      echo "lcd $ldir" >> $script
      echo asc >> $script
      echo "put -a ${filename}" >> $script
    echo "exit" >> $script
lftp -f "${script}" > ftp/lftpupload.log

Make it executable with chmod as above. (Again, you can execute it remotely using ssh as above.) It creates a sub-directory from the current called ftp, where it places the upload script that is generated and then used by lftp. When the lftp command runs it also stores the log file there, lftpupload.log where you can see what happened.

You haven't given enough info about the local location you want to copy to so I use ${filename} above. You will need to extract it first, or just change the script to copy everything in that directory.

Man lftp is useful. I use lftp to alter data in html pages (about 200 of them) and then create, and run, an script to automatically update them. Changing (for example) the date on the footer on all my web pages takes less than a minute using this method.

EDIT: You can't run find within ftp, as you originally asked. But you can run the find command, or scripts remotely and then create an lftp script to automate the use of lftp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.