Secure your precious data and system settings with these easy steps.
Learn how to create a backup of your Raspberry Pi’s SD card to protect against hardware failure or data loss. …
Updated September 23, 2023
Learn how to create a backup of your Raspberry Pi’s SD card to protect against hardware failure or data loss.
Raspberry Pi is an affordable and versatile single-board computer that can be used for various projects like home automation, media centers, and much more. However, it requires careful handling since any issues with the SD card can lead to data loss or system failure. To prevent such incidents, it’s essential to learn how to backup your Raspberry Pi’s SD card regularly. Here are some simple steps you can follow:
Prepare the backup medium: Before starting the backup process, make sure you have a separate microSD card or USB drive with enough space to store all the data from the original SD card. You can use tools like
dd
orrsync
to perform the backup.Identify the device names: Use the command
lsblk
to list all the available storage devices and identify the one you want to back up. Make a note of the device name, as it will be required in the next steps. For example, if your SD card is/dev/mmcblk0p1
, make a note of this for future reference.Unmount the partitions: Before starting the backup process, make sure to unmount any mounted partitions on the device. You can use the
umount
command followed by the partition name. For example, if your SD card has two partitions/dev/mmcblk0p1
and/dev/mmcblk0p2
, run:
sudo umount /dev/mmcblk0p1
sudo umount /dev/mmcblk0p2
- Backup using
dd
: If you’re comfortable with command line tools, you can use thedd
command to create an exact copy of your SD card. Make sure to replace/path/to/backup/file.img
and/dev/mmcblk0
with appropriate values based on your device name:
sudo dd if=/dev/mmcblk0 of=/path/to/backup/file.img bs=4M status=progress conv=fsync
This command will create a byte-for-byte copy of the SD card into an image file, which can be restored later using the same dd
command or other tools like Etcher.
- Backup using
rsync
: Alternatively, you can use thersync
command to backup only the necessary files and folders. This is useful if you want a more organized backup and don’t need the entire SD card content. First, create a mount point for your backup drive:
sudo mkdir /mnt/backup
Next, mount your backup drive or SD card to this location using its device name:
sudo mount /dev/sdX1 /mnt/backup
Finally, run the rsync
command to copy only the required folders. For example, to back up the /home
, /etc
, and /opt
directories:
sudo rsync -avz --progress /home /etc /opt /mnt/backup/
Remember to replace /dev/sdX1
with your backup drive or SD card device name. This command will copy the contents of these folders while preserving permissions and ownership.
- Verify the backup: Once the backup process is complete, check the integrity of the backup by comparing the original and copied files using tools like
sha256sum
ordiff
. For example:
sha256sum /dev/mmcblk0p1 | cut -d ' ' -f 1 > original_checksum.txt
sha256sum /path/to/backup/file.img | cut -d ' ' -f 1 > backup_checksum.txt
diff original_checksum.txt backup_checksum.txt
This will help you ensure that the backup is accurate and no data has been corrupted during the process.
- Schedule regular backups: It’s important to regularly update your SD card backup to prevent data loss. You can automate this process using cron jobs or third-party backup tools like PiBackup. Remember to test your backup regularly to ensure it works as expected.
By following these steps, you can create a reliable backup of your Raspberry Pi’s SD card and restore it in case of any issues. Always keep multiple copies of your backups in different locations to avoid data loss due to hardware failures or other unforeseen events.