Category Archives: Raspberry Pi

Turn your old Raspberry Pi into an automatic backup server

Mirrored from OpenSource.com under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license

Turn your old Raspberry Pi into an automatic backup server

Posted March 16, 2016 by Seth Kenlon

If you’re one of those people upgrading to the Raspberry Pi 3, you might wonder what to do with your old, lesser Pi. Aside from turning it into an array of blinking LEDs to entertain your cat, you might consider configuring it as a microcontroller.

Making backups of our digital lives is, as most of us begrudgingly admit, the most important thing of daily computing that none of us bother to do. That’s because going through the backup process requires us to remember to do it, it takes effort, and it takes time. And that’s precisely why the best backup solution is the solution that you don’t do at all; it’s the one you automate.

Such a system is best when it’s always on, running in the background. And that’s exactly what a Raspberry Pi is best at. You can leave the Pi on all day and all night and never notice it on your power bill, and you can task it with the simple activity of running backups across your home network. All you need is a Raspberry Pi and a big hard drive and you have built, essentially, a custom version of those annoying “easy backup” systems that hard drive companies come out with every few years (you know the ones? the ones you hook up to your network, waste a weekend trying to configure only to discover in a hidden online forum that nothing works as advertised due to a bug in the firmware, which the hard drive company promised they’ll fix “soon” two years ago).

rdiff-backup

First, you need to choose some backup software to have your backup server (your Pi) and your clients (your laptop, desktop, and whatever else) run.

There are several tools for auto backups, but I’ve found over the years that most of the nice slick graphical backup solutions end up falling out of maintenance until they fade away, forcing me to switch to something different. That gets annoying after a while, so I started using rsync, the venerable old UNIX command that’s been around for decades. This served me quite well, but I started finding myself wanting versioned backups of certain files; rsync does a backup for files that have changed, but it overwrites the old version with the new, so if my problem isn’t that a file has been deleted but that I’ve messed up a file beyond recognition, then having rsync’d backup files don’t do me a bit of good, because the backup almost always ends up being the bad version of a file that I was looking to replace.

Then I found rdiff-backup, a simple backup tool based on rsync (it uses librsync), and thereby inheriting its reliability (it has, however, only been around since 2001, so it doesn’t have quite the history that rsync has). Rdiff-backup performs incremental backups locally or over a network using standard UNIX tools (tar,rdiff, rsync, and so on), so even if it does fade away, the backup files it creates are still useful. It’s lightweight and runs on both Linux and FreeBSD, so it’s trivial to run even on the oldest Raspberry Pi.

Server install

You don’t need any special setup to turn your Raspberry Pi into a backup server. Assuming your Pi is up and running, all you need to do is install rdiff-backup from your repository, ports, or extras site.

Client install

As for your clients (that is, the computers that are going to get backed up by your Pi), rdiff-backup can be run on Linux, BSD, Windows, and Mac OS X, so chances are you can use this for all the computers running in your home.

The big hard drive

Even a 64GB SD card isn’t going to go very far for incremental backups, so you’ll need a big hard drive to hook up to your Pi. You know your own data best, so let that be your guide when shopping for a drive. For my home network, I have a relatively small (given the number of multimedia data files I work with) 3TB drive; I do that for a number of reasons, but primarily because I don’t actually back up all of the data I own. A lot of data I work with exists elsewhere anyway, so there’s no need for me to back it up, and things like my music and movie collection I don’t consider vital enough to backup, either. So don’t feel like you have to literally keep track of every last kilobyte; just get to know your data and what matters to you most.

Once you’ve got the hard drive, hook it up to your Pi and format it. Strictly speaking, you may not absolutely have to format it, but if you’re going to have Linux manage the data then you may as well store the data on a native filesystem. This assumes that your backup drive is either new or a drive you want to wipe completely. If not, you can skip this part.

To format a drive on Linux, you must use root permissions. It somewhat depends on what distribution you are running on your Pi (Raspbian, Pidora, and so on), but usually the sudo command is the way to invoke this. No matter what, the tool to use is parted, and as long as you have no other drives attached to your Pi (aside from the SD card it has booted from), then the location of your drive is /dev/sda. For safety, I’ll use /dev/sdx just to avoid potential copy-paste mishaps.

