Backing up Raspberry Pi SD Cards on Mac OS X, Linux & Windows

If you want to back up the Raspberry Pi SD card, you cannot simply plug the SD Card into your Mac or PC and copy all the files to your hard drive. Instead, you need to create what it is called disk image. This is because,  when you create a disk image of your SD card, you will preserve not only all the files but also the entire filesystem structure.
Therefore, once you decide to flash a new SD card, you just have to plug it in and it will work.

Mac

Firstly, insert the SD card into a USB card reader of  your Mac and open the Terminal and type the following command in order to list all the disks attached to your Mac:

diskutil list

From the list, identify which /dev/disk corresponds to the SD card.

Now just use the dd command to backup of your SD card:

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

where /dev/rdiskx is your SD card and /path/to/image is the path where you want to save the backup. The backup image will be a file .img.

However, it is also possible to create a compressed SD card image (.gz) as follows:

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

To restore an SD card from a backup image, use the following command:

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

Linux

On Linux, similar to Mac, you can use the standard dd tool:

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

Where /dev/sdx is your SD card.

To create a compressed SD card image use the following command:

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

To restore an SD card from a backup image (.img):

sudo dd if=/path/to/image.img of=/dev/rdisk1 bs=1M

Windows

On Windows, you can use Win32 Disk Imager. The following screen-shoot is the main window.

Firstly you need to select which drive is your SD card. In this case my SD card is F:\.

Then, click on the blue icon just next to the F:\ to open a file-explorer window. Here you have to select where you want to save the image file and the type of image (.img, .bin, etc..).

Win32 Disk Imager

You have to confirm the folder and filename are correct. When ready just click the Read button. When completed it will show Done!

To restore the SD card from a backup image, just follow the same process but this time click the Write button.

Advertisements

How to set up Wi-Fi USB dongle on Raspberry Pi using the terminal + Android Tethering

It is relatively simple and cheap to add Wi-Fi connectivity to your Raspberry Pi. You only need to buy a Wi-Fi USB dongle and make sure that is compatible with GNU/Linux. On http://elinux.org/RPi_USB_Wi-Fi_Adapters you can find a list of compatible Wi-Fi adapters.

In this tutorial, I am using Edimax EW-7811UN available for less than 10 £ on Amazon.

Edimax EW-7811UN 150Mbps Wireless Nano USB Adapter

Before to start with the actual setup, you need to take note of the following details of your Wi-Fi connection:

  • Network SSID
  • Wireless password
  • Security (e.g.: WPA2-PSK)

In order to make this project a little more interesting, I am going to use my Nexus 4 as a Wi-Fi Hotspot. Therefore, I am going to share my device’s 3G data connection as a portable Wi-Fi hotspot.

(If you are not using an Android device as Hotspot just skip the following section)

On your Android device (This steps apply to Nexus 4, Galaxy Nexus, Nexus 7 (2013), and Google Play edition devices):

  1. Go to Settings > Under the section Wireless & networks > More > Tethering & portable hotspot.
  2. Go to Set up Wi-Fi hotspot and type your Network SSID, Security (e.g. I selected WPA2-PSK), Password > Save
  3. Tick Portable Wi-Fi hotspot

After a moment, the device starts broadcasting its Wi-Fi network name (SSID). Once you want to stop sharing your data connection, just untick Portable Wi-Fi hotspot.

Now, it’s time to go back to our Raspberry setup.

It is always good practice to update the system to the latest available software. Open your Terminal and type:

sudo apt-get update
sudo apt-get install

Now, type the following command to open the interfaces configuration file:

sudo nano /etc/network/interfaces

You should see the following lines:

auto loiface lo inet loopback
iface eth0 inet dhcp

This is the default setup of your Ethernet connection (eth0). We need to add the following lines for the new Wireless connection that we want to set up (wlan0):

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Press Ctrl-x to save and exit.

In the terminal enter the following command:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Replace all the content of the file with the following:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={

ssid=”YOUR_NETWORK_SSID”

# scan_ssid type can be: value of 1 means broadcast and value of 2 means hidden
scan_ssid=1

psk=”YOUR_WIRELESS_PASSWORD”

# Protocol type can be: RSN (for WP2) and WPA (for WPA1)
proto=RSN

# Key management type can be: WPA-PSK or WPA-EAP (Pre-Shared or Enterprise)
key_mgmt=WPA-PSK

# Pairwise can be CCMP or TKIP (for WPA2 or WPA1)
pairwise=CCMP

# Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP)
auth_alg=OPEN

}

The lines with # are only comments that should guide you. When you’re done with the editing, press CTRL+x to save and exit.

Now you only need to reboot in order to load the new network set up.

Raspberry will connect automatically during the boot to the Wi-Fi network.

How To: Rasperry PI Camera Module web streaming and motion detector

In this post I am going to describe how to setup a web streaming server and motion detector using a Raspberry PI with the PI Camera Module.

Before to start, I am assuming that you have already your Raspberry PI Camera Module setup and working.

Step 1: Download the software

The software that we are going to install is called Motion; this is both a motion detector and streaming software. You could potentially install Motion using the apt-get command from the terminal. However, if you want to use Motion with the Raspberry PI Camera Module you need a special version of Motion (called Motion – MMAL Camera) – see this post for more info and credits.

Therefore, what you are going to do is the following:

  1. Install motion using apt-get command so it will install all the required dependencies;
  2. Remove Motion that we have just installed leaving all the dependencies
  3. Download Motion – MMAL Camera

Ok then, let’s start..

sudo apt-get install motion

Press Y when it is requested to install all the required dependencies.  Now, we can remove Motion because, as I wrote above, we are going to use Motion – MMAL Camera.

sudo apt-get remove motion

At this point, we can download the pre-compiled version of Motion – MMAL Camera:

wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz

Unzip:

tar zxvf motion-mmal.tar.gz

Step 2: Config the software

As you can see, the archive contains two files: the executable and a config file.  Before running the software you need to make some changes to the config file. Make sure you are in the same directory where the config file is and open it:

sudo nano motion-mmalcam.conf

The config contains a lot of settings that you are free to modify as you like. However the most important ones are the following:

  • Image width and height: I suggest to set 640×480;
  • Framerate should be between 2 and 5 in order to not overload the Raspberry PI;
  • output_pictures: set to on if you want to generate a picture when a motion is detected;
  • ffmpeg_output_movies: set to on if you want to generate a movie when a motion is detected;
  • target_dir: set the folder where do you want to save the videos or the pictures. Example: /home/pi;
  • Under the section Live Stream Server set stream_port 8081 if you want to enable the web streaming video; moreover, set stream_localhost to off if you want to be able to access the streaming from any host other than the localhost. If you do not want the web streaming set stream_port to 0;
  •  Under the section HTTP Based Control set webcontrol_port 8082 if you want to access the Motion config from the web browsers. This is a very useful capability that allows you to change all the setting of Motion directly from the browsers. Set stream_localhost to off if you want to access the configs  from any host;

As you can see, Motion offers a lot of setting that you can play with…

Step 3: Run the software

Once you are happy with your config you can run motion using the following command:

sudo ./motion -n -c motion-mmalcam.conf

Now, if you want see the streaming open a browser (use Firefox or Opera but do not use Chrome because it does not work for me) and go to the following address:

http://IP_ADDRESS_OF_YOUR_RASPBERRYPI:8081

and you should be able to see the streaming! If you want to see the config tool go to:

http://IP_ADDRESS_OF_YOUR_RASPBERRYPI:8082

You can access to the web streaming from different devices such as iPhone, Android, etc..

That’s it. When a motion is detected Motion will generate pictures and/or videos according to what you chose in the config file.

How to run a command automatically at system boot in Raspberry Pi (Linux)

In the previous post I explained step by step how to set up a VPN connection in Raspberry Pi. When you want to connect to the VPN you have to run the following command:

sudo openvpn config-filename-goes-here.ovpn

where config-filename-goes-here.ovpn  is the VPN configuration file. The only problem is that every time you want to connect to the VPN you have to manually execute this command. It would be much more convient to run it automatically during the system startup. So let’s do it..

Firstly, you need to create a script in /etc/init.d directory:

sudo nano /etc/init.d/VPNConnection

where VPNConnection is the name of the script (you are free to choose any name that you prefer).

Once the nano editor is open, just copy and paste the following content:

#! /bin/sh
# /etc/init.d/VPNConnection

### BEGIN INIT INFO

# Short-Description: Simple script to start a program at boot
# Description:       A simple script from http://www.stuffaboutcode.comwhich will start / stop a program a boot / shutdown.
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case “$1” in
start)
echo “Starting VPN Connection”
# Connect to the VPN
cd /etc/openvpn
sudo openvpn config-filename-goes-here.ovpn
;;
stop)
echo “Stopping VPN Connection”
# Disconnect
killall openvpn
;;
*)
echo “Usage: /etc/init.d/VPNConnection {start|stop}”
exit 1
;;
esac

Now you can save and close the editor. The next step is to make the script executable:

sudo chmod 755 /etc/init.d/VPNConnection

Before to proceed any further test if the script works as expected. Try to connect to the VPN using the following command:

sudo /etc/init.d/VPNConnection start

and then test the stop command (disconnect from the VPN):

sudo /etc/init.d/VPNConnection stop

The final step is to register the script to be run at boot and shutdown:

sudo update-rc.d VPNConnection defaults

Next time you will boot the system you will be automatically connect to the VPN.

If you want to remove the script from the start-up:

sudo update-rc.d -f  VPNConnection remove

Let me know if you have any comments or suggestion!