🔐 FTP Backup Script – Encrypted & Unencrypted


🔐 Encrypted FTP Backup Script

This guide describes a universal shell script to back up any folder or file, compress and encrypt it with a password, and upload it securely via FTPS (FTP with SSL/TLS).

Use Case: Server admins needing secure, automated, off-site backups with minimal dependencies.

⚙️ Requirements

  • tar – for creating the archive
  • gpg – for encrypting the archive with a password
  • lftp – for FTP over SSL/TLS (FTPS)

🔧 Install on Debian/Ubuntu

sudo apt update
sudo apt install gnupg lftp

💾 The Full Backup Script (Encrypted)

#!/bin/bash
# ========== CONFIGURATION ==========
BACKUP_SOURCE="/var/www"
BACKUP_DIR="/root"
BACKUP_NAME="backup_$(date +%d-%m-%Y_%H-%M-%S).tar.gz"
ENCRYPTED_BACKUP="${BACKUP_NAME}.gpg"
GPG_PASSWORD="your_strong_password"

FTP_HOST="ftp.example.com"
FTP_PORT="21"
FTP_USER="your_ftp_user"
FTP_PASS="your_ftp_password"
FTP_TARGET_DIR="/backups"

# ========== CREATE TAR BACKUP ==========
echo "[*] Creating archive of '$BACKUP_SOURCE'..."
tar -czf "$BACKUP_DIR/$BACKUP_NAME" -C "$(dirname "$BACKUP_SOURCE")" "$(basename "$BACKUP_SOURCE")"

# ========== ENCRYPT BACKUP ==========
echo "[*] Encrypting archive with GPG..."
echo "$GPG_PASSWORD" | gpg --batch --yes --passphrase-fd 0 -c "$BACKUP_DIR/$BACKUP_NAME"

# ========== SECURE FTP UPLOAD ==========
echo "[*] Uploading encrypted backup via FTPS..."
lftp -u "$FTP_USER","$FTP_PASS" -e "
set ftp:ssl-force true
set ftp:ssl-protect-data true
cd $FTP_TARGET_DIR
put $BACKUP_DIR/$ENCRYPTED_BACKUP
bye
" -p "$FTP_PORT" "$FTP_HOST"

# ========== CLEANUP ==========
echo "[*] Cleaning up local backup files..."
rm -f "$BACKUP_DIR/$BACKUP_NAME"
rm -f "$BACKUP_DIR/$ENCRYPTED_BACKUP"

echo "[✓] Encrypted backup completed and uploaded successfully."
exit 0

📅 Automation (Optional)

0 2 * * * /path/to/backup-script.sh >/dev/null 2>&1

🔐 Notes on Security

  • Store passwords securely using tools like pass, gpg-agent, or a secrets manager.
  • Ensure restricted access to sensitive directories like /root.
  • openssl can be used instead of gpg if preferred.

📁 Unencrypted FTP Backup Script

This guide describes a minimal FTP backup script that compresses a folder and uploads it via unencrypted FTP on port 21.

Use Case: Fast uploads to internal or trusted remote FTP servers where encryption is not required.

⚙️ Requirements

  • tar – to compress the folder
  • lftp – for FTP upload

🔧 Install on Debian/Ubuntu

sudo apt update
sudo apt install lftp

💾 The Full Backup Script (Unencrypted)

#!/bin/bash
# ========== CONFIGURATION ==========
BACKUP_SOURCE="/var/www"
BACKUP_DIR="/root"
BACKUP_NAME="backup_$(date +%d-%m-%Y_%H-%M-%S).tar.gz"

FTP_HOST="ftp.example.com"
FTP_PORT="21"
FTP_USER="your_ftp_user"
FTP_PASS="your_ftp_password"
FTP_TARGET_DIR="/backups"

# ========== CREATE TAR BACKUP ==========
echo "[*] Creating archive of '$BACKUP_SOURCE'..."
tar -czf "$BACKUP_DIR/$BACKUP_NAME" -C "$(dirname "$BACKUP_SOURCE")" "$(basename "$BACKUP_SOURCE")"

# ========== FTP UPLOAD ==========
echo "[*] Uploading archive via FTP..."
lftp -u "$FTP_USER","$FTP_PASS" -e "
cd $FTP_TARGET_DIR
put $BACKUP_DIR/$BACKUP_NAME
bye
" -p "$FTP_PORT" "$FTP_HOST"

# ========== CLEANUP ==========
echo "[*] Cleaning up local backup file..."
rm -f "$BACKUP_DIR/$BACKUP_NAME"

echo "[✓] Backup completed and uploaded successfully via FTP."
exit 0

📅 Automation (Optional)

0 3 * * * /path/to/ftp-backup.sh >/dev/null 2>&1

⚠️ Security Notice

  • This script sends credentials and files unencrypted.
  • Only use this on trusted networks or isolated infrastructure (e.g. LAN/VPN).
  • For sensitive data, use the encrypted FTPS version above.