Use SSH and dd to Remotely Backup a Raspberry Pi

Posted: 2016-11-25 in Ham Radio, How To, Unix Geekery
Tags: , , ,

I love the Raspberry Pi! I am currently using one as a local DSTAR hotspot. It works great. I have tried several different images (Maryland-DSTAR, Western, and DSTAR Commander). I think I have settled on DSTAR Commander. I think I finally have everything working exactly how I want it to.

When making tweaks to the system, it is very important to make a backup of your current system so that you can easily go back to where you started if you messed something up. Additionally, if your SD Card fails (and it WILL), having a backup will enable you to quickly get your system back online.

I used to have to shut down the pi, pull the SD Card out, find my SD Card adapter, plug it into my Mac, make a backup, and then reverse the process to get my system online again.  Those days are over. You can make a backup of your pi and have the backup written to your regular PC all from the command line. Here is how to do it.

Bottom Line Up Front:

From your local machine, run the remote backup command:

ssh pi@xx.x.x.xx "sudo dd if=/dev/mmcblk0 bs=1M | gzip -" | dd of=~/Desktop/pibackup.gz

If you ever need to use your back up you cannot do it remotely (at least that I know of).  You will need to insert the SD card into your local machine and run the following commands:

diskutil unmountDisk /dev/disk#
gzip -dc ~/Desktop/pibackup.gz | sudo dd of=/dev/rdisk# bs=1m conv=noerror,sync

Continue reading if you need much more detailed instructions on what this command does, what each piece of the command means, and how to get all of the necessary inputs.

Note 1:

My local a machine is a MacBook Pro and therefore uses Darwin Unix.  My Raspberry Pi is running Debian unix.  Some of the commands used on the local machine will not work on the remote machine.  Use the info command on the system you want to run the command in order to get the proper syntax.

Note 2:

Not all SD cards are the same size, even if they both say 32GB.  I have seen variations.  To ensure this technique works all of the time, I recommend that you use “gparted” to create some free space at the end of your SD Cards to ensure they are EXACTLY the same size.

Find Your Device

SSH into your RPi using the following command:
ssh pi@xx.x.x.xx
xx.x.x.xx is the ip address of your RPi.

Use the following command to determine the block device of your SD Card:
lsblk -p

lsblk = the command to “list block devices”
-p = tells lsblk to print the full path of the block devices

The output will look something like this:

pi@raspberrypi:~ $ lsblk -p
/dev/mmcblk0 179:0 0 29.9G 0 disk
\u251c\u2500/dev/mmcblk0p1 179:1 0 60M 0 part /boot
\u2514\u2500/dev/mmcblk0p2 179:2 0 3.7G 0 part /

/dev/mmcblk0 is our device

Exit your current SSH session by typing exit

Backup Your Raspberry Pi

Use the following command:
ssh pi@xx.x.x.xx "sudo dd if=/dev/mmcblk0 bs=1M | gzip -" | dd of=~/Desktop/pibackup.gz

