Building a Network Storage Device
In This Chapter
Unless you’re deep into data backup and networking solutions, you’ve probably never heard of a network storage device (NAS). As the name suggests, a NAS is a hard drive that’s accessible on a network. In the case of a home network, this means you can access a hard drive from any computer on the network.
This is useful for a variety of reasons. If you have a lot of computers in your home, you can set up a NAS so they can all access and share data on one hard drive. This is great for media and any other files you need to get to in your home.
Beyond just accessing files remotely, a NAS is great for creating a backup of your home computer. You can set it up so your primary computer automatically backs up everything on your hard drive to the NAS once a day. This way, no matter what happens to your computer’s hard drive, you’ll always have a backup of all your important data. What’s more, you can automate this whole process so once it’s set up, you don’t even have to think about it.
This might sound complicated, but the setup process is actually pretty simple. In this chapter, you learn to build your NAS, connect your external hard drive to your Raspberry Pi, share folders with your primary computer, and establish a daily backup system.
Getting Started with Your NAS
A NAS operates as a file server, so everything you store on the NAS is accessible from any computer on your network. Most often, a NAS is run on a computer where nothing else is running. Typically a stripped-down operating system is used because there’s really no reason for anything else to be going on.
For that reason, a Raspberry Pi is a fantastic device to turn into a NAS. It doesn’t consume a lot of power, it has a very small footprint, and it’s easy to tuck away under a shelf somewhere. It’s also simple to set up and use, so once you get it working, you’ll rarely notice it’s there.
In this project, you’ll set up your own NAS system for about $120 or so. Commercial solutions tend to retail for anywhere between $300 and $400, so doing this yourself is a great way to save a bit of cash.
SOFTWARE SOLUTION
Technically, you can run a NAS on just about any computer you have. A number of different operating systems exist to turn your computer into a NAS, and the route in this chapter certainly isn’t your only option. You’ll be doing a bit of a DIY method by setting up a sharing service called Samba, but there are other options available if you’d prefer something else.
The Benefits of a NAS
Having a hard drive that’s accessible on your home network is useful on its own. You can store whatever you want on this drive. For example, if you want to make your home movies accessible from any computer in your house, you can store them on your NAS. The same goes for photos or whatever else you want.
Likewise, let’s say you set up the XBMC media center in Chapter 12. You could point that media center to your NAS, drop a bunch of video files on there, and play those videos from any computer. It’s a pretty slick little system.
Media storage of all kind is a great usage for a Raspberry Pi–powered NAS. If you have a massive music library, you can store all your MP3 files on it, and everyone in your household can access your music library.
Properly set up, having a backup service you don’t have to think about can save you if your hard drive ever fails. Using your NAS to essentially make a clone of your computer’s hard drive once a day is a simple, effective way to keep a backup of your important data.
The good news is that you can do both of these things. You can use your NAS to store specific files you want to access from your network, and you also can use it to back up your computer. You just need a hard drive big enough to handle both.
What Else You Need
In addition to your Raspberry Pi and its usual components, you’ll need a USB external hard drive for this project. Any USB external hard drive will work, so grab whatever’s cheap.
If you plan on backing up your entire primary computer, you’ll want to grab a hard drive that’s at least as big as your primary computer’s hard drive. If your hard drive is 1 terabyte (TB) in size, get an external hard drive that’s at least that big. If you want to do both a backup and store additional files on the NAS, get a hard drive that’s bigger than the one you want to back up. For example, if you’re backing up a computer with a 1TB hard drive, you should get a 2TB external drive so you have plenty of room to store both the backup and the additional files you want to access.
External hard drives are really affordable these days. If you hit up the right sales, you can usually find a 2TB external hard drive for about $80. Just be sure you buy one with its own power supply so you don’t have to rely on the Raspberry Pi for power.
HARDWARE HELPER
One hard drive is great and technically all you need for this project, but if you want to practice good backup behavior, you should grab a second one as well. This way, you end up having two backups of all your data, so if something goes wrong and one hard drive fails, you’ll have a second one available.
Mounting the External Drive
When you have your external hard drive in hand, all you need to do for setup is plug it in to your Raspberry Pi. Then you’ll get the hard drive mounted and formatted so you can start the installation process.
Essentially, you need to add support to your Raspberry Pi to read a specific drive structure, format your hard drive to match that structure, and tell your Raspberry Pi where the hard drive is. This is usually automatic on an operating system like Windows or Mac, but you’ll need to do this manually on your Raspberry Pi.
You need Raspbian installed for this project. Refer to Chapter 8 if you don’t have an SD card with Raspbian installed already.
Adding NTFS Support for Raspbian
In order to use an external hard drive with your Raspberry Pi, you need to install NTFS support. This is the file system most hard drives use, and the Raspberry Pi needs a little help before it can read them. Here’s what to do, working from the command line:
sudo apt-get install ntfs-3g
, and press Enter.That’s it for NTFS support. Next, you need to get your hard drive mounted on your Raspberry Pi.
Creating Mounting Directories
Unlike a hard drive on your personal computer, your Raspberry Pi doesn’t immediately recognize and mount an external hard drive when it’s attached to it. You need to do that manually for your Raspberry Pi.
Here’s what to do:
sudo fdisk -l
, and press Enter. You’ll see a display of your hard drives alongside your SD card. The first disk you see, /dev/mmcblk0, is your SD card. The second disk should read something like /dev/sdb1 or /dev/sda1. (If you’re using two hard drives, you’ll see both.) Make a note of the sdb or sda addresses. You’ll need this information later./dev/sda1
with the information you gathered earlier. Type in sudo mkfs.ntfs /dev/sda1 -f -v -I -L untitled
, and press Enter. Wait for the hard drive to finish formatting.
Be sure you pick the right drive before you format it.
sudo mkdir /media/hdd1
, and press Enter. If you’re using two hard drives, also type in sudo mkdir /media/hdd2
, and press Enter.sudo mount -t auto /dev/yourharddrivename /media/USBHDD1
, and press Enter. For example, it should read sudo mount -t auto /dev/sda1 /media/hdd1. If you’re using two hard drives, you’ll need to type in the command again with the second hard drive; for example, sudo mount -t auto /dev/sdb1 /media/hdd2
.sudo mkdir /media/hdd1/shares
, and press Enter. If you have a second hard drive, type in sudo mkdir /media/hdd2/shares
, and press Enter.With that, your hard drives are mounted and ready for sharing. Now you just need to set up the software so your Raspberry Pi can actually share those folders.
Installing Samba
Before you can set up your NAS, you need to get some software that can share your Raspberry Pi’s hard drive with other computers on your network. You’re going to use Samba to do this.
Samba is free software that was originally developed in 1992 by computer programmer Andrew Tridgell. It’s a file-sharing service that enables computers to share data easily over a network.
You’ll use Samba in this project because it supports Windows, OS X, and Linux. So no matter what type of computers you have on your home network, they’ll be able to access your NAS and save files to it.
Originally, each operating system had its own file-sharing system. If you wanted to share a folder on a network, you needed to ensure it was compatible among the different computers. That’s still the case, but fortunately, nearly every modern operating system supports a variety of file sharing protocols. Samba was originally just for Windows, but it works perfectly fine on Linux and Mac these days.
Downloading Samba
First things first, you’ll need to download Samba. Here’s what to do:
sudo apt-get install samba samba-common-bin
.Wait for Samba to download and install.
Backing Up Samba’s Configuration Files
You’re going to edit the Samba configuration files quite a bit here, so first, make a backup copy just in case things get messed up. Here’s how:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup
.When Samba is set up, you can access your files from any computer on the same network.
This makes a copy of the configuration file so you can restore it later in case something goes wrong.
Editing Samba’s Configuration Files
Next, you’ll edit the configuration file. You’re going to use nano
for this one. Here’s what to do:
sudo nano /etc/samba/smb.conf
, and press Enter.[Backups]
comment = Backup Folder
path = /media/hdd1/shares
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no
When you’re finished, tap Ctrl+X to save and exit.
sudo /etc/init.d samba restart
, to restart the Samba server with your new settings.sudo useradd backups -m -G users
, and press Enter.sudo passwd backups
, and press Enter. You’ll be prompted to type in your password two more times.sudo smbpasswd -a backups
, and press Enter. Retype your password a couple times when you’re prompted to, and you’re all set.With that, the folder should be shared and working.
PI POINTER
You can swap out much of this information for your own if you want. [Backups] is the name of the hard drive. You can call it whatever you want, but be sure you swap out backups in steps 6, 7, and 8 with your updated information.
Configuring Your Raspberry Pi to Mount to External Drives on Boot
With the Samba share working and your external hard drive sharing all set up, your NAS should be good to go. However, the Raspberry Pi won’t automatically mount your hard drives when it boots up. You need to do a little more work to get that to happen:
sudo nano /etc/fstab
, and press Enter. This loads the file systems table that launches on startup./dev/sda1 /media/hdd1 auto noatime 00
/dev/sdb1 /media/hdd2 auto noatime 00
The fstab file enables you to mount your external hard drive automatically.
Your Raspberry Pi will now automatically mount the external hard drive on startup. This is great in case the Raspberry Pi powers off for some reason, or if you need to move it because you can just plug it in and go without worrying about setting up anything again.
Accessing Your NAS from Your Other Computers
Now to test to be sure a computer on your home network can access the Raspberry Pi. You’ll test this with your personal computer. The process is a little different on Windows versus Mac, so follow the guide for your operating system.
Windows:
Here’s how to be sure you can access your Raspberry Pi and the external hard drive from your Windows computer:
You now have access to your external hard drive over your network.
Mac:
Now, let’s access your Raspberry Pi’s external hard drive from your Mac:
The mounted hard drive works just like it’s connected to your computer.
You should now have access to your Raspberry Pi’s hard drive.
Now that everything appears to be working properly and you can access your Raspberry Pi from other computers on your network, check to be sure it’s actually working. Here’s how:
cd /media/hdd1/shares
, and press Enter.ls
, and press Enter.Create a test file to ensure everything’s syncing properly.
You should see the file you just created on your personal computer listed. If it’s there, everything’s working properly.
PI POINTER
With everything set up, you can now start moving files onto your NAS from your personal computer. Remember that they’ll be accessible on the network from any other computer in your house. If your NAS is on Wi-Fi, this process might be a little slow at first, so connect it with an Ethernet cable if you’d like it to move a little faster.
Adding a Second Hard Drive
If you’re using two hard drives, you haven’t really done anything with the second one yet besides mount it. If you want true data redundancy, you need to ensure everything you put on the first hard drive is copied onto the second one.
The Importance of Data Redundancy
Your Raspberry Pi is hooked up to a network, the hard drives are mounted, file transferring works, and you can now save any file you want to your network hard drive with just a couple clicks. This is great, but if you want a truly bulletproof backup, you also should add a second hard drive that clones the first one.
When you create a backup, you also should make a second backup of your hard drive in case something fails. If anything does go wrong, you’ll always have backup. It might sound like overkill, but it’s always better to be safe rather than sorry with your data.
This is totally optional, but it’s a good idea nonetheless. Hard drives are cheap enough that you can easily add a second one for under $100. If you’re storing anything important—like photos, videos, or anything else you find irreplaceable—it’s well worth the extra cash to ensure you’ll always have your data safe.
To set up extra data redundancy, you’ll basically just clone your external hard drive using software called rsync. Your NAS ends up working like this:
All this happens behind the scenes, so once you set it up, you won’t have to think about it.
Setting Up rsync Backup
You’ll set up your syncing backup using software called rsync. This essentially clones your first external hard drive onto your second one. This is entirely optional, but if you want the data redundancy and three sets of your files, it’s a good idea. Hop back to the command line on your Raspberry Pi.
With a schedule, you can ensure you’ll always have a recent backup.
sudo apt-get install rsync
, and press Enter. This downloads and installs the rsync software.crontab -e
, and press Enter. This opens the cron scheduling table. This tells your Raspberry Pi to run something on a schedule.04 * * * rsync -av --delete /media/hdd1/shares /media/hdd2/shares
The command tells your Raspberry Pi to run rsync at 4 A.M. every day. You can change the time by changing the 4 in the code.
rsync will now run at 4 every morning. It will compare your first and second hard drives, copy everything from it that’s new, and delete anything that doesn’t match. It essentially clones the first drive onto the second every morning so you have two backups of all your data all the time.
PI POINTER
You can use crontab
to run anything you want on a schedule, not just rsync. The text file makes it look pretty complicated, but all it does is tell your Raspberry Pi to run a specific program at a specific time.
Setting Up Automated Backups
Your Raspberry Pi now has one or two external hard drives mounted, and you can access them from your Windows PC or your Mac. This is great if you just want a hard drive on your network you can access from any computer you own. All you need to do is copy files to your Raspberry Pi’s hard drive, and you’re all set. For example, you could store all your music and movies on your NAS so you can get to them from any computer in the house. Or you could store photos there so you don’t have them spread across multiple computers.
However, if you want, you also can create automated backups of your primary computer. Essentially, you’ll use some software to automatically back up your hard drive onto your Raspberry Pi. This is useful if you don’t want to worry about backing up specific files. Instead, it happens on a schedule, every day.
So next, you’re going to create a system to automatically back up your home computer’s hard drive to your NAS. Then you’ll have two (or three) hard drives filled with the same data, so if anything goes wrong, you’ll have backup copies of everything.
You actually have a variety of options to do this, but for this project, you’re going to use backup software called CrashPlan.
Downloading and Installing CrashPlan
CrashPlan is free backup software that works on both Windows and Mac computers. Although both operating systems have built-in backup options, neither works particularly well with the Raspberry Pi’s system. CrashPlan is a better alternative and much easier to set up.
CrashPlan is free if you want to just back up to a local hard drive, but it also has paid plans you can use to back up to cloud servers if you’re interested. Because you’ll have two backups at your home, this probably isn’t necessary, but if you’re worried about losing your data, it’s always better to have backups offsite as well.
Here’s how to download and install CrashPlan:
code42.com/CrashPlan/download
to download CrashPlan.Once the installation is complete, it’s time to point CrashPlan to your Raspberry Pi and set up a schedule.
SOFTWARE SOLUTION
CrashPlan isn’t the only program that does this, but it’s the easiest to use and works across multiple platforms. You also can use Time Machine on a Mac or Windows’s built-in backup software, but you might need to tweak the settings on your NAS to get them working.
Configuring CrashPlan to Back Up to Your NAS
Once CrashPlan is installed, you need to tell it where to back up your data to. Here’s how:
Select your NAS from the CrashPlan menu.
CrashPlan is now scheduled to automatically back up all the data on your home computer to your NAS.
Scheduling Backups in CrashPlan
By default, CrashPlan automatically backs up all your files whenever it feels like it. If you work on your computer a lot, this can be a bit annoying because it’ll slow things down a bit while it runs. It’s best to set up a schedule so CrashPlan only backs up when you’re probably not using your computer.
Create a schedule for your backups so you always have access to your newest files.
Now your backups will only run when you want them to.
PI POINTER
File sharing tends to take up a lot of bandwidth on your network, so it’s best to run your backups when you’re not likely to be using your computer. Pick a time in the very early morning or late evening that won’t interfere with the rest of your computer use.
Restoring a Backup
If things go bad and you lose all your data on your computer, restoring your CrashPlan backup is quick and easy.
Here’s what to do:
It takes a while for CrashPlan to do its thing, so let it go and eventually all your files will be restored to the same location where they were originally.