Automating website backups is essential for maintaining security and minimizing the risk of data loss. This guide will show you how to automate backups using FTP scripts, with real examples and scheduling tips.

Website backups are crucial for disaster recovery, but doing them manually can be tedious and error-prone. Automating this process ensures that backups are consistently performed without the need for intervention. Using FTP scripts to automate backups allows you to:
Before you begin, there are a few prerequisites you need to ensure you have:
Now that you have the prerequisites covered, let's walk through how to create an FTP backup script to automate the backup process.
1. Open your terminal and create a new bash script file:
nano ftp-backup.sh
2. Add the following FTP backup script code:
#!/bin/bash
# FTP server credentials
HOST='ftp.yourdomain.com'
USER='yourFTPusername'
PASSWD='yourFTPpassword'
REMOTE_DIR='/public_html/'
LOCAL_DIR='/path/to/backup/folder/'
# Current date for timestamp
DATE=$(date +%Y-%m-%d)
# Backup filename
BACKUP_FILE="backup_$DATE.tar.gz"
# Create a backup of the remote directory
tar -czf $LOCAL_DIR$BACKUP_FILE $REMOTE_DIR
# FTP commands to upload the backup
ftp -inv $HOST <<EOF
user $USER $PASSWD
binary
put $LOCAL_DIR$BACKUP_FILE
bye
EOF
# Print success message
echo "Backup complete: $BACKUP_FILE uploaded"
3. Save and close the script (press CTRL+X, then press Y to save and Enter).
4. Make the script executable:
chmod +x ftp-backup.sh
1. Create a new text document and rename it to ftp-backup.bat.
2. Add the following batch script code:
@echo off
SET HOST=ftp.yourdomain.com
SET USER=yourFTPusername
SET PASSWD=yourFTPpassword
SET REMOTE_DIR=/public_html/
SET LOCAL_DIR=C:\path\to\backup\folder\
REM Get current date
FOR /F "tokens=1-4 delims=/ " %%A IN ('date /t') DO SET DATE=%%D-%%B-%%C
REM Backup filename
SET BACKUP_FILE=backup_%DATE%.tar.gz
REM Create a backup of the remote directory
tar -czf %LOCAL_DIR%%BACKUP_FILE% %REMOTE_DIR%
REM FTP commands to upload the backup
ftp -n -i -s:ftp-script.txt
3. Create the FTP script file ftp-script.txt with the following content:
user yourFTPusername yourFTPpassword
binary
put C:\path\to\backup\folder\backup_%DATE%.tar.gz
quit
4. Save and run the batch file to test the script.
Once you have written your FTP backup script, the next step is to set up a scheduled task to run the backup automatically.
1. Open your crontab configuration file:
crontab -e
2. Add a line to schedule the backup. For example, to run the backup every day at 2 AM:
0 2 * * * /path/to/ftp-backup.sh
1. Open Task Scheduler and click on Create Basic Task.
2. Follow the prompts to name the task and select a trigger. Set it to run daily at a specified time.
3. In the Action section, select Start a Program, then browse to the ftp-backup.bat file.
4. Complete the wizard, and your backup will run automatically at the scheduled time.
Here are a few example schedules and backup scenarios you can implement for your site:
Schedule: Run the backup every day at 2 AM.
0 2 * * * /path/to/ftp-backup.sh
Schedule: Run the backup every Sunday at 4 AM.
0 4 * * 0 /path/to/ftp-backup.sh
Scenario: Run a backup before performing updates on plugins or themes.
#!/bin/bash
# Backup script
/path/to/ftp-backup.sh
# Run WordPress update command or manual update
While you can store backups locally, it's always a good idea to keep them offsite for extra security. Here are a few storage options:
Automating website backups using FTP (File Transfer Protocol) scripts ensures your site is always protected. By setting up scripts and schedules, you can streamline the backup process, reduce human error, and maintain your website’s security efficiently. Regular backups are essential for disaster recovery and peace of mind.
RSH Web Services pre-made WHMCS knowledgebase articles provide clear, visual guides for cPanel FTP setup, praised for their user-friendliness. WHMCS Knowledge Base Articles
Tweet Share Pin Email
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments