You should consider using passwordless ssh connections. This would encrypt the data while it is being transmitted and it would mean that you don't have to put a password into a configuration file. It is considered a bad idea to put passwords into configuration files or into scripts. If you used ssh connections then you could either use scp or sftp to copy the files. If you don't use ssh then you still have the choice between using rcp or ftp to copy the files. If the destination computer is on your LAN then scp or rcp might be easier to configure than ftp or sftp. Nevertheless this is how I would do the job as you described it.
Don't use the root account to do the copying. You should have a normal user account to do the copying. Naturally this account will have to have read access to the file to copy. Let's call this account abcftp and it will have a home directory in /home/abcftp. The abc.txt file will reside in /home/abcftp. For simplicity we will call the script abcftp.sh and it will also reside in /home/abcftp. Also, the user name on the remote computer is abcftp. I'm not able to test this so there may be some debugging required.
The user account should have a .bashrc in its home directory. That .bashrc file should contain the following line.
Code:
export MAILTO=root@localhost
This will determine where the cron daemon will send the log file of the script.
The abcftp.sh file should contain this:
Code:
#! /bin/bash
# This file will use ftp to copy the file /home/abcftp/abc.txt to a remote
# server. It uses the file /home/abcftp/.netrc for remote server
# authentication credentials.
if [[ -e /home/abcftp/abc.txt ]] ; then
ftp -v abcftp@remote.computer
send abc.txt
quit
exit;
else
echo The abc.txt file was not available at `date +%F %H:%M`;
fi
exit
Once you have created this file then you have to change its permissions to allow it to execute. Use the chmod command as follows:
Code:
chmod u+x abcftp.sh
Then you need to create a file called .netrc. It should contain the following lines.
Code:
machine remote.computer
login abcftp
password <password>
The <password> is the real password.
The .netrc file has to be readable by ONLY the abcftp account or else the ftp software will not use it. You use the chmod command to accomplish this.
Code:
chmod u=rw,g-rwx,o-rwx .netrc
You should be able to test this by calling the script as follows.
Code:
/home/abcftp/abcftp.sh
Make sure that is working before you bother doing any of the following.
Next you want to get this to automatically run at a given time. You do this with the cron service. You will create your own crontab file with a line to tell the cron daemon when to run this script. You create a new crontab file or edit an existing one using the crontab command.
The syntax of the crontab records is a little bit confusing because you can have several types of qualifiers regarding the days and times to execute a given command. You can read about the syntax of the crontab file by entering the following command.
Let's say that you want the job to run Monday through Friday at 8:00 p.m. In twenty four hour format the time is 20:00. The crontab entry should look like this.
Code:
* 20 * 1-5 /home/abcftp/abcftp.sh > /home/abcftp/abcftp.log 2>&1
The crontab default editor is vi.
Let us know if you succeeded or if you need help.