How to backup an image of the system


16 posts
by Bernez » Sat Jul 27, 2013 12:53 pm
Hi,

I'm new on this board and quite new to Raspbian.
But I have a question regarding making an image copy/backup of the system.
In 2 weeks, I got the SD card corrupted 3 times. This mean new install of the system (NOOB, Raspbian), installing some lib's again, installing a program I use for mij weather station, etc, etc...
Very frustrating and time consuming.

My question is:
Is it possible to make a backup or copy of the entire working system (image) ?
And, if yes, is it possible to make it bootable from another device (SD or USB Hard disk).


Thx,
Bernez
Patience: In time, even an egg will walk...
Posts: 8
Joined: Sat Jul 27, 2013 12:36 pm
by DougieLawson » Sat Jul 27, 2013 7:14 pm
I've got one of these SD Card readers
http://www.amazon.co.uk/Fujifilm-Card-R ... B001725Y0Q

That means I can have my RPi booted from one SD card and read/write a second SD card.
It also means I can pull my SD Card from my RPi and mount it on a Windows system or my Linux server.

To create a backup I use the Unix `dd` command (as root).

dd if=/dev/mmcblk0 of=/dev/sda bs=1000000

Beware: that will destroy ALL data on the `of` device.
Hacker on ZX80 and Microtan65
Unemployed mainframe database specialist.
Linux hacker since 1995.
RPi owner since 2012.
Twitter: @DougieLawson

Gaffer tape is "The Force", it has a dark side and a light side and it holds the Universe together.
User avatar
Posts: 3624
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by Bernez » Sat Jul 27, 2013 9:09 pm
Hi Dougie,

Thanks for your reply.
First, can you do the same thing with an USB Hard Disk ?
I have one spare on the shelf. It would be great if I could use it to backup the SD card and run my weather program instead of running it on the SD card.
I could make 2 different partitions. One for the backup and one for the program (which data of cause must remain intact).
If yes, what are the commands for this job. And, how do I format and mount the USB Hard Disk

Second, how do I restore the backup to a SD card.

As I said I'm quite new to Linux. So, forgive me if all of this sounds stupid to you.

Thanks,
Bernez
Patience: In time, even an egg will walk...
Posts: 8
Joined: Sat Jul 27, 2013 12:36 pm
by DougieLawson » Sat Jul 27, 2013 9:36 pm
What's the format of the filesystem on the USB harddisk?
If you plug it into your RPi does it get automounted? Check with the `mount` command.

If not take a look at the output from `dmesg`
Code: Select all
[888264.614464] sd 0:0:0:0: [sda] 7761920 512-byte logical blocks: (3.97 GB/3.70 GiB)
[888264.615955] sd 0:0:0:0: [sda] Write Protect is off
[888264.615989] sd 0:0:0:0: [sda] Mode Sense: 0b 00 00 08
[888264.617457] sd 0:0:0:0: [sda] No Caching mode page present
[888264.617488] sd 0:0:0:0: [sda] Assuming drive cache: write through
[888264.626725] sd 0:0:0:0: [sda] No Caching mode page present
[888264.626761] sd 0:0:0:0: [sda] Assuming drive cache: write through
[888264.631512]  sda: sda1 sda2 < sda5 sda6 >
[888264.637629] sd 0:0:0:0: [sda] No Caching mode page present
[888264.637668] sd 0:0:0:0: [sda] Assuming drive cache: write through
[888264.637689] sd 0:0:0:0: [sda] Attached SCSI removable disk


You'll see that when I plugged in my removable device (/dev/sda) it has one primary partition (sda1), one extended partition (sda2) and two logical partitions (sda5, sda6). I can mount those with the root account `mkdir /media/sda1 && mount /dev/sda1 /media/sda1`

Unix dd can read from a device and write to a file. So this time we use
`dd if=/dev//dev/mmcblk0 of=/media/sda1/rpi.27Jul2013.img bs=1000000`

Again beware that you can destroy things (without prejudice) if you get your of= spec wrong.

That *.img file can be rewritten to an SD card using unix dd to put it back (reverse if/of spec) or by using Win32DiskImager.
Hacker on ZX80 and Microtan65
Unemployed mainframe database specialist.
Linux hacker since 1995.
RPi owner since 2012.
Twitter: @DougieLawson

Gaffer tape is "The Force", it has a dark side and a light side and it holds the Universe together.
User avatar
Posts: 3624
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by Bernez » Sat Jul 27, 2013 10:17 pm
The USB HD is format FAT32 (windows) for now.

And, if I understand correctly, it's possible.
As I said, I want to make 2 partitions. One for the Linux system backup, which I eventually could restore to the SD card and one for running my weather program, which stay intact.

Bernez
Patience: In time, even an egg will walk...
Posts: 8
Joined: Sat Jul 27, 2013 12:36 pm
by DougieLawson » Sat Jul 27, 2013 10:34 pm
I'd mount the FAT/VFAT/FAT32 filesystem and write your image to a file on there. That gives you a versatile backup because you can restore it using any machine that can read that FAT partition on your USB HD and write to an SD Card.

