Backing up means saving copies of your important files so you don't lose them. Recovery means using those copies to get your files back if something goes wrong.
0
0
Backup and recovery strategies in Raspberry Pi
Introduction
You want to save your Raspberry Pi project files before trying new code.
You want to protect your photos and documents stored on your Raspberry Pi.
You want to restore your system after a power failure or accidental deletion.
You want to move your Raspberry Pi setup to a new SD card without losing data.
Syntax
Raspberry Pi
rsync -av --delete /source/directory/ /backup/directory/ # or using dd to copy SD card: sudo dd if=/dev/mmcblk0 of=/path/to/backup.img bs=4M status=progress
rsync copies files and keeps them updated between folders.
dd makes a full image copy of your SD card for complete backup.
Examples
This copies your projects folder to a USB drive, keeping all files and folders.
Raspberry Pi
rsync -av /home/pi/projects/ /media/usb/backup_projects/
This creates a full image backup of your Raspberry Pi SD card to a file.
Raspberry Pi
sudo dd if=/dev/mmcblk0 of=/home/pi/backup.img bs=4M status=progress
This syncs your home folder to backup and deletes files in backup that are removed from source.
Raspberry Pi
rsync -av --delete /home/pi/ /media/usb/backup_pi/
Sample Program
This script copies your projects folder to a USB backup folder. It creates the backup folder if needed and shows a message when done.
Raspberry Pi
#!/bin/bash # Simple backup script for Raspberry Pi SOURCE_DIR="/home/pi/projects" BACKUP_DIR="/media/usb/backup_projects" # Create backup directory if it doesn't exist mkdir -p "$BACKUP_DIR" # Run rsync to copy files rsync -av --delete "$SOURCE_DIR/" "$BACKUP_DIR/" echo "Backup completed from $SOURCE_DIR to $BACKUP_DIR"
OutputSuccess
Important Notes
Always test your backups by restoring some files to make sure they work.
Use external drives or cloud storage to keep backups safe from hardware failure.
Schedule regular backups using cron jobs to automate the process.
Summary
Backup saves copies of your important files to protect against loss.
Recovery uses backups to restore your files or system after problems.
Use tools like rsync for file backups and dd for full SD card images.