First, confirm the location of your drive:

Then run parted on the drive to confirm its total size:

Look at the line that starts with Disk; this gives you the total size of the drive in megabytes. Jot that down somewhere, because you’ll need it in a moment.

Next, create a new partition on the drive, spanning the entire drive. Only do this if you want to wipe the backup drive completely to make room for all your backups. If there is any data on the drive that you do not want to disappear forever, then do not do this.

Your fresh partition exists now, so create a filesystem inside of it. Note that for this command, you use the partition rather than the disk location. So instead of/dev/sda, for example, you would use /dev/sda1. For best results, also provide the disk with a label (the -L option), which we will use later to auto-mount the drive.

Your drive is now ready for its life as a backup drive.

Auto-mounting the backup drive

The idea of using a Pi for your backup server is, in part, that it’ll always be on. But if something does happen (a power failure, for example, or accidental shutdown) then you want your backup drive to be re-mounted automatically or else any attempt to backup will fail.

To setup auto-mounting for your drive, first create a standard location for it to be mounted. Drives are usually mounted to locations like /media or /run/media, which is fine, but for simplicity just create a directory for it at the root of your filesystem:

And then edit /etc/fstab with root privileges in the text editor of your choice. Add this line:

And finally mount the drive:

The initial backup

The first backup you do is the largest and slowest backup because everything that you want backed up is getting copied to your drive. Subsequent backups are much smaller and faster because only new files (or blobs) or changes to files get copied over.

First, install rdiff-backup on the client computer (the one to be backed up to the Pi). It’s available for the major operating systems.

To make sure that your future backups go as expected, make your first backup using the same command and same setup that you intend to use for the incremental backups. That means you shouldn’t disconnect the big drive from the Pi and plug it into the client so that it goes faster; perform every backup the same way every time, so that you know exactly how to automate it later.

On the Pi, make a directory for the folder you are about to backup from your client. Assuming you want to backup the client’s home directory, create the a mirror of that folder on the backup drive:

And then make sure that the same user owns the directory:

This assumes that user seth exists both on the client and on the Pi. You don’t have to do it that way (rdiff-backup can sign into the Pi as a different user), but it sometimes makes it easier to manage when the backups are mirrors of the source.

This also assumes that you are backing up your home directory. That’s usually a good place to start (I assume that if you’re running Linux, then you can download and replace the base system for free), but you might want to leave out large files that you don’t need to backup. List files and folders to exclude from backups in a file called .excludes in your home directory. At the very least, you can probably safely exclude your trash directory:

The basic rdiff-backup command from your client computer, where 192.168.3.14 is the IP address of your Pi:

That command should kick off a lengthy rsync process in which all files are discovered to not exist on the backup drive, and therefore are copied from the client to the Pi. If it failed, check the permissions involved; your user (on the Pi) must be able to write to the backup drive. Also, your user must be able to successfully SSH into the Pi remotely.

Auto login

Since our aim is to automate this process, the login process that kicks off backups must also happen without intervention. It’s easy to make SSH login automatic; just use ssh key login. This can be done as a single step with ssh-copy-id, which should be in your Pi distro’s repository). To use a special key just for this backup server, use the ssh config file to specify what key to use.

Cron job

Assuming everything has worked so far, there’s no reason an unattended backup should fail. To make that happen, take the same command you used for the initial backup and assign it to a cronjob. This is generally done with the commandcronjob -e:

That cronjob runs the backup command every six hours (on the hour). You can adjust the frequency according to your needs.

Restore data

Now that the backup has been automated, there’s only one command you actually need to remember: how to restore a file from the backups you are so dutifully making.

The simplest restore command is as simple as an rsync or scp:

This command restores from the backup server the most recent version oftux.svg to the same path on your client machine. Notice that you don’t have to worry about special file paths to account for versions; if you want the most recent version, you just restore the same path that is missing or that you have corrupted, and let rdiff-backup resolves that request to the most recent version.

But the –restore-as-of option is more flexible than that. Maybe the version of the file you need is from five days ago:

There are several other means of restoring files, and they’re all listed in the officialrdiff-backup documentation, but in practice I have found that the –restore-as-ofoption is the one that gets used most often. In the less common circumstances that you know the exact day and time of the last good version of a file and need to pull it very specifically from your backups, rdiff-backup handles that for you too; you just have to get the rather unwieldy diff filename, stored alongside your backup data on the backup drive.

For example:

This restores the file paint from the backup performed at 6:06 a.m. on January 24. It does not place, of course, just the diff data of that file into your home directory, but a fully reconstructed version of the file. That’s what rdiff-backup is for.

Back it up

Backing up is important, and your old Pi can help. Set it up today and you won’t be sorry.

Raspberry Pi and Mono – Hello World!

This post will walk through installing mono, compiling, and running the Hello World! code example on a Raspberry Pi

At the time of writing:

  • Raspberry Pi 2, Model B, 1GB RAM
  • Linux: Raspbian 8.0 “Jessie”
  • C#

Update package lists

sudo apt-get update

Install Mono complete

Since we will be compiling our Hello World! program on the Pi we will install the mono-complete package.  If you just wanted to run a complied executable (.exe) all you need is the mono-runtime package.

sudo apt-get install mono-complete

Make your directory and write the code

We’re not making a Solution or Project here, simply writing a C# source file and compiling it.

Create the “project” directory

Using your favorite text editor (I’m using the pre-installed nano here) create your program.

nano HelloWorld.cs

Write the code

Type or paste the following code

Exit and save

Compile the code

mcs HelloWorld.cs

See your executable

Run it!

mono HelloWorld.exe

Links

Mono Project: http://www.mono-project.com/

Built from: http://logicalgenetics.com/raspberry-pi-and-mono-hello-world/

Install WordPress on Raspberry Pi

This post will walk through installing and configuring the software stack (LAMP (Linux, Apache,  MySQL, and PHP)) for running WordPress on a Raspberry Pi

At the time of writing:

  • Raspberry Pi 2, Model B, 1GB RAM
  • Linux: Raspbian 8.0 “Jessie”
  • Apache 2.4.10 (Raspbian)
  • PHP 5.6.17-0+deb8u1
  • MySQL 5.5.44-0+deb8u1 (Raspbian)
  • WordPress 4.4.2

Build and updated from: https://www.raspberrypi.org/learning/lamp-web-server-with-wordpress/worksheet/

Update package lists

sudo apt-get update

Install Apache

First install the apache2 package by typing the following command into the terminal:

sudo apt-get install apache2 -y

5,131 kB of additional disk space will be used

Test the Web Server

By default, Apache puts a test HTML file in the web folder. This default web page is served when you browse to http://localhost/ on the Pi itself, or http://10.2.1.90 (whatever the Pi’s IP address is) from another computer on the network. To find out the Pi’s IP address, type hostname -I at the command line (or read more about finding your IP address) in our documentation.

Browse to the default web page, either on the Pi or from another computer on the network, and you should see the following:

20160208-apache-raspbian-it-works

This means you have Apache working!

Install PHP

PHP is a preprocessor; it’s code that runs when the server receives a request for a web page. It runs, works out what needs to be shown on the page, then sends that page to the browser. Unlike static HTML, PHP can show different content under different circumstances. Other languages are capable of this, but since WordPress is written in PHP, that’s what we need to use this time. PHP is a very popular language on the web; large projects like Facebook and Wikipedia are written in PHP.

Install the PHP and Apache packages with the following command:

sudo apt-get install php5 libapache2-mod-php5 -y

18.6 MB of additional disk space will be used

If you haven’t changed directories to /var/www/html yet do so now.

Test PHP

Create the file index.php:

sudo nano index.php

Put some PHP content in it:

<?php echo "hello world"; ?>

Now save the file (Ctrl+x y) . Next delete index.html because it takes precedence over index.php:

sudo rm index.html

Refresh your browser. You should see “hello world”. This is not dynamic but it is still served by PHP.

20160208-php-raspbian-hello-world

Try something dynamic, for example:

<?php echo date('Y-m-d H:i:s'); ?>

Or show your PHP info:

<?php phpinfo(); ?>

20160208-php-raspbian-phpinfo

Install MySQL

MySQL (pronounced My Sequel or My S-Q-L) is a popular database engine. Like PHP, its overwhelming presence on web servers enhanced its popularity. This is why projects like WordPress use it, and why those projects are so popular.

Install the MySQL Server and PHP-MySQL packages by entering the following command into the terminal:

sudo apt-get install mysql-server php5-mysql -y

85.6 MB of additional disk space will be used.

Note: When installing MySQL you will be asked for a root password. You’ll need to remember this to allow your website to access the database.

Download WordPress

You can download WordPress from wordpress.org using the wget command. Helpfully, a copy of the latest version of WordPress is always available at wordpress.org/latest.tar.gz and wordpress.org/latest.zip, so you can grab the latest version without having to look it up on the website. At the time of writing, this is version 4.0.

Navigate to /var/www/html/, and download WordPress to this location. You’ll need to empty the folder first (be sure to check you’re not deleting files you need before running rm); change the ownership of this folder to the pi user too.

Now extract the tarball, move the contents of the folder it extracted (wordpress) to the current directory and remove the (now empty) folder and the tarball to tidy up:

Set up your WordPress Database

To get your WordPress site set up, you need a database. Run the mysql command in the terminal and provide your login credentials (e.g. username root, password password):

mysql -uroot -ppassword

Here I have provided my password (the word password) on the command line; there is no space between -p and your password.

Alternatively you can simply supply an empty -p flag and wait to be asked for a password:

mysql -uroot -p

Now you will be prompted to enter the root user password you created earlier.

Once you’re connected to MySQL, you can create the database your WordPress installation will use:

mysql> create database wordpress;

Note the semi-colon ending the statement. On success you should see the following message:

Query OK, 1 row affected (0.00 sec)

Exit out of the MySQL prompt with Ctrl + D.

WordPress Configuration

You need to find out your Pi’s IP address to access it in the browser, so in a terminal type the command hostname -I.

Navigate to http://YOUR-IP-ADDRESS e.g. http://192.168.1.5 in the web browser on your Pi.

You should see a WordPress error page; this is good! Click the big button marked Create a Configuration File followed by the Let’s go! button on the next page.

Now fill out the basic site information as follows:

Database Name: wordpress
User Name: root
Password: <YOUR PASSWORD>
Database Host: localhost
Table Prefix: wp_pi314159_

Upon successful database connection, you will be given the contents of your wp-config.php file:

Copy this text, return to the terminal on the Pi and edit the file with:

nano wp-config.php

Paste the text into this file, and save and exit with Ctrl + X, then Y for yes and Enter.

Now hit the Run the install button.

Welcome Screen

Now you’re getting close.

Fill out the information: give your site a title, create a username and password, put in your email address and untick the search engines box. Hit the Install WordPress button, then log in using the account you just created.

Now you’re logged in and have your site set up, you can see the website by visiting your IP address in the browser on the Pi or another computer on the network. To log in again (or on another computer), go to http://YOUR-IP-ADDRESS/wp-admin.

Friendly Permalinks

It’s recommended that you change your permalink settings to make your URLs more friendly. To do this, log in to WordPress and go to the dashboard. Go to Settings then Permalinks. Select the Post name option and click Save Changes. After saving, you will be prompted to update your .htaccess file. You probably don’t have one yet, so add one in /var/www/html/ by typing nano .htaccess; note this is a hidden file, so it starts with a dot. Then paste in the contents provided:

Save the file and return to the website homepage. Click on the post title or the sample page link and you’ll probably see a Not Found error page. This is because the rewrite module has not been enabled in Apache. To do this, enter:

sudo a2enmod rewrite

