A very nice way to fix a backup is to make a copy, bootable, of the actual SD card You are using on the Raspberry Pi(RPi). This will be done even when the RPi is up and running !
This application can also be used to make a bootable SSD or USB drive !!
You clone from the command line so You don't need the standard GUI, Pixel, and the "SD card copier" function.
It is also possible to set up a a scheduled job, cronjob explained later, which for example makes a clone every night at 1 o'clock.
Check SD
You just need a USB SD card reader, with a SD Card, and then You plug the SD Card reader into a free USB port on the RPi.
Log on to the RPi and at a command line type
lsblk
Which checks if the SD Card is "accepted" and also which name it's assigned to. Could be "sda", "sdb" ......
On Github You can find additional info about the application. For example it handles that You are cloning from/to SD Cards with different sizes.
Log on to the RPi and at a command line type
lsblk
Which checks if the SD Card is "accepted" and also which name it's assigned to. Could be "sda", "sdb" ......
On Github You can find additional info about the application. For example it handles that You are cloning from/to SD Cards with different sizes.
Install
Run the script
curl https://raw.githubusercontent.com/geerlingguy/rpi-clone/master/install | sudo bash
If you would like to run installation manually check here.
Manual clone
Execute the clone script with
sudo rpi-clone sda -f
(The USB could be assigned another name than "sda", so use the name You got earlier) and something like this will appear
Booted disk: mmcblk0 15.9GB
Destination disk: sda 15.9GB
---------------------------------------------------------------------------
Part Size FS Label Part Size FS Label
1 /boot 43.5MB fat32 -- 1 43.5MB fat32 --
2 root 15.9GB ext4 cpy_stretch 2 15.9GB ext4 --
---------------------------------------------------------------------------
== Initialize: IMAGE mmcblk0 partition table to sda - forced by option ==
1 /boot (21.5MB used) : IMAGE to sda1 FSCK
2 root (2.0GB used) : RESIZE(15.9GB) MKFS SYNC to sda2
---------------------------------------------------------------------------
Run setup script : no
Verbose mode : no
-----------------------:
** WARNING ** : All destination disk sda data will be overwritten!
: The partition structure will be imaged from mmcblk0.
-----------------------:
Initialize and clone to the destination disk sda? (yes/no):
---------------------------------------------------------------------------
Part Size FS Label Part Size FS Label
1 /boot 43.5MB fat32 -- 1 43.5MB fat32 --
2 root 15.9GB ext4 cpy_stretch 2 15.9GB ext4 --
---------------------------------------------------------------------------
== Initialize: IMAGE mmcblk0 partition table to sda - forced by option ==
1 /boot (21.5MB used) : IMAGE to sda1 FSCK
2 root (2.0GB used) : RESIZE(15.9GB) MKFS SYNC to sda2
---------------------------------------------------------------------------
Run setup script : no
Verbose mode : no
-----------------------:
** WARNING ** : All destination disk sda data will be overwritten!
: The partition structure will be imaged from mmcblk0.
-----------------------:
Initialize and clone to the destination disk sda? (yes/no):
Answer with yes and type a label for the destination SD card
Optional destination ext type file system label (16 chars max):
XX
Initializing
Imaging past the start of /boot partition 2.
=> dd if=/dev/mmcblk0 of=/dev/sda bs=1M count=50 ...
Resizing last partition to end of disk ...
Resize success.
Changing destination Disk ID ...
Delaying so partprobe can update /dev entries ...
=> fsck -p /dev/sda1 ...
=> mkfs -t ext4 -L XX /dev/sda2 ...
Syncing file systems (can take a long time)
Syncing mounted partitions:
Mounting /dev/sda2 on /mnt/clone
=> rsync // /mnt/clone with-root-excludes ...
Mounting /dev/sda1 on /mnt/clone/boot
=> rsync /boot/ /mnt/clone/boot ...
Editing /mnt/clone/boot/cmdline.txt PARTUUID to use dee316b8
Editing /mnt/clone/etc/fstab PARTUUID to use dee316b8
===============================
Done with clone to /dev/sda
Start - 12:06:06 End - 12:12:36 Elapsed Time - 6:30
Cloned partitions are mounted on /mnt/clone for inspection or customizing.
Hit Enter when ready to unmount the /dev/sda partitions ...
unmounting /mnt/clone/boot
unmounting /mnt/clone
===============================
Initializing
Imaging past the start of /boot partition 2.
=> dd if=/dev/mmcblk0 of=/dev/sda bs=1M count=50 ...
Resizing last partition to end of disk ...
Resize success.
Changing destination Disk ID ...
Delaying so partprobe can update /dev entries ...
=> fsck -p /dev/sda1 ...
=> mkfs -t ext4 -L XX /dev/sda2 ...
Syncing file systems (can take a long time)
Syncing mounted partitions:
Mounting /dev/sda2 on /mnt/clone
=> rsync // /mnt/clone with-root-excludes ...
Mounting /dev/sda1 on /mnt/clone/boot
=> rsync /boot/ /mnt/clone/boot ...
Editing /mnt/clone/boot/cmdline.txt PARTUUID to use dee316b8
Editing /mnt/clone/etc/fstab PARTUUID to use dee316b8
===============================
Done with clone to /dev/sda
Start - 12:06:06 End - 12:12:36 Elapsed Time - 6:30
Cloned partitions are mounted on /mnt/clone for inspection or customizing.
Hit Enter when ready to unmount the /dev/sda partitions ...
unmounting /mnt/clone/boot
unmounting /mnt/clone
===============================
In this case I cloned from/to 16 Gb SD Card with approx 2 Gb of
data.
Before you hit "Enter", to unmount, the destination can be checked, in another terminal window, with
Before you hit "Enter", to unmount, the destination can be checked, in another terminal window, with
ls -la /mnt/clone
Automated clone
To add an automated setup, from a command line, type
crontab -e
If it's the first cronjob You are adding, something like this will appear
no crontab for pi - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano <---- easiest
3. /usr/bin/vim.tiny
Choose 1-3 [2]:
crontab: installing new crontab
Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano <---- easiest
3. /usr/bin/vim.tiny
Choose 1-3 [2]:
crontab: installing new crontab
where You select the editor You want to use.
On the last line add
0 1 * * * sudo bash rpi-clone sda -q
and exit. This means that a new clone will be made every night at 1 o'clock
Clone alternative
With GUI installed use the standard
"Accessories => CD Card Copier" found in the RPi meny. (If you
don't use RPi OS lite)
If not installed ? sudo apt install piclone
Remark
On one of my RPi 4 I suddenly couldn't boot from SD-card and got the message, on the monitor,
Failed to open device: ’sdcard’ (cmd 371a0010 status 1fff0001)
Googled a little and found that this could happen when the SD card slot broken. So how to fix it ?
I could do some soldering and exchange the broken slot but bought a fast USB 3 flash drive, cloned the SD card to the flash drive, inserted it in one of USB 3 ports and booted from that instead.
Wonderful, it´s just what I was looking for. Thanks a lot!
ReplyDeleteGreat! tyvm
ReplyDeleteThank you so much! Other tutorials were too complicated, this one is the easiest and the greatest! I'm working on a project and I don't want to lose my data after corruption so going to copy the whole OS onto another SD card.
ReplyDeleteThx
DeleteNice!
ReplyDeleteI am doing a copy of the SD card before I do changes and I am always using
SD Card Copier in Raspian. Never failed so far :-)
Thanks, but if You are using Raspbian Lite, without GUI, You can't use SD Card Copier, or ?
DeleteSorry did not think of that :-( My mistake!
DeleteNo problem.... just so I got it right ;-)
DeleteHi and thanks for your tuto!
ReplyDeleteI have a question, my RPi4 boot with part boot on sdcard and after, the rootfs part is on a SSD.I'm waiting for native boot on RPi4 ;)
So today may it possible to use RPI Clone to clone my rootfs to a sdcard.
My rootfs is about 15Gb on my SSD and my SDcard is a 64Gb.
Good question, but sorry to say I don't have any answer. Think the best way is to open an issue on GitHub.
DeleteOn the line "Optional destination ext type file system label (16 chars max): XX", did you enter "XX" so it knows it's a bootable card? I entered something different and my card won't boot.
ReplyDeleteIt's just a label so it will not affect the boot process. Corrupt SD card ?
DeletePi4 - I have successully made an SD of the new Raspbian that works both as SD and USB for USB boot and I've used rpi-clone to make a copy to a real USB which also boots.. I need to get back my working setup of a slightly older SD which will NOT USB boot. Can I somehow use pi-clone ONLY to overwite the second partition to make a new USB boot compatibe version of my workingg installation? Any ideas?
ReplyDeleteGood question. I'm not the author of the script and I see that You already created an issue on GitHub, which was my recommendation for "next step".
DeleteGreat post, thank you
ReplyDeleteGreat! I followed this post step by step and it works flawlessy.
ReplyDeleteThanx for this info!
When I make the cronjob, it will be saved with an not easy readable name.
It is saved in the "tmp" directory, which is not accessable.
Edit with crontab-e
Can I save it with an more readable name?
Thanks ! Don't bother about the filename it's just a temporary one before the changed info is stored in the "/usr/bin/crontab" file.
DeleteStrange things hapen ;-). Think with me plse.
ReplyDelete• Rpi3B+ regular running on a 16Gb stick => call it: “operation stick A”
• Back up SSD (yes indeed the same Kingspec) => call it: SSD KS.
• Back up method: Pyssel.
• First manual Back-up: January 26.
• Cronjob back up: every night at 3 o’clock.
• Test: January 29.
• Remove: Rpi “operation stick A “
• Start up with only SSD KS => OK, including actual data past 24 houre.
• Start up with only “operation stick A “ => OK, BUT NO actual data past 24 houre.
• This means the Rpi is not running on the “operation stick A” but is running on the Back up SSD KS???
Any idea how/what?
Thnx, Thomas
My fault ;-(
ReplyDeleteFound myself the solution due to my numeration in my post herefore ;-)
I have set the wrong sd* usb port.
Useful command to asure you select the right usb ports:
sudo lsblk -o UUID,NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,MODEL
To avoid any doubt. In the cronjob you specify the destination disk. You back up here To and not From. (correct me if i'am wrong.
ReplyDelete(thanks for your patience)
Correct. Using "lsblk -f" as said in the beginning of this post you will get the target/destination name. And in that respect there is no differens using the command at a command line or in a cronjob.
DeleteI have been trying for days to find a good way to make a backup image of my Octopi image on my raspberry pi. This is the BEST and clearest instructions on exactly what to do. I used Putty to log into my pi and enter the recommended commands. Thanks a million
ReplyDeleteThx!
DeleteAs a side note, while this guide was excellent in every respect, I had one bit of trouble... Yes, in hindsight, I should have been able to catch it, but... This wouldn't boot to my ssd attached until I edited /etc/fstab.
ReplyDeleteI used this to copy an image from microsdcard to M.2 Sata, as I was unable to simply burn to the M.2... Wouldn't complete boot, hung after the message random: crng initdone...
Using this most Excellent guide, I was able to make an exact copy of a bootable sd card to my preferred usb booting method using the M.2.
Just a note if someone has issues... it's easy to overlook.
DeleteThx, just wonder what change you did?
Hi Pyssel, for the /etc/fstab I also needed to change something.
ReplyDeleteFirst when booted with sdcard and the SSD connected I found out the PARTUUID with: ls -l /dev/disk/by-partuuid
Then I put that ID in place of what was already there in the fstab file.
Then shutdown pi and reboot. Now it boots from the SSD correctly :)
Hi and thanks for the feedback. Never had the problem myself but hope that your comment will help others with similar problem.
Deleteamazingly simple... Thanks!!!
ReplyDelete👍 yes, really nice software !
DeleteThis is the guide this topic deserves, thanks a lot!
ReplyDeleteBefore adding the line to the cron, I'm just wondering: if at a certain point I remove the destination USBadapted+SDcard (or for some reason it's not recognized by the OS), will sdb (in my case) point to a different physical disk? is there a risk to overwrite by xTB disks then?
👍The "lsblk" will show you what storage is assigned to which name. If the clone target is removed the name for the other storage will not change, but if a reboot is done you probably will have a problem.
Deleteok thanks for the answer; in order to avoid overwriting undesired destinations, is there any way to address the serial number instead of sdx-whatever that, as far as I understand, can change in certain conditions?
DeleteThe first that comes to my mind is using "lsblk -O" and then find out which attributes are unique for your clone target storage. Then write a script, or using NodeRed, which handles that the clone process will only start if the "right" clone target storage is online.
DeleteThanks worked perfectly when nothing else would clone existing RPi SD card, reporting errors and other unrelated BS.....thanks,.,,,,,g_d bless intelligence !
ReplyDeleteThx, 👍
DeleteI like this. Thank you. While I am running my Pi I occasionally need to clone a card from another Pi to a new card. Is there a way in the manual clone command line to designate source and target cards other than (from) the boot card?
ReplyDeleteI don't think so. As said in the beginning of the readme "for cloning a running Raspberry Pi booted source disk".
DeleteThanks
DeleteIch habe meinen pi3B+ als USB-SSD boot eingerichtet. Der Clone wird auf SD-Karte im Kartenleser als sda erkannt und rpi-clone sda -f2 gestartet. Soweit OK. Der Clone verbleibt im pi, nach einem Reboot startet der pi mal von der SSD mal von der USB-Clone SD-Karte. Wie kann ich den pi zwingen nur von der SSD zu starten, weil sonst habe ich Datensalat.
ReplyDeleteeine IDEE ???
Hi ! A little confused.... for what purpose do you want to have 2 active boot media at the same time ?
DeleteWeil ich die Clone SD-Karte im pi belassen möchte, um Synchronisierungen des laufenden Systems durchzuführen.
Delete(sofern das geht???)
OK..... it's possible to select boot order on a RPi >= 4. To my understanding it's not possible on your model.
DeleteWhat to do if my taget drive has a third partition that I want to keep, i.c.e. not delete/overwrite?
DeleteSince it's a clone of the source SD card, with a format of the target, it's not the purpose. But why not copy the third partition from the target to the source and then do the clone ?
DeleteAlso, this does not work for NVMe cards .... very sad ;-(
ReplyDeleteOK. I'm not using NVME cards so didn't know that, thanks. But why not create an issue at GitHub ?
Delete