How to Set Up Automatic pfSense Backups to Google Drive

Keeping regular backups of your pfSense configuration is very important. In this guide, I’ll show you how to automatically back up pfSense to Google Drive using Rclone on Ubuntu/Linux.

Step 1: Install Rclone

Run the following command to install Rclone:

 
curl https://rclone.org/install.sh | sudo bash

Step 2: Configure Rclone for Google Drive

Start the configuration:

 
rclone config

Follow these options:

  1. Press n for a new remote.

  2. Give it a name (e.g., gdrive).

  3. Choose drive for Google Drive.

  4. Leave client_id and client_secret blank.

  5. For scope, choose 1 (full access).

  6. Leave root_folder_id and service_account_file blank.

  7. Press n for “Edit advanced config”.

  8. Press y for “Use auto config” (a browser will open).

  9. Log in with your Google account and allow access.

  10. Back in the terminal, confirm settings.

  11. Press q to quit.


Step 3: Create Backup Script

 Create a script file, for example:

 
nano /usr/local/bin/pfsense-backup.sh

Paste the script below (update with your own pfSense details):

 
#!/bin/bash

# --- pfSense Configuration ---
PFSENSE_IP="PFSENSEIP:PORT"
PFSENSE_USER="USER"
PFSENSE_PASS="PASSWORD"

# --- Google Drive Configuration ---
GDRIVE_REMOTE="gdrive"
GDRIVE_FOLDER="pfSense_Backups"

# --- Backup Filename ---
BACKUP_FILENAME="config-pfsense-$(date +%Y%m%d%H%M%S).xml"

# --- Temporary Files ---
COOKIE_FILE="/tmp/pfsense_cookies.txt"
BACKUP_DIR="/tmp/pfsense_backups"
mkdir -p "$BACKUP_DIR"

# --- Login & Backup ---
CSRF_TOKEN=$(curl -s -k --cookie-jar "$COOKIE_FILE" "https://$PFSENSE_IP/" | grep "name='__csrf_magic'" | sed 's/.*value="\([^"]*\)".*/\1/')
curl -s -k --cookie "$COOKIE_FILE" --cookie-jar "$COOKIE_FILE" \
  --data-urlencode "__csrf_magic=$CSRF_TOKEN" \
  --data-urlencode "login=Login" \
  --data-urlencode "usernamefld=$PFSENSE_USER" \
  --data-urlencode "passwordfld=$PFSENSE_PASS" \
  "https://$PFSENSE_IP/" > /dev/null

CSRF_TOKEN=$(curl -s -k --cookie "$COOKIE_FILE" "https://$PFSENSE_IP/diag_backup.php" | grep "name='__csrf_magic'" | sed 's/.*value="\([^"]*\)".*/\1/')
curl -s -k --cookie "$COOKIE_FILE" \
  --data-urlencode "__csrf_magic=$CSRF_TOKEN" \
  --data-urlencode "donotbackuprrd=yes" \
  --data-urlencode "backupssh=yes" \
  --data-urlencode "download=download" \
  "https://$PFSENSE_IP/diag_backup.php" -o "$BACKUP_DIR/$BACKUP_FILENAME"

# --- Upload to Google Drive ---
rclone copy "$BACKUP_DIR/$BACKUP_FILENAME" "$GDRIVE_REMOTE:$GDRIVE_FOLDER"

# --- Clean Up ---
rm -f "$COOKIE_FILE"
rm -f "$BACKUP_DIR/$BACKUP_FILENAME"
Make the script executable:
chmod +x /usr/local/bin/pfsense-backup.sh

Step 4: Automate with Cron

You can schedule automatic backups with cron.

For the current user:

 
crontab -e

Or for the whole system:

 
sudo nano /etc/crontab

Add this line to run backup daily at 2 AM:

0 2 * * * /usr/local/bin/pfsense-backup.sh

🎉 Done!

Now your pfSense configuration will automatically back up to Google Drive. This ensures your settings are always safe in case of system failure

👉 Watch the full step-by-step process in my YouTube video for better understanding.

Leave a Reply

Your email address will not be published. Required fields are marked *