As part of tidying up after a virtualisation of a physical Redhat ES4 server – I needed a mechanism to transfer nightly backups to a location where they could be backed up for offsite storage. I wanted a solution we could simply drop in to the current scripts and the simple solution that we came up with was to use the functionality of the .netrc file to process the archive and have it transferred in binary mode for later recovery.
The first thing you will need is the address of the ftp server and the username and password of the account that you are using. test that you can logon correctly with the ftp client
ftp hostname (press return)
enter password when prompted
If you get a successful logon then you are ready to put the details in a file called .netrc
use vi .netrc
to open the file then enter the following line
machine 192.168.0.1 login the_ftp_account_name password the_ftp_account_password
where these are the three items mentioned at the start.
In order to test that the script can login automatically use
ftp hostname and press return
if the login is successful you will see that in the output from the ftp command – if you get a message about incorrect mode – you made need to use the command
chmod 400 .netrc
or
chmod 600 .netrc
depending on which flavour of linux / unix you are on.
Retry the ftp hostname command and you should be rewarded with a successful login
Now comes the useful bit. In the .netrc file you can define macros or sequences of commands that can be used when in an interactive ftp session or in this case an automated session.
Open the .netrc file again in vi
below the line starting machine enter
macdef uploadtest
bin
put test.tar
quit
Leave a blank line after that quit command. Save the file in vi and test from the command line using
echo “$ uploadtest” | ftp -v 192.168.0.1
which in essence says launch an ftp session to the host 192.168.0.1 and if logged on successfully run the macro in .netrc called uploadtest
the macro instructs the ftp client to change the file transfer mode to binary, send the file test.tar and finally quit (close) the ftp session.
The -v flag on the ftp command just gives you some more feedback from the transfer.