Magimedia
Posts: 33
Joined: Sun Feb 22, 2015 8:31 pm

Contract file system?

Mon Feb 23, 2015 8:03 pm

I'm wondering how I can best backup my Pi. My current method is as follows:

Use Win32DiskImager to read the SD card - creates an 8Gb img file
Use 7zip to create a compressed self-extracting image - creates a far smaller .exe
Delete the .img file

This takes a lot of processing, and quite some time to read the SD card. It also means I have to restore the image onto another 8Gb or larger card.

What if I want to restore onto a smaller card - say 4Gb?
I'd also rather not have to backup, and compress, all the "unused space" on the card.

Is there a way I can contract the filesystem to the smallest it can be, then back it up?

Any help much appreciated!

RobHenry
Posts: 452
Joined: Fri Sep 21, 2012 9:04 pm
Location: UK

Re: Contract file system?

Mon Feb 23, 2015 9:22 pm

There's no easy way to do this on Windows. On Linux there are many options (get a USB sd card adaptor and spare sd card and you can do it all from the pi).

gparted can be used to shrink a partition and file system. You should only do this if you want to restore the image to a smaller sd card.

partclone will make a clone of a partition copying only the acual data (so removes the need to use gparted unless you want to restore to a smaller sd card) and also allows an entire image to be restored.

Another option is to perform incremental backups using rsync (or even the very basic cp) . Smart use of option flags can ensure that only changed data is copied so once you've done the first back up it takes very little time to bring it up to date. Advantage of this approach is that you can inspect or restore individual files easily (it is possible using an image back up, but less simple).

Hope this helps.

Magimedia
Posts: 33
Joined: Sun Feb 22, 2015 8:31 pm

Re: Contract file system?

Tue Feb 24, 2015 10:23 am

I don't really mind whether I do it from Linux, Windows or the Pi itself. I'm only using Windows because that's what I used to make the SD in the first place.

I want to have an image backup, rather than incremental backup, because the Pi is in a situation where the power is going to be pulled without warning, so I think the SD card will get corrupted. The idea is then to just blast a new image onto the card.

When I change anything on the Pi, I will restore from the clean image, do what I need, and make a new clean image.
I suppose I could use a 4Gb card as my "clean" card, then duplicate it to the 8Gb "working" card, since I'm unlikely to ever use a card smaller than 4Gb.

Partclone sounds like the ideal solution for backing up. Presumably I run this on the Pi? Could it be told to clone to a file on an external USB2 drive (at the moment formatted to NTFS) ?

ghans
Posts: 7882
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Contract file system?

Tue Feb 24, 2015 11:55 am

Under Linux , would use kpartx to create device mappings for the partitons in the image , use resize2fs to shrink the last one and then delete the mappings through kpartx.
Now the last partition in the image should be much smaller. After that , you can truncate the file with "truncate" or "dd" to a smaller size.

Alternatively use zerofree on mounted partitions inside the image to improve compression
of the image file itself (though simply using a slower algorithm than gzip works too).

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

Return to “Raspberry Pi OS”