Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/bin/sh
HOST='hostname'
USER='sd-xxx'
PASSWD='password'
ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt
cd /Backup/dmp
mput *.dmp
# delete 02 days old backup
find /Backup/dmp/* -name "*.dmp" -ctime +2 -exec rm -rf {} \;
bye
EOT
When I test as CentOS command line. Its giving output as following:
Code:
## Making dmp backup--working fine
Trying 62.210.17.46...
Connected to xxx.net (62.210.xx.xx).
220 server ready - login please
530 login first
331 password required
230 login accepted
Interactive mode off.
250 OK. Current directory is /Backup/dmp
local: GSW-05-07-2016.dmp remote: GSW-05-07-2016.dmp
227 Entering Passive Mode (62,210,17,4,191,237)
150 Accepted data connection
226 0.199 seconds (measured here), 60.58 Mbytes per second
12836220 bytes sent in 0.199 secs (64559.10 Kbytes/sec)
local: MI-05-07-2016.dmp remote: MI-05-07-2016.dmp
227 Entering Passive Mode (62,210,17,4,30,126)
150 Accepted data connection
226 17.526 seconds (measured here), 59.81 Mbytes per second
1111816414 bytes sent in 17.5 secs (63441.74 Kbytes/sec)
?Invalid command
?Invalid command
221 goodbye
Comman Line prompt:
===================
Its making backup and moving as well as uploading to ftp server but not removing old files
When I use it in crontab:
=========================
30 2 * * * sh /var/oracle/dmp-backup-script/dmp.sh>>dmp.log | mail -s "dmp backup tried. check log" xxx@gmail.com
Then its only making backup and quit from the script.
Ignore your script for a minute, and just run your ftp commands manually:
Code:
ftp -n -v hostname
ascii
user sd-xxx password
prompt
cd /Backup/dmp
mput *.dmp
# delete 02 days old backup
find /Backup/dmp/* -name "*.dmp" -ctime +2 -exec rm -rf {} \;
bye
and see what happens.
Also, you should not be using names in all caps for local variables. All caps is reserved for environment variables, using it for local variables can cause all kinds of unexpected conflicts. For example, $USER is already an environment variable that contains the username of the user that is running the script. You're overwriting this variable with your ftp user name, which means anything down the line that references $USER for any reason, will receive your ftp user name instead of the local system's user name, which can cause all kinds of problems.
As stated in your other thread ftp does have the capabilities to delete files and directories. It is not as simple as the find command and what makes it more difficult is if you do not run the script everyday.
Ignore your script for a minute, and just run your ftp commands manually:
Code:
ftp -n -v hostname
ascii
user sd-xxx password
prompt
cd /Backup/dmp
mput *.dmp
# delete 02 days old backup
find /Backup/dmp/* -name "*.dmp" -ctime +2 -exec rm -rf {} \;
bye
and see what happens.
Also, you should not be using names in all caps for local variables. All caps is reserved for environment variables, using it for local variables can cause all kinds of unexpected conflicts. For example, $USER is already an environment variable that contains the username of the user that is running the script. You're overwriting this variable with your ftp user name, which means anything down the line that references $USER for any reason, will receive your ftp user name instead of the local system's user name, which can cause all kinds of problems.
1. You seem to have a problem with the basic tools and utilities. While there are problems with your script, the major issues are your misunderstanding of ftp and other gnu software rather than the shell.
2. Your script has no error checking at all, so there is significant opportunity for much unexpected behavior resulting from a single error.
My advice, after studying your tools and what they can and cannot do, is to consider this only a start. You will want to tune and improve your process for some time as you learn these things.
One thought I had at first glance is that some tool that can be scripted might be a better option. ftp can read and use scripts, but these are scripts of ftp commands and you need to understand those limitations mentioned. lftp is more powerful, handles additional protocols, and has more scripting options: it may be worth consideration.
No matter what tools you use, before deleting files you will want to test the error return from the transport mechanism to ensure that they are safely transferred elsewhere before you delete the local copy. I would also want to add error detection to any called script, and a return code that would tell the parent script something about the results.
If ssh and sftp are options, they provide ways to securely execute a command on the remote host. (your find, perhaps) lftp also supports sftp protocol. If ssh/sftp is an option, those offer more security, greater power, and superior error detection. I often use the putty-tools versions, as they allow options better suited to some kinds of scripting, but the basic tools from OpenSSH work well.
Finally, you are running on CentOS. The default shell is normally bash, with sh being a link to bash that runs in a more posix mode. Using bash explicitly opens up a little more power. You MAY want to consider that, if this does not need to be portable to non-bash based distributions.
ftp has a very limited number of commands which are defined in the link I provided above. find is a command line utility and only available using telnet or ssh.
In your previous thread you stated that ssh was not available by your ISP.
#!/bin/bash
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="ftp.yourserver.com"
ftpuser="loginusername"
ftppass="password"
putdir="/Backup/dmp"
ndays=1
# work out our cutoff date
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
echo removing files older than $MM $DD
# get directory listing from remote source
listing=`ftp -i -n $ftpsite <<EOMYF
user $ftpuser $ftppass
binary
cd $putdir
ls
quit
EOMYF
`
lista=( $listing )
# loop over our files
for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} File: ${lista[`expr $FNO+8`]}
# check the date stamp
if [ ${lista[`expr $FNO+5`]}=$MM ];
then
if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];
then
# Remove this file
echo "Removing ${lista[`expr $FNO+8`]}"
ftp -i -n $ftpsite <<EOMYF2
user $ftpuser $ftppass
binary
cd $putdir
delete ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
fi
done
Here is output:
Code:
removing files older than Jul 06
Trying
Date ftp 12672814 File: 7
Date ftp 12672913 File: 1
Date ftp 12672889 File: 7
Date ftp 1099121178 File: 7
Date ftp 1099121165 File: 1
Date ftp 1099121206 File: 7
Date File:
Removing
Trying 62.210.xx.x...
(remote-file) Could not delete quit: No such file or directory
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.