Scripting & Automation (Raspberry Pi 5) · Backup to NAS Server

Backup to NAS Server

Automate backups from Raspberry Pi 5 to a Raspberry Pi 3 NAS with SSH keys, proper permissions, an rsync-based Bash script, and weekly scheduling via cron.

Goal: back up the Automation directory from the Pi 5 to a Pi 3 NAS. Steps: generate SSH keys → copy the public key to the NAS → confirm key-based login → set directory permissions → write the backup script → make it executable → run it → verify on the NAS → schedule weekly.

1) Generate SSH key pair

Generate SSH key pair

2) Copy the public key to the NAS

Copy public key to NAS

3) Confirm key-based SSH login

SSH login using key

4) Verify user context with whoami.

whoami confirmation

5) Grant write permissions on NAS path

Ensure the user referenced by the script can write to /mnt/nasdrive on the NAS.

Grant write access to NAS directory

6) Write the backup script

Open nano and create the script: set variables, check directory access, run rsync with logging, and print success/fail status.

Backup script code (rsync to NAS)

7) Make the script executable

chmod +x on the backup script

8) Run the script

Manual run of backup script

9) Verify on the NAS

Backup result shown on NAS

10) Schedule weekly via cron

Open crontab editor
Add weekly cron entry

Back to Home