LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   automated ftp (https://www.linuxquestions.org/questions/programming-9/automated-ftp-550965/)

sang_froid 05-03-2007 09:36 AM

automated ftp
 
Hi
I am trying to delete some specific files ( files other than created today) from the server on a cron basis. I wrote a small script, but I am stuck up in how to delete only specific files.


#!/usr/bin/expect -f

set IP [lindex $argv 0]
set timeout -1
spawn ftp $IP
expect ):
send "username\n"
expect word:
send "password\n"
expect ftp>
send "prompt\n"
expect ftp>
send "passive\n"
expect ftp>
send "cd logs/\n"
expect ftp>


After this I am stuck up. It is because I want to delete files with old dates ie, I don't want to delete the file dated today.


The files are like logfile-070501,logfile-070502 and so on..

I cannot run the command "rm logfile-0705*" ( as it will delete today's file too)

By the way, I have the list of files to be deleted in a separate file. I am wondering if by running any other additional script, we can delete selected files from the remote server .

I tried with ! command of ftp, but it runs the commands on local machine.

Your help will be highly appreciated.Thanks

avallach 05-04-2007 06:14 AM

Can't you use some tools like logrotate ?

Sorry for a bit offtopic

sang_froid 05-04-2007 02:10 PM

nopes...the thing is diff.

System logrotates it...but i need to ftp and del only selected old files..

jim mcnamara 05-05-2007 05:20 AM

Best practice limits what commands an ftp access to a server can execute.
If you can:
Code:

usr/bin/ftp -n<<EOF
open servernode
USER usern passwd
cd /path/to/logs
ls -l *
bye
EOF  > listingfile

and get meaningful results, you may be able to parse out files with older dates from the dta in listingfile.

Otherwise, you will have to try one of:
1. rsh over to the server to get a complete file listing and/or delete old files
2. set up a cron job on the server to clean up old files

twantrd 05-05-2007 05:54 AM

Yup, i would follow Jim's advice here. However, since you already have a list of files you want to remove, then just put in a for loop and you should be good to go.

-twantrd

jschiwal 05-05-2007 06:09 AM

I think that a cron job on the server(s) to delete files based on the file modification date would be a better solution.
You could use the find command to match the date and pattern both:
find /path/to/logs/ -name "logfile-*" -ctime -1 -exec dir rm '{}' \;
Note: there is an option of find to align -ctime to midnight, but I'm too lazy to look it up.

Otherwise, since cron will be performed daily, you can use yesterdays date to construct a wildcard to use in the ftp script.

You could then use something like JimMcNamara's script:

Code:

YESTERDAY=$(date --date=yesterday +%y%m%d)
SERVERNODE=<ip-address>

ftp user:password@${SERVERNODE} << EOF
cd logs/
del log-${YESTERDAY}
bye
EOF >localcleaninglog-$(date +%y%m%d)


I've used the form "ftp user:password@ipaddress <ftpscript"
in a loop to automate the renaming of files in a large number of devices (5 racks of duets) after we found out that we needed
to change all of the filenames to include leading zeros. It worked from my laptop running linux, but didn't work in windows
or using cygwin in windows. Also, the --date=yesterday is a GNU option of date.

sang_froid 05-07-2007 10:53 AM

At the first, ftp user : password@${SERVERNODE} << EOF didn't work :(

Quote:

Originally Posted by jschiwal
I think that a cron job on the server(s) to delete files based on the file modification date would be a better solution.
You could use the find command to match the date and pattern both:
find /path/to/logs/ -name "logfile-*" -ctime -1 -exec dir rm '{}' \;
Note: there is an option of find to align -ctime to midnight, but I'm too lazy to look it up.

Otherwise, since cron will be performed daily, you can use yesterdays date to construct a wildcard to use in the ftp script.

You could then use something like JimMcNamara's script:

Code:

YESTERDAY=$(date --date=yesterday +%y%m%d)
SERVERNODE=<ip-address>

ftp user:password@${SERVERNODE} << EOF
cd logs/
del log-${YESTERDAY}
bye
EOF >localcleaninglog-$(date +%y%m%d)


I've used the form "ftp user: password@ipaddress <ftpscript"
in a loop to automate the renaming of files in a large number of devices (5 racks of duets) after we found out that we needed
to change all of the filenames to include leading zeros. It worked from my laptop running linux, but didn't work in windows
or using cygwin in windows. Also, the --date=yesterday is a GNU option of date.



All times are GMT -5. The time now is 04:36 PM.