To create a cron job for automatic FTP backups on RHEL or CentOS Linux, you can use the crontab
command to schedule a script that uses the ftp
command to transfer files from the local system to a remote FTP server.
Here is an example of a script that can be used for automatic FTP backups:
#!/bin/bash # Set the FTP server, username, and password FTP_SERVER=ftp.example.com FTP_USER=user FTP_PASSWORD=password # Set the local and remote directories LOCAL_DIR=/path/to/local/dir REMOTE_DIR=/path/to/remote/dir # Connect to the FTP server and log in ftp -inv $FTP_SERVER <<EOF user $FTP_USER $FTP_PASSWORD # Change to the remote directory cd $REMOTE_DIR # Transfer all files in the local directory to the remote directory mput $LOCAL_DIR/* # Disconnect from the FTP server bye EOF
This script connects to the FTP server ftp.example.com
as the user user
with the password password
, changes to the remote directory /path/to/remote/dir
, and transfers all files in the local directory /path/to/local/dir
to the remote directory.
To schedule this script as a cron job, you can use the crontab
command to add a line to the crontab file for the current user. The crontab file is a configuration file that specifies when and how often cron jobs should be run.
For example, to schedule the FTP backup script to run every day at 1:00 AM, you can use the following command:
echo "0 1 * * * /path/to/ftp-backup.sh" | crontab -
This command will add a line to the crontab file that runs the ftp-backup.sh
script every day at 1:00 AM.
You can use the crontab -l
command to list the current crontab file, and the crontab -e
command to edit the crontab file in a text editor.
For more information about cron and the crontab file format, you can consult the crontab
man page by running the man crontab
command.
Keep in mind that you may need to configure the firewall and the FTP server to allow incoming FTP connections from the local system, in order for the FTP backups to work. You may also need to set up passwordless login with SSH key pairs, if you want to automate the FTP backups without having to enter a password.