This article provides a simple guide for using LFTP, a powerful command-line FTP client, to automate file backups to a remote FTP server. We will leverage dynamic variables like date and time to create unique backup directories and filenames.
What is LFTP?
LFTP is a robust command-line tool that supports a wide range of features, including:
- FTP and SFTP Support: Transfer files securely over FTP or SFTP protocols.
- Automation: Automate tasks like mirroring directories and managing uploads/downloads.
- Resuming Transfers: Restart interrupted transfers without losing progress.
- Batch Scripting: Use scripts for complex workflows.
Backup Script with Dynamic Variables
Below is a bash script to automate the process of backing up files to a remote FTP server using LFTP.
Script
#!/bin/bash
# Define the variables
DATE=$(date +%Y-%m-%d) # Set DATE to the current date
DATE_TIME=$(date +%Y-%m-%d_%H-%M-%S) # Set DATE_TIME to the current date and time
BACKUP_PREFIX="backup" # Prefix for backup files
HOST_NAME="host-name-or-ip-address" # FTP server address
PORT="port-number" # FTP server port
USER_NAME="remote-ftp-server-user-name" # FTP username
PASSWORD="remote-ftp-server-user-password" # FTP password
LOCAL_BACKUP_PATH="local-path" # Path to the local file or folder to backup
# Start LFTP session
lftp -e "
set ssl:verify-certificate no; # Ignore certificate verification
open ftp://$USER_NAME:$PASSWORD@$HOST_NAME:$PORT;
mkdir -p $DATE; # Create a remote directory for date variable
cd $DATE; # Navigate to the created directory
mput $LOCAL_BACKUP_PATH/$BACKUP_PREFIX-$DATE_TIME.tar.gz; # Upload file
close;
bye;
"
BashScript Breakdown
- Dynamic Variables:
DATE
andDATE_TIME
are dynamically set to ensure backup files are uniquely named and organized by date.- This makes it easier to identify and retrieve specific backups.
- LFTP Commands:
set ssl:verify-certificate no
: Disables certificate verification for SSL connections.mkdir -p $DATE
: Creates a remote directory using the current date as the folder name.mput $LOCAL_BACKUP_PATH
: Uploads the specified file or folder to the FTP server.
- SSL Certificate Verification:
- By default, LFTP verifies SSL certificates for secure connections.
- You can disable this in the script or permanently in the LFTP configuration file.
Permanently Ignoring SSL Certificate Verification
To always ignore SSL certificate verification, modify the LFTP configuration file:
- Open the LFTP configuration file in a text editor:
vim /etc/lftp.conf
- Add the following line:
set ssl:verify-certificate no
- (:x)Save and exit the editor.
Key Notes
- Ensure your local backup path (
LOCAL_BACKUP_PATH
) and remote FTP credentials are set correctly. - Test the script with a small file to verify functionality before automating with cron jobs or similar schedulers.
Using this approach, you can create reliable, automated backups with organized file structures, ensuring that critical data is safely stored on a remote server.
Leave a Reply