Skip to main content

Actual Backup from Kronos FTP to Famex NAS

We have created a Bash script that connects to an FTPS server, lists the files in a specific remote directory, identifies the most recent file, and downloads it to a local directory using lftp. This script can be automated to run regularly using cron.

Script Location: /opt/scripts/ftp_to_smb.sh

Bash Script Content

#!/bin/bash
# FTP Authentication Details
FTP_URL="ftp://kronosit.ddns.net:8462"  # FTPS server URL
FTP_USER="FAMEX_FTP"  # FTP username
FTP_PASS="h6|1HbJW"  # FTP password (handle securely in production environment)
FTP_REMOTE_DIR="/FTP/U71_Famex_Actual_Backup/"  # Remote directory on the FTPS server

# Local Destination Directory on SMB
SMB_DEST_DIR="/mnt/IT/04 - BiztonsagiMentes/Actual"  # Local destination directory

# Ensure the local destination directory exists
mkdir -p "$SMB_DEST_DIR"

# Step 1: Use lftp to list files in the remote directory and store the result
file_list=$(lftp -u "$FTP_USER,$FTP_PASS" "$FTP_URL" -e "
set ftp:ssl-force true;  # Enforce SSL/TLS for control and data connections
set ftp:ssl-protect-data true;  # Encrypt data transfers
set ssl:verify-certificate no;  # Disable certificate verification
cls -1 $FTP_REMOTE_DIR;  # List files in the remote directory
quit")

# Step 2: Sort files and identify the most recent one
latest_file=$(echo "$file_list" | sort | tail -n 1)  # Select the latest file by sorting

# Check if a file was found
if [ -z "$latest_file" ]; then
  echo "No file found in the remote directory."
  exit 1
fi

# Extract only the filename from the path (if needed)
file_name=$(basename "$latest_file")  # Extract only the filename

# Step 3: Download the latest file using lftp
lftp -u "$FTP_USER,$FTP_PASS" "$FTP_URL" -e "
set ftp:ssl-force true;  # Enforce SSL/TLS for connections
set ftp:ssl-protect-data true;  # Protect data transfer with SSL/TLS
set ssl:verify-certificate no;  # Disable certificate verification (use cautiously)
cd $FTP_REMOTE_DIR;  # Change to the remote directory
get '$latest_file' -o '$SMB_DEST_DIR/$file_name';  # Download the latest file
quit"

# Log the operation
echo "$(date '+%Y-%m-%d %H:%M:%S') - Downloaded latest file: $file_name" >> /var/log/ftp_to_smb_backup.log

Step Explanations

FTP Authentication Details and Paths: The script defines the FTP server URL (FTP_URL), user authentication details (FTP_USER and FTP_PASS), remote directory (FTP_REMOTE_DIR), and the local directory where files will be placed (SMB_DEST_DIR).

Creating Local Directory: mkdir -p "$SMB_DEST_DIR": Creates the local directory if it does not already exist.

Listing Files with lftp: The lftp command lists all files in the remote directory using secure settings (ssl-force, ssl-protect-data, ssl:verify-certificate).

Identifying the Latest File: The script sorts the file list and selects the latest one.

Downloading the Latest File: The lftp command downloads the latest file to the local directory using the get command.

Logging the Operation: Each run logs the name, date, and time of the downloaded file.

Automating the Script with cron To run the script automatically at 1:00 AM every day, add the following line to the root crontab:

Open the cron editor:

sudo crontab -e

Add this line:

0 1 * * * /opt/scripts/ftp_to_smb.sh

This will execute the script every day at 1:00 AM.

Checking the Downloaded File Verify that the file was successfully downloaded to the local destination directory:

ls -l "/mnt/IT/04 - BiztonsagiMentes/Actual"

Log File The script logs its operations to /var/log/ftp_to_smb_backup.log. To view the log, use:

cat /var/log/ftp_to_smb_backup.log

Security Considerations

  • Authentication Details: Be careful when storing credentials directly in the script. Consider using more secure methods like environment variables or a secure vault.

  • --insecure Option: Disabling certificate verification (set ssl:verify-certificate no) can be helpful for testing but should be used cautiously in production.

Summary

This script automates the download of the latest file from an FTPS server to a local directory, enabling regular backups or transfers without manual intervention. By using lftp, the script efficiently and securely handles FTPS connections.