VPS1Euro Dashboard Wiki

Complete client guide: infrastructure, services, account and support.

FR EN

Automatic backups to an FTP server

Back up files and databases to a remote FTP server with automatic rotation.

What this tutorial does

The tutorial "Automatic backups to an FTP server" helps you achieve this goal:

Back up files and databases to a remote FTP server with automatic rotation.

← Back to tutorials
1 profiles 33 steps 7 commands ~25 min

Debian/Ubuntu

  1. Install the necessary tools: apt install -y lftp mariadb-client
  2. --- Create the script /usr/local/bin/backup_ftp.sh ---
  3. #!/bin/bash
  4. DATE=$(date +%Y-%m-%d)
  5. BACKUP_DIR=/tmp/backups/$DATE
  6. FTP_HOST="ftp.example.com"
  7. FTP_USER="utilisateur_ftp"
  8. FTP_PASS="ftp_password"
  9. FTP_DIR="/backups/$(hostname)"
  10. mkdir -p $BACKUP_DIR
  11. --- Backup of all databases ---
  12. mysqldump -u root --all-databases | gzip > $BACKUP_DIR/db_$DATE.sql.gz
  13. --- Backing up web files ---
  14. tar -czf $BACKUP_DIR/web_$DATE.tar.gz /var/www/html/
  15. --- Sending to FTP server with lftp ---
  16. lftp -u $FTP_USER,$FTP_PASS $FTP_HOST <<EOF
  17. mkdir -p $FTP_DIR
  18. put $BACKUP_DIR/db_$DATE.sql.gz -o $FTP_DIR/db_$DATE.sql.gz
  19. put $BACKUP_DIR/web_$DATE.tar.gz -o $FTP_DIR/web_$DATE.tar.gz
  20. bye
  21. EOF
  22. --- Cleaning local temporary files ---
  23. rm -rf $BACKUP_DIR
  24. --- Make the script executable ---
  25. chmod +x /usr/local/bin/backup_ftp.sh
  26. --- Schedule with cron (every day at 3am) ---
  27. crontab -e puis ajouter :
  28. 0 3 * * * /usr/local/bin/backup_ftp.sh >> /var/log/backup_ftp.log 2>&1
  29. --- Test manually ---
  30. /usr/local/bin/backup_ftp.sh && echo "Sauvegarde OK"
  31. --- Rotation: delete FTP backups older than 30 days ---
  32. Add in the script before EOF:
  33. find $FTP_DIR -name "*.gz" -mtime +30 -delete