Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I have been writing programs for my RasPi (running Debian) for a few weeks now, and would like to make sure I protect the work I have done.

How can I backup the files I have created?

Can I simply plug the SD Card into my Windows (XP or 7) PC and copy it either to the hard drive or another SD Card?

share|improve this question
    
Is your pc running linux? –  Jivings Jun 14 '12 at 18:35
3  
I have PC's running both Ubuntu and Windows (XP & 7), but I would expect most people to be interested in the Windows answer. I will edit the question to make that clear. –  Steve Robillard Jun 14 '12 at 18:38
4  
rsync from Raspi to your pc? –  Alex L Jun 14 '12 at 18:54
20  
For code, you may also want to consider using distributed version control software like git or hg, that way you not only have copies of your code in multiple places, but also have the complete history of how that code has changed. –  Mark Booth Jun 15 '12 at 11:01
    
I've used ext2 filesystems on Windows with no trouble at all (ran Visual Studio off it) using Ext2IFS. I don't know about the state of ext3/ext4 support (haven't tried ext2fsd). –  reinierpost Feb 28 '13 at 15:37

10 Answers 10

up vote 127 down vote accepted

If you want to preserve all of the data, you will probably have to create a disk image. Furthermore, Windows cannot recognize typical Linux filesystems, so you probably won't even be able to see your files, when you plug in your SD card.

Creating a disk image will preserve not only files but also the filesystem structure and when you decide to flash your new SD card, you will be able to just plug it in and it will work.

Linux

On Linux, you can use the standard dd tool:

dd if=/dev/sdx of=/path/to/image bs=1M

Where /dev/sdx is your SD card.

Mac

On Mac, you can also use the standard dd tool with a slightly different syntax:

dd if=/dev/rdiskx of=/path/to/image bs=1m

Where /dev/rdiskx is your SD card.

(using rdisk is preferable as its the raw device - quicker)

To find out which disk your device is type diskutil list at a command prompt - also, you may need to be root; to do this type sudo -s and enter your password when prompted.

Windows

Option 1

On Windows, you can use the reverse process that you used when flashing the SD card.

You can use Win32 Disk Imager, which is the preferred tool for flashing a SD card of the Foundation. Just enter the filename, select the device and press read:

Win32 Disk Imager

Of course, you can also use RawWrite, dd for Windows or similar tools, the process is quite similar.

Option 2

If you don't want to back up your entire system, but only specific files, I suggest you connect to your Raspberry Pi via SFTP and copy the files to your local computer (You can use the WinScp client). If you have SSH enabled, SFTP usually requires no special configuration on the Raspberry Pi side.

You can also install special drivers so your Windows can read ext filesystems (and will thus be able to read the whole SD card), such as ext2fsd but it s probably not orth the effort.


Since the image will be of the same size as your SD card, you may want to compress it. THis can be achieved simply by using your favourite compression tool, such as gzip, 7zip, WinZip, WinRar ...

share|improve this answer
    
+1 in general, although rsync via ssh would probably be easier. Also, for source code - as Mark Booth already stated - you should absolutely use a DVCS –  Tobias Kienzler Dec 13 '12 at 16:54
    
If my SD card became corrupted, how would I install this image to a new SD card? –  Flipper Feb 3 '13 at 8:57
1  
Your link to Win32 Disk Imager comes up with no downloads available. I assume it's the same as sourceforge.net/projects/win32diskimager ? –  Yamikuronue Feb 14 '13 at 1:03
    
@Yamikuronue Yes. The page in the answer has a link to the sourceforge page you specified. –  Cristi Diaconescu May 6 '13 at 16:34
1  
When I use WinDiskImager, I get an image that is the size of the storage partition - the boot partition appears to be missing. Have you tried writing the image onto a new SD card? I don't have a second one with which to test at the moment. –  me-- Jan 3 at 3:46

If you are running linux then you can use the dd command to make a full backup of the image:

dd if=/dev/sdx of=/path/to/image

or for compression:

dd if=/dev/sdx | gzip > /path/to/image.gz

Where sdx is your SD card.

To restore the backup you reverse the commands:

dd if=/path/to/image of=/dev/sdx

or when compressed:

gzip -dc /path/to/image.gz | dd of=/dev/sdx 
share|improve this answer
4  
This is brilliantly handy. –  phalt Jun 15 '12 at 9:14
2  
I highly recommend using gzip - I have backed up some desktop partitions today and a 20Gb partition was saved in 8.9Gb. –  Alex Chamberlain Jul 4 '12 at 15:48
5  
@AlexChamberlain: gzip is pretty awesome. –  Jivings Jul 4 '12 at 16:00
1  
bzip2 should compress even better, and is available on all Linux systems. On newer Linux systems xz should compress even better.The decompressors for these are bunzip2 and unxz respectively. –  Arne May 29 '13 at 12:10
1  
@Arne I tried using bzip2 now, and maybe it would be appropriate if I was backing up from my Class 4 SD card, but on this Class 10 UHS 1 SanDisk 8GB card (which was $12 last week) it is maxing out an Ivy Bridge CPU thread and limiting read speed from the card to 6MB/s (dd with Ctrl+T reports 4.2MB/s). This is not ideal because I can use gzip with this card and read off of it at a much quicker pace (dd reporting 18MB/s). Granted, if most of the disk is free space, then reading during those empty stretches will speed up. But I'll have to see what the difference is in the resulting file size. –  Steven Lu Apr 20 at 3:40

If you are using Windows:

  1. Download Win32 Disk Imager.

  2. Create the file path you wish to use to save your image. (I use C:\RasPi_Bkp)

  3. Run Win32 Disk Imager

  4. Browse to your backup file path spot, and type a file name.

  5. Click save

  6. Make sure the device shown in the drop down to the right of your file path is the one you want to back up.

Win32 Disk imager in Action

  1. Click "Read"

  2. Wait.

share|improve this answer
    
Thanks - just the job. (Oddly, when I plug my SD card into my main PC, Windows 7 goes to BSOD! Fortunately its fine on another machine.) –  Jon Egerton Jul 5 '12 at 10:01

Besides that block-level backups there are two common approaches to deal with the sources: to archive it continuously (1), or to use the revision control system (2). We are going to use command-line (any local terminal or ssh connection to a raspberry machine), right?

1. Archive

cd ~/projects
tar czvf your-raspberry-project-top-level-dir-v1.0.tgz \
         ./your-raspberry-project-top-level-dir 
scp your-raspberry-project-top-level-dir-v1.0.tgz \
         user@backup-host:/home/user/backups/

2. RCS (Git for instance)

cd ~/projects/your-raspberry-project-top-level-dir
make clean                          # Or clean it manually using rm (rm ./*.o ./*.pyc)
git init                            # Create new repo here
git add .                           # Add source files to the staging index
git status                          # Verify if it's ok
git commit -a -m "Initial import"   # Fix application's source changes
git add remote https://github.com/user/your-raspberry-project.git
git push -u origin master           # Sends the sources to your github repo
git pull && git push && git status  # Now origin/master is your tracking branch
share|improve this answer
    
It might also be worth noting that people can set up bare repos on other machines on their network and push from their Raspberry Pi to there, rather than having to set up a github account, ssh keys etc and push to there. –  Mark Booth Apr 13 at 12:06

You can run this command from your linux PC with lots of space

ssh root@raspberrypi gzip -c /dev/mmcblk0 > img.gz

As a prerequisite you'll need to have generated .ssh keys and copied the id_XXX.pub over to /root/.ssh/authorized_keys

It's possible to have issues with the file system since it's a live backup, but if you RPi isn't real busy it will usually be ok.

It's probably faster to do the compression on the PC like this

ssh root@raspberrypi dd if=/dev/mmcblk0 | gzip -c > img.gz
share|improve this answer
1  
or with sudo 'ssh pi@raspberry sudo dd if=/dev/mmcblk0 | gzip -c > raspberry.img.gz' –  cupakob Nov 25 '12 at 14:35

On the Mac you don't want to be using /dev/diskn, you should use /dev/rdiskn instead, where n is the number the OS uses to identify your SD card. This decreases the time required to copy by a huge amount.

So for the optimal backup process on a mac, I would recommend doing the following:

Run diskutil list, and find the disk corresponding to your Pi's SD card:

$ diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            499.2 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *7.9 GB     disk1
   1:             Windows_FAT_32                         58.7 MB    disk1s1
   2:                      Linux                         7.9 GB     disk1s2

Clearly /dev/disk1 is my 8GB SD card, the Linux partition name is also a bit of a clue.

However, instead of using /dev/disk1 with dd, you should use /dev/rdisk1, like so:

sudo dd if=/dev/rdisk1 of=/path/to/backup.img bs=1m

And to restore it, just swap the if (input file), and of (output file) parameters:

sudo dd if=/path/to/backup.img of=/dev/rdisk1 bs=1m

Or, with gzip, to save a substantial amount of space:

sudo dd if=/dev/rdisk1 bs=1m | gzip > /path/to/backup.gz

And, to copy the image back onto the SD:

gzip -dc /path/to/backup.gz | sudo dd of=/dev/rdisk1 bs=1m

For more info see this wiki page.

share|improve this answer

If your programs are all in the pi userid, there's really no reason to back up the entire filesystem, as is being suggested. what I do is to just back up the single id. I run the command:

tar -czf pi.tgz *

from pi's home directory, which creates the file pi.tgz, containing all the files and directories contained there (excluding hidden files). I then scp this file to another linux computer, but you could ftp it or e-mail it somewhere for safe-keeping... anything that got it onto another computer.

This creates a much smaller file for your back-up.

Concerned about all the additional packages you've installed along the way? Create a script that will perform those installs for you again should you need to recreate your SD card, and keep it in pi's home directory somewhere. (I have a ~/bin directory for such things). Have all the commands you need to do the installs to bring you back to the position you want to be in. It documents your changes, and allows you to quickly build up a new SD card. It would contain commands in the form of:

apt-get --assume-yes install apache2 mysqld mysql php5 php-pear

Start it out with

apt-get update apt-get --assume-yes upgrade

so that your system also is brought up to the current level before you start adding your packages.

Since this will be in your pi.tgz file, you'll have it when you need it.

share|improve this answer
    
Note that for the Raspbmc spin-off, the apt-get upgrade is apparently known to cause issues. It did for me, and apparently it's a known issue . –  Cristi Diaconescu May 6 '13 at 16:11

I run raspbian and use dd and cron to do automatted backups of my SD to my external usb drive.

Really simple solution, it runs once a week at 2am on a Monday morning and makes an image of the SD card.

I wrote a script which shuts down services such as lighttpd and cron, to minimise the risk of the sd card being written to in the middle of the backup, it then calls dd, before starting to the services again when the backup has finished.

Ive put the script and some instructions on my blog, http://www.stuffaboutcode.com/2012/08/raspberry-pi-auto-backups.html

share|improve this answer
1  
This might be a reasonable solution if you can't unplug the card. However, dding an sd? device with mounted partitions doesn't sound quite safe. –  tohecz Mar 24 at 9:58
    
Your probably right, but I haven't had a problem. –  Martin O'Hanlon Mar 24 at 13:57

For debian you could use dd and tar. This would make a complete copy of your SD card, and would require external (to SD card) storage, probably mounted USB or network drive.

dd if=/dev/sdd of=yourbackupfilenamehere.dd
tar zcvf yourbackupfilenamehere.dd.tar.gz

with /dev/sdd being the location of your SD card, and of being output file name.

share|improve this answer
    
This has the limitation of needing somewhere to write the image to though (that is also large enough to take the image(s). Presumably it also means a large data io through the usb hub - would take a while? –  Jon Egerton Jul 5 '12 at 10:38
    
True, but it's kind of required in the question: "I want a full image of the SD card." –  Forkrul Assail Jul 5 '12 at 10:39
1  
Why do you make a .tar.gz file of the dd-file? Wouldn't it be better to just run gzip or bzip2 on the dd-file directly? –  Anders Jul 25 '12 at 21:11

I've been using usbit for Windows. It's the only tool I can find that will allow you to swap from a larger SD card to a smaller one. All I did was tick the following on the options page; Ignore size checks & Truncate oversize images.

This allowed me to swap my openelec & Xbian images from a 16gb class 4 SD card to an 8gb class 10 card.

It's MUCH easier than resizing partition tables etc.

share|improve this answer
2  
Have you ever verified the files? I mean with find -type f -exec md5sum {} \; > filelist.txt? –  Avio Oct 5 '12 at 22:16
2  
This is not guaranteed to work, as data stored on the second half of the disk is dropped. Furthermore, the partition table is corrupted. –  Alex Chamberlain Oct 6 '12 at 9:22

protected by Jivings Feb 19 '13 at 16:27

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.