Last active
May 8, 2022 00:59
-
-
Save ipatch/94a3ee4e6213a03ba95d740fc6c95095 to your computer and use it in GitHub Desktop.
Clone Raspberry Pi SD card to image file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### | |
# OBJECTIVE > A GitHub gist attempting to describe how one could backup the | |
# ...entirity of a Raspbian OS install to a sparse image file. | |
## | |
# | |
#### | |
# REQUIREMENTS | |
# 1) raspberry pi | |
# 2) SD card | |
# 3) A seperate computer or SD card to mount the original Raspbian OS | |
## | |
# | |
#### | |
# SETUP | |
# 1) Install Raspbian OS to an SD card using preferred method, ie. Etcher or | |
# ...some other method. | |
# 2) Install, update, software in Raspbian, make and save some configuration changes | |
# 3) Power off the Pi, remove the SD card, mount the SD via a card reader, | |
# ...or mount the SD card on a different computer preferably running Linux, or | |
# ...or have another SD card that has enough space to backup the entirity of SD | |
# ...card #1, ie. preferably have a 128GB SD card to backup a 64GB SD. | |
# NOTE during the initial backup phase 64GB of space will be used on SD card #2 | |
# ...or computer backing up the initial SD card, ie. SD card #1. | |
## | |
# | |
#### | |
# BACKING UP > creating a sparse image of an entire SD card, ie. | |
# ...both the 1) `boot` and the 2) `root` partitions into a single file. | |
# NOTE the majority of commands will require super user privs via `su` or `sudo` | |
## | |
# | |
#### | |
# TL;DR Steps to reproudce | |
# 1) zeros will need to be written to empty space (null bytes) on the partitions | |
# 2) the large single file containing the zero bytes will need be deleted | |
# 3) sync the file system changes with the local OS | |
# 4) unmount the file system(s) | |
# 5) create a sparse image file of the entire disk containing all partitions | |
## | |
# | |
#### | |
# Procedure | |
# NOTE path(s) to disks, partitions, and mnt points will vary | |
## | |
# | |
#### | |
# mount SD card #1 from SD card #2 or seperate computer, and then write zeros | |
# ...to boot partition of SD card #1, NOT the BOOT parition of the active system | |
## | |
mount -o rw /dev/sdb1 /home/pi/mnt/backup-sd-boot | |
dd if=/dev/zero of=/home/pi/mnt/backup-sd-boot/zero-file bs=1M | |
sync | |
rm /home/pi/mnt/backup-sd-boot/zero-file | |
## | |
# | |
#### | |
# BACKUP > write zeros to root partition | |
## | |
mount -o rw /dev/sdb2 /home/ip/mnt/backup-sd-root | |
dd if=/dev/zero of=/home/pi/backup-sd-root/zero-file bs=32M | |
sync | |
## | |
# | |
#### | |
# BACKUP > SPARSE > create sparse image file of SD card #1 | |
## | |
dd if=/dev/sdb \ | |
of=/path/to/ext-file-system/to/hold/sparse/image/backup-sd.img \ | |
bs=512 conv=sparse status=progress | |
## | |
# | |
#### | |
# RESTORE > restoring from a backed up image, images can be restored using Etcher | |
## | |
dd if=/path/to/ext-file-system/to/hold/sparse/image/backup-sd.img \ | |
of=/dev/sd[DISK_LETTER] \ | |
bs=32M | |
#### | |
# NOTES | |
## | |
# running `ls /path/to/backup-sd.img` on backed up image file will report the full | |
# ...file size of the SD card backed up however `-s` flag will print the actual | |
# ...block size of the file on disk because the `img` file contains sparse bytes | |
## | |
# Not all file systems handle sparse files, ie. FAT32, and different filesystems | |
#...manage sparse files differently making them incompatible when tranferring | |
#...between file sytems. | |
#### | |
# mount an accessible network partition on a LAN using Raspbian so a local SD | |
# ...card can be backed up to an `.img` file. | |
# | |
# NOTE I'm mounting a partition on a Apple Time Capsule from Raspbian to | |
# ...backup the entire contents of an SD card. | |
# | |
sudo mount -t cifs \ | |
//[IP_ADDRESS_OF_NFS]/[MNT_POINT] \ | |
/path/to/local/mnt \ | |
-o rw,uid=1000,gid=1000 \ | |
-o iocharset=utf8,sec=ntlm,vers=1.0 | |
#### | |
# the below command will backup an entire sd card mounted on a running Raspbian | |
# ...OS to a network mount point that has been mounted locally from the Raspbian | |
# ...OS where the local mount point is a partition / file system elsewhere on a LAN. | |
# NOTE `/dev/sdb` refers to the entire SD card not a single partition | |
## | |
sudo dd \ | |
if=/dev/sdb \ | |
of=/mnt/net/tc/backup/rpi/backup-01.img \ | |
bs=32M conv=noerror,sync status=progress | |
## | |
# | |
#### | |
# CREDIT 💳 > https://superuser.com/a/1097391/148584 | |
## | |
# | |
# cheers 🍻 | |
# @ipatch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment