If you run a smart home (e.g. home assistant on a raspberry) you may have 30+ devices individually configured in your Internet router.
Once this router happens to "die" you are in trouble, if you don't have a decent recent backup of its settings. When this happened to me once, I started "hunting" for a solution to automatically backup my routers settings overnight to the cloud.
Luckily, my Internet router is a Fritz!Box where I found a solution to the backup and another one for storing this backup on GDrive.
Big kudos to Johannes Hubig, who helped me lot with the Fritz!Box back-up itself and the author of myhowto.blog where I found hints how access GDrive from a raspberry. I tried mounting it but downloading the Fritz!Box settings directly to a mounted GDrive crashed the Fritz!Box, when ran as cron job. Thats why the below copy script is needed.
Even though I did not understand much of the details here is how I put all the pieces together:
Prepare the Fritz!Box
To download the Fritz!Box settings via script, the anyway annoying 2FA must be turned off beforehand. Here is >>> howto
Download, "install*" and configure the scripts (fritzBoxShell.sh and fritzBoxShellConfig.sh) required to download the Fritz!Box settings and run them as cron job. See below.
Install and configure rclone to access GDrive for storing the backups
> sudo apt install rclone
> rclone config
Unlike many hints found on the web, that one should create a Google cloud project to get a client ID and secret, it worked straight, when you answer the config questions as follows:
Enter name: gdrive
Select type of storage # 18 (Google Drive)
Option client_id: Leave blank
Option client_secret: Leave blank
Option scope: Select 1
Option service_account_file: Leave blank
Edit advanced config: No (default)
Use auto config?: Yes (default) > Browser opens. Login to your Google Account
You are done
Install a script to sync the backup-directory to a corrosponding GDrive-directory (Source: Copilot) You may want to name the file SyncToGDrive.sh
#!/bin/bash
remote="gdrive:FritzBackup"
# Sync directories
rclone sync --verbose "/home/Username/FritzBackup" "$remote"
Install a script to delete backup-files older than 3 days to avoid data waste (Source: Chat GPT with some trial & error attempts)
You may want to name the file DeleteOlderThan3Days.sh
#!/bin/bash
# Directory from where old files shall be deleted# /home/user/FritzBackup
# Check if directory argument is provided
if [ $# -ne 1 ]; then  Â
echo "Usage: $0 <directory>"Â Â Â exit 1
fi
DIRECTORY=$1
# Check if the given argument is a valid directory
if [ ! -d "$DIRECTORY" ]; then
echo "Error: $DIRECTORY is not a valid directory."Â Â Â exit 1
fi
# Find and delete files older than 3 days
echo "Deleting files in $DIRECTORY older than 3 days..."find "$DIRECTORY" -type f -mtime 3 -exec rm -fv {}Â \;
echo "Deletion completed."
Every AI you ask for this script will use "-mtime +3". It will however only function properly, when you omit the "+" sign. Also important to use "-f "after the rm command.
Run the scripts in cron tab
> crontab -e
# Create a Fritz!Box back-up every night at 02:05, later delete old backups and copy the latest to GDrive
05 2 * * * /home/user/fritzBoxShell.sh BACKUP passwordforbackup
10 2 * * * /home/user/DeleteOlderThan3Days.sh /path/to/folder
15 2 * * * /home/user/SyncToGDrive.sh
Choose a "passwordforbackup" for the Fritz!Box settings file. You will need it in case of a recovery of your Fritz!Box.
*) "Install" means bring the files to your user folder and make them executable
> sudo chmod +x fritzBoxShell.sh
> sudo chmod +x fritzBoxShellConfig.sh
> sudo chmod +x SyncToGDrive.sh
> sudo chmod +x DeleteOlderThan3Days.sh
Comments