I've noticed quite a few people have been having issues copying the contents of their SD card to another card, if the size of the destination card is smaller, or maybe don't have another computer to hand that can do this.
I've been thinking about this for a while and think I've come up with a way of safely backing up the SD card to another one (even if the size is slightly different), using just the Pi, a USB card reader, keyboard and screen.
I'm using the "recovery kernel" as supplied on Raspbian and at least some of the other distros (although I've only been working with Raspbian so far). The recovery kernel is loaded on boot if pins 5 and 6 on P1 ( the third pair from the edge of the board) are connected together when power is applied (unless disabled in config.txt).
The advantage of the recovery kernel is that it all runs from a RAMdisk, so the SD card is not in use, especially after I unmount /boot.
I have a script that will read the size of the partitions on the card, check the size of the destination card and see if the contents will fit. It will then partition the new card, copy the FAT partition using dd, copy the contents of the ext4 partition using tar, create a swap partition only if one existed on the original card and that's it. Complete ready to use backup card.
The script would be placed in the /boot partition and copies itself to /tmp before unmounting /boot.
The only real issue I've had so far is that there is no program in the emergency kernel to create an ext4 filesystem. I've managed to get around that for now by mounting the current ext4 partition in read-only mode, linking in the required library files and using mke2fs from there. It seems to work.
Once I've tidied it up a bit, is anyone interested in giving it a try?
Can anyone see any pitfalls I might hit using this method?