It is good to have automatic backups of critical servers. An easy way to make these backups is with a utility called rsync, but if you have to back up to a server that only supports FTP, here's a different approach.

Step-by-step guide


Some commands may require root permission, so log in as a standard user and use sudo.


  1. Install lftp

    yum install lftp
  2. Create the backup script

    1. Create the new file

      You can save the file anywhere you like, it doesn't have to be in the example location we are using here.

      nano /home/admin/Documents/backup.sh
    2. Insert the script contents into the file

      #!/bin/bash
      
      # Backs up files to remote FTP server
      
      HOST='server.example.com'
      USER='username'
      PASS='password'
      TARGETFOLDER='/target'
      SOURCEFOLDER='/source'
      
      lftp -f "
      open $HOST
      user $USER $PASS
      lcd $SOURCEFOLDER
      mirror --reverse --delete --verbose $SOURCEFOLDER $TARGETFOLDER
      bye
      "
    3. Replace 'server.example.com' with the IP or FQDN of the server to the FTP server where you will be sending files.
    4. Fill in the FTP username and password next.
    5. Replace '/target' with the location on the FTP server where you want the files sent.
    6. Replace '/source' with the location on the local machine where the files are that you want backed up.
    7. Don't change anything below the source folder line.
    8. Save the file.
    9. Make the file executable

      chmod +x /home/admin/Documents/backup.sh
  3. Add the file to cron to make it execute automatically on a schedule.

    The example below will execute every day at 2:00 AM.

    0 2 * * * /home/admin/Documents/backup.sh
  4. You can run the script manually to verify that it works.

    sh /home/admin/Documents/backup.sh
  5. That's it! Everything should be all set.