Script to upload latest file to other server via FTP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to upload latest file to other server via FTP
# 1  
Old 03-06-2012
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:


Code:
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


Last edited by Franklin52; 03-07-2012 at 05:10 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-06-2012
-exec ls is redundant. find prints by default.

You could make a ~/.netrc file like this:
Code:
machine 192.168.0.10
        login minecraft
        password minecraftpw

Then you could do

Code:
( 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

# 3  
Old 03-06-2012
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:

====================
Code:
#!/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?

Last edited by Corona688; 03-06-2012 at 06:13 PM.. Reason: Use code tags, please
# 4  
Old 03-06-2012
Quote:
Originally Posted by nbsparks
So I created a ".netrc" file in my home folder. Do I need to chmod it in any way?
chmod 600 it.

Quote:
Then, I'll create a new .SH script like this:
You removed a few too many newlines. Smilie

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...

Code:
#!/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

Quote:
So would this copy the file to the ftproot of the minecraft user on the other server?
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?

Last edited by Corona688; 03-06-2012 at 06:18 PM..
# 5  
Old 03-06-2012
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
# 6  
Old 03-06-2012
Quote:
Originally Posted by nbsparks
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.
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.
Quote:
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 wouldn't bother with find then -- just ls -t and grab the topmost. That makes it a lot simpler.

Code:
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?
# 7  
Old 03-06-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Upload latest file to ftp

Hi, I want to create a script that parses the content of a file (on each line there is a filename with full path containing the latest fiels created on that day) and uploads every file to a ftp location. Any elegant way to do it ? (4 Replies)
Discussion started by: liviusbr
4 Replies

2. Ubuntu

FTP script to get latest file from server

Hello people, I have to download, with a scheduled script, the latest file from an FTP server. In the remote DIR, named .../TEKNONET/60468/, every night a CDR file like this gets uploaded into it: 000006046820151122N001.CDR, so my script will have to download every day the previous day... (12 Replies)
Discussion started by: virtus96
12 Replies

3. Shell Programming and Scripting

FTP in shell script and selecting files for upload

Hi, Im a newbie with programming and shell scripting. Im running OSX/Mac and Darwin. I would like to create a shell script that would : 1. Search a Volume and directory (including subdirectories) for a file that : * filename ends with ”_Highres.pdf” and * the file creation date of... (8 Replies)
Discussion started by: NickeZ28
8 Replies

4. Shell Programming and Scripting

Download latest file via ftp server unix through shell script

Hello this is my first post in this forum , I dont want to be unhappy.. I am writing one script but facing difficulty to find the latest file with some new pattern My requirement is 1. The file is coming like "ABCD-23220140303" at FTP server once in a week. 2. script will run on daily... (3 Replies)
Discussion started by: ajju
3 Replies

5. Shell Programming and Scripting

Find files and upload to FTP Server

HI, I need a script to find new files that created after 6:00 from /home/ugh /demo/conn /UAT/d01 and upload them into ftp server according to system date: Please help me (1 Reply)
Discussion started by: refra
1 Replies

6. Shell Programming and Scripting

Curl ftp upload success but no file exist on the server !!!!

hello, I'm trying to upload a file to this ftp server and others ftp://ftp.byethost12.com as you can see in the output of CURL using the -v option curl reports that the upload succeeded but when i connected to the server with file-zilla there is no file uploaded the same command upload files... (5 Replies)
Discussion started by: laraaj
5 Replies

7. Shell Programming and Scripting

How to FTP the latest file, based on date, from a remote server through a shell script?

How to FTP the latest file, based on date, from a remote server through a shell script? I have four files to be FTP'ed from remote server. They are of the following format. build1_runtime_mmddyyyy.txt build2_runtime_mmddyyyy.txt build3_runtime_mmddyyyy.txt buifile_count_mmddyyyy.txt ... (9 Replies)
Discussion started by: imran_affu
9 Replies

8. Shell Programming and Scripting

bash script for ftp-upload is not working

Hello everyone, sorry for the title, most of you must getting sick of reading something like this, but I haven't found a solution, although I found many threads according to it. I'm working on a bash script that connects to a network printer with ftp where I want to upload a pdf created... (3 Replies)
Discussion started by: le_mae
3 Replies

9. Shell Programming and Scripting

ftp put in shell script -- whole file doesn't upload

Hi I'm having some trouble with a bash shell script that I'm writing. In the script, I'm trying to upload a file to a backup repository using ftp, but the whole file doesn't get uploaded. This is the file's properties at the start (I've highlighted the file size in red): -rw-r--r-- 1 root... (2 Replies)
Discussion started by: Viola
2 Replies

10. Shell Programming and Scripting

Shell script to ftp latest file

Hello All, Hope u r doing fine. I'm writing a shell script to ftp the latest file having pericular convention as 'ULTI_15072007043205.txt' on daily basis. Now the date & timing of the file geleration isnt constant, so the file name daily varies as per the date & timing of generation. Can anyone... (7 Replies)
Discussion started by: im_new
7 Replies
Login or Register to Ask a Question