You’ll also need to tell the virtual host serving the site to allow requests to be overwritten. Do this by editing the virtual host file (with root permissions):

sudo nano /etc/apache2/sites-available/000-default.conf

You will need to add a directory block

Notice AllowOverride setting is set to All.

Save the file and then restart Apache with:

sudo service apache2 restart

Once it’s restarted, refresh the page and it should load successfully. Now posts have URLs like /hello-world/ instead of /?p=123, and pages have URLs like /sample-page/ instead of /?page_id=2.

Customization

WordPress is very customizable. By clicking your site name in the WordPress banner along the top of the page (when logged in), you’ll be taken to the Dashboard. From here you can change the theme, add pages and posts, edit the menu, add plugins and lots more. This is just a taster for getting something interesting set up on the Raspberry Pi’s web server.

What Next?

  • PHPmyAdmin – coming soon

Raspberry Pi Accessories

Miniature WiFi (802.11b/g/n) Module

Webcam

Polaroid 5V DC Battery

Build a Raspberry Pi Webcam Server using Motion

This tutorial is for a Raspberry Pi running the out of the box Raspbian OS.

Download and install the Motion package using the following command:

sudo apt-get install motion

Optional, but recommended if you are just starting out.  Make a backup of the out of the box configuration file.  Change to the motion directory with:

cd /etc/motion

and copy the default motion.conf file:

sudo cp motion.conf motion.conf.bak

Next we need to make some changes to the configuration file motion.conf.  The nano file editor comes pre-installed so we’ll use it:

sudo nano /etc/motion/motion.conf

Find the following lines and make the following changes.  (Ctrl+W is search in nano)

  • daemon off -> daemon on (this will run allow the program to run as a service)
  • stream_localhost on -> stream_localhost off (this will allow you to view the stream webpage from other computers)

Other optional/tweakable parameters:

  • framerate 15 (changes the number of frames per second (fps) that are captured)
  • width 640 (This changes the width of the images)
  • height 480 (This changes the height of the images)
  • target_dir /var/lib/motion (This is where Motion stores images and videos when it detects motion, this directory needs to be writable)

Save your changes

Now we need to enable the motion daemon by editing the motion file:

sudo nano /etc/default/motion

Make the following change:

  • start_motion_daemon=no -> start_motion_daemon=yes

Next we need to make sure the motion capture directory is writable

sudo chmod 777 /var/lib/motion

Now make sure the camera is connected and start the service with the command:

sudo service motion start

To stop the service, the command is:

sudo service motion stop

Now you can view the camera from a browser on the Pi (or other computer if stream_localhost was set to off) by connecting to your Pi’s IP address and Motion’s default port 8081

http://192.168.1.70:8081

To find your Pi’s IP address you can type the following in the terminal (also 5 Ways to Check the IP Address in Linux):

ip addr show

and look for the line that starts with inet


Common errors

  1. Webpage is not available or error connection refused.
    • Check to make sure you turned the localhost restriction off (stream_localhost off) in the motion.conf file.
  2. The webpage crashes every time motion is detected.
    • Check to make sure the motion directory is writable, any external drives are plugged in and mounted.

Troubleshooting

If you made a backup of the original motion.conf file and want to restore it, you may do so by changing to the motion directory:

cd /etc/motion

and copying the backup file over your modified version:

sudo cp motion.conf.bak motion.conf

If you get any “Permission denied” messages add sudo to the beginning of your command

The webpage with the webcam stream does not work in Microsoft’s Internet Explorer.

Error logging (http://www.lavrsen.dk/foswiki/bin/view/Motion/ErrorLogging)

Motion reports errors to the console when it runs in non-daemon mode. And it outputs even more information when run in setup mode.

Error logging has been implemented so that errors during daemon (background) mode are logged in the syslog.

The syslog is in most Linux systems the file /var/log/messages (e.g. RedHat/Fedora) or /var/log/syslog and /var/log/user.log (e.g. Debian).

Based on the tutorial from pimylifeup: http://pimylifeup.com/raspberry-pi-webcam-server/