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.
Debian/Ubuntu
-
Install the necessary tools: apt install -y lftp mariadb-client - --- Create the script /usr/local/bin/backup_ftp.sh ---
- #!/bin/bash
- DATE=$(date +%Y-%m-%d)
- BACKUP_DIR=/tmp/backups/$DATE
- FTP_HOST="ftp.example.com"
- FTP_USER="utilisateur_ftp"
- FTP_PASS="ftp_password"
- FTP_DIR="/backups/$(hostname)"
- mkdir -p $BACKUP_DIR
- --- Backup of all databases ---
-
mysqldump -u root --all-databases | gzip > $BACKUP_DIR/db_$DATE.sql.gz - --- Backing up web files ---
-
tar -czf $BACKUP_DIR/web_$DATE.tar.gz /var/www/html/ - --- Sending to FTP server with lftp ---
- lftp -u $FTP_USER,$FTP_PASS $FTP_HOST <<EOF
- mkdir -p $FTP_DIR
- put $BACKUP_DIR/db_$DATE.sql.gz -o $FTP_DIR/db_$DATE.sql.gz
-
put $BACKUP_DIR/web_$DATE.tar.gz -o $FTP_DIR/web_$DATE.tar.gz - bye
- EOF
- --- Cleaning local temporary files ---
- rm -rf $BACKUP_DIR
- --- Make the script executable ---
-
chmod +x /usr/local/bin/backup_ftp.sh - --- Schedule with cron (every day at 3am) ---
-
crontab -e puis ajouter : - 0 3 * * * /usr/local/bin/backup_ftp.sh >> /var/log/backup_ftp.log 2>&1
- --- Test manually ---
-
/usr/local/bin/backup_ftp.sh && echo "Sauvegarde OK" - --- Rotation: delete FTP backups older than 30 days ---
- Add in the script before EOF:
- find $FTP_DIR -name "*.gz" -mtime +30 -delete