Use SSH and dd to Remotely Backup a Raspberry Pi

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

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
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
/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.

Completion:

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!

References:
Advertisements
Comments
  1. […] my instructions here to backup your updated sd card  to your […]

    Like

  2. anne says:

    Hi, Thanks for the suggestion.

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

    Like

  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.

    Like

  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 pi@xxx.xxx.x.xxx “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)

    Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s