virdi / pi-clone

A shell script to clone a running Raspberry Pi SD card to a USB mounted SD card.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This script is based on a earlier version called rpi-clone by Bill Wilson https://github.com/billw2/rpi-clone

I've just added to restore mode and made some minor changes (e.g. --yes-to-all mode) Kolja


pi-clone is a shell script that will back up (clone using dd and rsync) a running Raspberry/Banana Pi file system to a destination SD card 'sdN' plugged into a Pi USB port (via a USB card reader). I use it to maintain backups of several Pi SD cards I have and the destination backup SD cards can be a different size (smaller or larger) than the booted SD card.

pi-clone can clone the running system to a new SD card or can incrementally rsync to existing backup Raspberry Pi SD cards. During the clone to new SD cards, pi-clone gives you the opportunity to give a label name to the partition 2 so you can keep track of which SD cards have been backed up. Just stick a correspondingly named sticky label on each SD card you have and you can look up the last clone date for that card in the pi-clone log file /var/log/pi-clone. My convention for my set of cards is to name 8GB cards: SD-RPI-8A, SD-RPI-8B, ... and similarly, 4GB cards: SD-RPI-4A, ...

If the destination SD card has an existing partition 1 and partition 2 matching the running partition types, pi-clone assumes (unless using the -f option) that the SD card is an existing backup with the partitions properly sized and set up for a Raspberry Pi. All that is needed is to mount the partitions and rsync them to the running system.

If these partitions are not found (or -f), then pi-clone will ask if it is OK to initialize the destination SD card partitions. This is done by a partial 'dd' from the running booted device /dev/mmcblk0 to the destination SD card /dev/sdN followed by a fdisk resize and mkfs.ext4 of /dev/sdN partition 2. This creates a completed partition 1 containing all boot files and an empty but properly sized partition 2 rootfs. The SD card partitions are then mounted on /mnt/clone and rsynced to the running system.

In addition to that you can restore your system to another disk. I don't have enough SD cards to have a "running" and a "backup" card for each pi, but I do have a lot of usb flash drives. Calling it "pi-clone sda" will copy everything to my USB-flash drive. After destroying my system I just remove my SD card go to another pi, insert USB-flash drive and the messed up SD-Card (via cardreader) and run "pi-clone sda sdb" to restore everything. (Just ensure that the first drive (in the case "sda") will be source and the second destination for your files).

You should avoid running other disk writing programs when running pi-clone, but I find pi-clone works fine when I run it from a terminal window. However I usually do quit my browser first because a browser can be writing many temporary files.

pi-clone must be run as root and you must have the rsync program installed.

After pi-clone is finished with the clone it pauses and asks for confirmation before unmounting the cloned to SD card. This is so you can go look at the clone results or make any custom final adjustments if needed. For example, I have a couple of Raspberry Pis and I use one as a master. When I clone for the benefit of the second Pi, I do a "cd /mnt/clone/etc" and fix the files needed to customize for the second Pi. This can be as simple as loading new hosts and network interfaces file. Just keep all the files on the master:

/etc/hostname		# hostname is trivial, so don't really need the
/etc/hostname.rpi0  # multiple copies - but I'll just list it here.
/etc/hostname.rpi1
/etc/hosts
/etc/hosts.rpi0
/etc/hosts.rpi1
/etc/network/interfaces
/etc/network/interfaces.rpi0
/etc/network/interfaces.rpi1

In my case, rpi0 is my master so I really don't need the .rpi0 copies, but I keep them just in case. Then when cloning to update for rpi1, I just copy the .rpi1 files over before letting pi-clone umount everything. But don't forget to cd out of the /mnt/clone tree before telling pi-clone to unmount.

pi-clone is on github, to get it and install it to /usr/local/sbin: Go to https://github.com/KoljaWindeler/pi-clone and download the zip file:

$ unzip pi-clone-master.zip
$ cd pi-clone-master
$ cp pi-clone /usr/local/sbin

or, use git to clone the repository:

$ git clone https://github.com/KoljaWindeler/pi-clone.git 
$ cd pi-clone
$ cp pi-clone /usr/local/sbin

About

A shell script to clone a running Raspberry Pi SD card to a USB mounted SD card.


Languages

Language:Shell 100.0%