Script to upload latest file to other server via FTP

Hello,

I have a script that finds the latest version of a file in a folder on my Minecraft server. I'm trying to come up with something that will then FTP that file over to my fileserver.

Here's what I have that finds the newest file:

find /home/mc/archive/sbhouse -type f -mtime +45 -exec ls {} \;

Then I'd like to have it FTP over to:

192.168.0.10
Username: minecraft
Password: minecraftpw

(no, these are not my real passwords but placeholders for someone to use if they can help me)

Thank you!

  • Nick

---------- Post updated at 01:24 PM ---------- Previous update was at 01:10 PM ----------

I found this from someone else's posting. Now I would just need a way to combine my find piece with this FTP piece:

set FILENAME = /local/filename.log

ftp -v -n "myipadd" << cmd
user "user" "passwd"
cd /remote/FTP
lcd /local/ ssh_install

ls -1 *.txt $local_path > $FILENAME

put $FILENAME
quit

-exec ls is redundant. find prints by default.

You could make a ~/.netrc file like this:

machine 192.168.0.10
        login minecraft
        password minecraftpw

Then you could do

( cd /home/mc/archive/sbhouse

find . -type f -mtime +45 |
while read LINE
do
        echo "put $LINE /path/to/remotedestination/$LINE"
done 

echo "quit" ) | ftp hostname

So I created a ".netrc" file in my home folder. Do I need to chmod it in any way?

Then, I'll create a new .SH script like this:

====================

#!/bin/bash

( cd /home/mc/archive/sbhouse  find . -type f -mtime -1 | 
while read LINE
do
         echo "put $LINE /$LINE"
done

echo "quit" ) | ftp 192.168.0.10

====================

(BTW: Replaced +45 with -1. I pasted wrong script example for within a day.)

So would this copy the file to the ftproot of the minecraft user on the other server?

chmod 600 it.

You removed a few too many newlines. :wink:

I doubt you actually want to upload these files into root, /filename. (Though you might have a chroot going on, for all I know.) Put them where you actually want them to go...

#!/bin/bash

( cd /home/mc/archive/sbhouse

find . -type f -mtime -1 | while read LINE
do
        echo "put $LINE /path/to/$LINE"
done

echo "quit" ) | ftp 192.168.0.10

I know nothing of minecraft's internal organization, you've shown nothing of it, and you provided the find yourself. I honestly don't know if it does what you want, your requirements aren't clear.

Try replacing ftp 192.168.0.10 with cat, see what your program prints. Is it naming the files you want for upload?

I figured I would configure the root of the FTP login for the "minecraft" user to go to the /home/minecraft folder on my FTP server, the home folder for that user. So, yes, I guess that's a chroot. But I have that configured via the FTP server software through Webmin. Can you tell I'm still new to Linux?

As for the minecraft structure, this is simply a daily tarball that I have built of the entire structure of the server. So the find statement I was using before (if it had the -1 vs. the +45 as the argument for mtime) will find only one file each time it is run, the one that is within 24 hours of now.

I'll test tonight when I get home from work. Was just curious to see if I understood you correctly. And thanks for correcting my hack-job of quoting the scripting you did.

  • Nick

The login folder might not be the same as the root folder. If you can ftp into it with commandline FTP then 'cd /' and see etc, usr, sbin, and stuff, then it's not a chroot.

IF you leave the second filename, the destination filename, off completely it should just end up in the login folder. I wasn't certain the login folder was actually your minecraft folder at the time, so tried to cover all possibilities.

I wouldn't bother with find then -- just ls -t and grab the topmost. That makes it a lot simpler.

ftp 192.168.0.10 <<EOF
put /home/mc/archive/sbhouse/$(ls -t /home/mc/archive/sbhouse | head -n 1)
quit
EOF

That <<EOF ... EOF pattern is a here-document, which is equivalent to feeding the section of text inside it into ftp's standard input with a pipe. The ending EOF must be at the very beginning of the line, or it won't be considered the end of the here-document...

For that matter, why not make uploading the tarball part of the process which creates it?

I did test this line:

find . -type f -mtime -1
It comes up with only one file, the latest tarball in the folder.

---------- Post updated at 02:50 PM ---------- Previous update was at 02:46 PM ----------

ls -t | head -n 1
That also finds the newest file! Cool. Something new.

The tarball is created by a series of scripts provided in a Minecraft management suite. I choose not to alter those scripts as they work so well and the last thing I want to do is jeopardize the stability of that system. So I figured I'd follow behind and do this.

Any chance you could do a pastebin of your latest suggestion? I'm not sure the <<EOF thing is where you say it's supposed to be. Just want to be sure.

Thanks for ALL of your help and teaching.

The ls -t | head -n 1 will always output only one file, while your find code could mess up and do two uploads if you happen to force a manual backup or somesuch one day and end up with two files on the same day.

The code's exactly as I have it written. It's the ending eof that has to be on the beginning of the line, not the beginning <<EOF -- that's supposed to be right after the command, where I have it. Copy-paste it and it'll be correct.