You could carve out a partition and format it as ext4 (which is one of the Linux journalling filesystems), but that's for the future when you've learned more about the basics of Unix systems admin.
Hacker on ZX80 and Microtan65
Unemployed mainframe database specialist.
Linux hacker since 1995.
RPi owner since 2012.
Twitter: @DougieLawson

Gaffer tape is "The Force", it has a dark side and a light side and it holds the Universe together.
User avatar
Posts: 3624
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by plugwash » Sat Jul 27, 2013 10:51 pm
DougieLawson wrote:I'd mount the FAT/VFAT/FAT32 filesystem and write your image to a file on there. That gives you a versatile backup because you can restore it using any machine that can read that FAT partition on your USB HD and write to an SD Card.

The problem with that is it's easy to run into the maximum filesize limit for fat32.

I usually end up using NTFS for drives that need to move between windows and linux for this reason.
Moderator
Moderator
Posts: 1965
Joined: Wed Dec 28, 2011 11:45 pm
by Bernez » Sat Jul 27, 2013 10:55 pm
OK, thanks.
A lot of stuff still to learn. :roll:
I will try all of this tomorrow. It's bed time now. ;)
Patience: In time, even an egg will walk...
Posts: 8
Joined: Sat Jul 27, 2013 12:36 pm
by Bernez » Sun Jul 28, 2013 9:07 am
Can I format the HD and make the 2 partitions on a Windows PC ?
That would be the easiest way for me, as a Windows user.
What would be a practical size for the NTFS backup partition (4 GB, 8GB ?). And, should I make it primary, logical or extended.

Bernez
Patience: In time, even an egg will walk...
Posts: 8
Joined: Sat Jul 27, 2013 12:36 pm
by framp » Mon Jul 29, 2013 8:21 pm
I wrote a bash script which causes all my Pis @home to create backups on any mounted disk space - either a dd backup or tar backup of my xbmc and raspbian. Maybe it's also useful for the community. Just add the script invocation in your crontab.
"Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect." Linus Torvalds, 28.9.2003
User avatar
Posts: 10
Joined: Sun Jan 27, 2013 7:03 pm
Location: Stuttgart, Germany
by Bernez » Sat Aug 03, 2013 9:05 am
@ framp

You got a PM.

Bernez
Patience: In time, even an egg will walk...
Posts: 8
Joined: Sat Jul 27, 2013 12:36 pm
by framp » Sat Aug 03, 2013 1:33 pm
The backup script will be called by cron according the definitions in crontab. I use for example
Code: Select all
00 22  * * 0 /usr/local/bin/raspiBackup.sh -p /backup -t tar -k 4 
.
which starts the backup every Sunday at 10 pm. If you go to the cron wiki page you will find in the section called 'format' the description of the 5 first entries in the line, which defines in detail when the cron job should run and much more details about cron.
Extract form the page:
Code: Select all
# *    *    *    *    *  command to execute
# ┬    ┬    ┬    ┬    ┬
# │    │    │    │    │
# │    │    │    │    │
# │    │    │    │    └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names)
# │    │    │    └────────── month (1 - 12)
# │    │    └─────────────── day of month (1 - 31)
# │    └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
"Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect." Linus Torvalds, 28.9.2003
User avatar
Posts: 10
Joined: Sun Jan 27, 2013 7:03 pm
Location: Stuttgart, Germany
by Bernez » Sat Aug 03, 2013 8:09 pm
Thanks Mark,

That's clear now.

Bernez
Patience: In time, even an egg will walk...
Posts: 8
Joined: Sat Jul 27, 2013 12:36 pm
by framp » Sat Aug 03, 2013 9:21 pm
Bernez wrote:Thanks Mark,

my nick is framp :roll:
"Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect." Linus Torvalds, 28.9.2003
User avatar
Posts: 10
Joined: Sun Jan 27, 2013 7:03 pm
Location: Stuttgart, Germany
by Bernez » Sun Aug 04, 2013 8:21 pm
framp wrote:
Bernez wrote:Thanks Mark,

my nick is framp :roll:


Sorry for that :oops:

I just tried your script, but it did nothing. I think I missed a point somewhere (don't forget, I'm a Noobie).
I just want to regularly backup one directory. i.e. /home/pi/Desktop/consolewdfiles
How do I set this in raspiBackup.sh and how do I set crontab. (time and day is not relevant for now).
I also don't know which type is best for the job (dd or tar).

Thanks,
Bernez
Patience: In time, even an egg will walk...
Posts: 8
Joined: Sat Jul 27, 2013 12:36 pm
by framp » Sun Aug 04, 2013 8:43 pm
Bernez wrote:...I just want to regularly backup one directory. i.e. /home/pi/Desktop/consolewdfiles...

The title of the thread is 'how to backup an image of the system' which means how to backup everything of the Pi (for XBMC its just the XBMC config which needs to be saved).

That said the script will not solve your problem to copy just one single directory.

I suggest to open a new thread and explain your problem. Actually there exist various ways to create a backup but to find the right way you have to give more details about the purpose of the backup. Feel free to xlink the new thread here.
"Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect." Linus Torvalds, 28.9.2003
User avatar
Posts: 10
Joined: Sun Jan 27, 2013 7:03 pm
Location: Stuttgart, Germany