Breakdown of the command:
  • ssh = secure shell
  • pi = The name of the user you are logging in as on the REMOTE machine
  • @xx.x.x.xx = the ip address of the remote machine
  • sudo = super user do (elevate your privileges to the super user)
  • dd = Data Description; utility used to copy and convert data.  See my other post here for more information on this powerful unix command.
  • if=/dev/mmcblk0 = input file (the device that you are copying FROM)
  • bs=1M = tells dd to use a block size of 1 million bytes.  If the number ends with a “b”, “k”, “m”, “g”, or “w”, the number is multiplied by 512, 1024 (1K), 1048576 (1M), 1073741824 (1G) or the number of bytes in an integer, respectively.  Macs want a capital M; on debian the command wants a lowercase letter.
  • | = pipe command reads the output of the command on the left and pipes it to the command on the right.
  • gzip = program to compress files
  • - = read standard input (tells gzip to compress standard input which is coming from the “pipe”
  • | = pipe the standard output of gzip to the next command
  • dd = Data Description; utility used to copy and convert data.
  • of=~/Desktop/pibackup.gz = Write the output file of dd to a file called pibackup.gz on the local user’s Desktop folder.

When you enter the command it is going to ask you for the password on the REMOTE machine. In this case it is asking for the password of “pi.”  Enter the password and press enter.  You will be staring at a blinking cursor for a long time.

Getting a Status of dd

If your local machine is a Mac, you can easily get a status by pressing “CTRL + T”

You will get a status that looks like this:

load: 0.98 cmd: dd 8967 running 0.17u 1.94s
317344+0 records in
317344+0 records out
162480128 bytes transferred in 167.835383 secs (968092 bytes/sec)
load: 1.13 cmd: dd 8967 running 0.57u 6.17s

You can do this as many times as you want, to get an idea of how much has been copied.


When the process is complete, your command prompt will return.  Your remote backup of your raspberry pi is complete and the backup file is on the desktop of your Mac.  Sweet!

  1. […] my instructions here to backup your updated sd card  to your […]


  2. anne says:

    Hi, Thanks for the suggestion.

    looks now as if the backup file pops up at the desktop of my Pi……


  3. John A says:

    Just used this to fix a reboot loop problem. I changed the timezone from UTC to my local time zone. Apparently this causes a reboot loop and the only way to fix is to re-image the SD card. With my backup saved to my computer, it made getting my system back extremely quick and easy.


  4. SH says:

    My earlier comment had a few errors and omissions. Here’s a corrected version.

    Here’s how to do it using “Bash on Ubuntu on Windows”:

    ssh “sudo dd if=/dev/mmcblk0 bs=1M | gzip -” | dd of=/mnt/c/Users/’username’/Desktop/pibackup.gz

    Notice the 1m should be 1M, and replace ‘username’ with your actual Windows username (e.g., /mnt/c/Users/Bob/Desktop/pibackup.gz)


  5. dave says:

    Can you provide examples or a link to how to do the opposite and restore from an archive via ssh?


    • John A says:

      It’s not really possible since you are overwriting the machine that you have an SSH connection to. If there is a way, I am unaware of it.


    • Norm says:

      You would need to have physical access to the SD card you’re going to restore the .gz backup onto. Once you have the SD inserted and visible in /dev/sdX, then run something similar to:
      gunzip -c /path/to/your-backup.img.gz | dd of=/dev/sdX


  6. Scott says:

    Love the breakdown of the commands. Thanks!


  7. mauvedeity says:

    This process worked (well, is still working) for me as well. It’s really slow, because the gzip is taking 80-90% of the CPU on the Pi, and since mine’s a Pi 1, it’s going to take a while. I also hit the thing with the bs parameter needing a capital M.

    One potential problem is if the sudo command on the Pi needs a password – mine doesn’t but I don’t know why.

    I might actually set this up to run regularly now, to keep my Pi backed up.


  8. mauvedeity says:

    Oh, and the lsblk command didn’t like the -p parameter. Without that, it gives you output as you’d expect.


  9. mauvedeity says:

    …actually, having tried this again, I’d suggest taking out the “| gzip -” part, especially for older Pis. You’d also need to change the “.gz” to “.img”. These changes mean that the Pi doesn’t compress the dump on the way through, but sends it over the network uncompressed. Because the processors on Pis can be quite slow, the compression adds a lot of time to the process. On my Pi 1, with compression on it took about 2.5 hours(!), but without compression it was about 55 minutes, transferring data about 9 times faster. If you want it compressed, compress the file on your local machine. Hope this helps!


  10. Jesse Geron says:

    You’re the man! I’m going to work on scheduling this to run nightly!


  11. Angelo says:

    Doesn’t work for me. Guessing you have done something to allow for remote sudo execution or that it prompts you for a password? Mine just returns in less than 1 second without actually copying the device from the Pi to the Mac. I also have passwordless login setup for SSH and that works fine – verified with -v switch. If I add a -t to the ssh command, it just sits there on the Mac and there is a process started found via pgrep but it’s dormant. Guessing it’s sitting waiting for something like a password. Edited the sudoers via visudo and added
    %adm ALL=(ALL) NOPASSWD: /bin/dd if=/dev/mmcblk0
    to attempt to allow pi (default user) to execute dd against that device. Still no good.


    • John A says:

      It may have something to do with your no-password login. When I run the command it asks me for the Pi’s password. I enter it and the command runs.


      • Angelo says:

        I think I have it working although not sure why on a Mac it does this as I’ve been doing dd via ssh for a lot of years. Anyway, I used the same basic command WITHOUT the quotes and it worked! Go figure. Maybe it’s an OSX version thing? The passwordless is setup so that I can cron the backup and it works fine. Anyway, thanks for the detailed instructions!


      • John A says:

        Fantastic! Glad it’s working Angelo.


    • John A says:

      When you use the “-t” option and it doesn’t do anything, that is the process running properly. Pressing CTRL-T will give you a status as I explained in the post.

      Liked by 1 person

  12. mrbeezer says:

    Worked great except ran out of room on sd card. can this be used to write to a usb stick?


  13. Joost says:

    I am trying this command but cannot get it to work:

    ssh pi@ “sudo dd if=/dev/mmcblk0 bs=1M | gzip -” | dd of=~/Desktop/pibackup.gz
    pi@‘s password:
    sudo: no tty present and no askpass program specified
    0+1 records in
    0+1 records out
    20 bytes transferred in 4.103791 secs (5 bytes/sec)

    Every time the “sudo: no tty present and no askpass program specified” 😦
    On the Pi I edited the sudoers file (/etc/sudoers) via “sudo visudo” with the following:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s