Raspberry Pi (Model B) NAS and Backup

Raspberry Pi B: Wheezy.

It seems that there is no one place to do all of the things you need to do to make this happen. So here are my detailed notes for the next time I have to set up a Linux based NAS.

Below is a photo of 1) not a model B, and 2) not my Pi.


I actually couldn’t get a non-self powered hard drive to work with my Pi. I think they made the later versions easier to hook up.

I bought a Raspberry Pi Model B on the first day they were for sale (February 29, 2012) and spent the following weekend finding a power cable and SD card that would work with it.

I installed the Raspbian image following the directions on the web site.
[need to find those notes – I know I have them]

And not being too Linux savvy, I immediately started Windows X and was disappointed in the performance. Dang it’s slow. Even a 10yr old closet PC was faster. So I gave up on it and let it ferment in a drawer for a few years.

Recently, I decided to get serious about my backup strategy. I had been backing up inconsistently – sometimes going for 6mos without a backup – so my thoughts returned to the RasPi. I could set up a NAS and a backup with it.

Initially, I considered two external drives connected via the two USB ports. I began by connecting an external 500GB external 3.5 HD to the USB, but the Pi didn’t recognize it. As I discovered, there wasn’t enough power to run it from the Pi. I tried a 1.5GB external 5.25HD that had its own power source, and it was recognized. In fact, I discovered that if I unplugged the Pi from its own power source, that the USB drive would “backpower” the Pi via the standard USB port. But I also discovered that it was enough power to light up the lights, but it didn’t run the Pi, so I plugged the Pi back in to its own power source.

One of the benefits of running a Pi NAS is that you can use NTFS (MS Windows) formatted disks and not Linux. So if I needed to, I could unplug it from the Pi and plug it directly into a Windows PC.

I still haven’t changed the password for the user “pi”. I did change root password. The Pi is internal only and isn’t serving port 80 externally. To find the version of Linux:

$ cat /etc/*release
 PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)"
 NAME="Raspbian GNU/Linux"
 VERSION="7 (wheezy)"

My Pi is headless – that is, it doesn’t have a keyboard, mouse, or monitor. To manage a headless Pi, install Putty. The original server name is raspberrypi, so to connect via Putty, connect to RASPBERRYPI on port 21.

To make life easier, in the Putty sesison, enter:

$ bash

That allows you to use up/down arrows to recall commands.

Also, set your preferences in Putty so that right mouse click copies but doesn’t immediately paste – honestly I don’t know why anyone would ever want to do that – and it’s the default setting in Putty.

  • The brief overview of the steps:
  • Set up your Pi.
  • Connect your NTFS drive.
  • Mount the drive.
  • Create a Linux user.
  • Install Samba.
  • Create a Samba user.
  • Configure Samba.
  • Set up crontab event.

First off, you need to mount the drive. Make sure your drive is recognized. List the drives:

$ sudo fdisk -l
mmcblk0     179:0 0 7.5G 0    disk
+-mmcblk0p1 179:1 0 56M  0    part     /boot
+-mmcblk0p2 179:2 0 7.5G 0    part     /
sda           8:0 0 1.4T 0    disk
+-sda1        8:1 0 1.4T 0    part

In the above output, mmcblk0 is the SD card, and the mmcblk01 and mmcblk02 are the partitions. The sda is the external drive, and sda1 is the partition. The first external drive will be sda, and if you have a second, it will be sdb, etc. According to the above, the sda1 partition is NOT mounted.

Side Note: I did all this as root, since I did not and still don’t have a handle on the ownership/permissions. I do understand that after I create a user such as backupUser and create the same user in Samba that I had to mount the drive as backupUser for Samba to work correctly.

My Pi recognized that drive on bootup and created a mount point for it automatically. Go to the drive mounts and see if your drive is already set up. Note that the default drive name can be changed by plugging it in to a Windows PC and renaming it there.

Go see what mount locations are already set up:

$ cd /media
$ ls -l

Displays mount locations for the user you are currently logged in as.

Let’s mount that sda1 partition to /media/1_5TBExt:

$ sudo mount -t auto /dev/sda1 /media/1_5TBExt

Side Note: mount the drive as your backupUser and not root. If you mount it as root, then the permissions will be incorrect. I can’t stress this enough.

Make sure you can view files there:

$ cd /media/1_5TBExt
$ ls -l

And if you list your disks, you should see that the sda1 partition is now mounted:

$ lsblk
sda         8:0 0    1.4T    0 disk
+-sda1      8:1 0    1.4T    0 part  /media/1_5TBExt
mmcblk0     179:0    0 7.5G  0 disk
+-mmcblk0p1 179:1    0 56M   0 part  /boot
+-mmcblk0p2 179:2    0 7.5G  0 part  /

Now we need to create a Linux user for the backup. You don’t want to be root or pi, since those are potential targets, although I suppose you could if you wanted to. To avoid a lot of frustration, just use root for everything – and light all the Linux experts’ hair on fire. Normally, you want to create a user for a particular reason and use it rather than using root, since root provides god-like power to your Pi and potentially your network.

Note that you need to use a Linux user AND a Samba user – BOTH. For Linux to share files with a Windows PC, you need to install and run Samba. Samba lets you use Windows connect to your Linux box and view mounted drives. Samba contains the SMB protocol, support for the Windows naming service (WINS), and support for joining Windows workgroups.

Create a new Unix user and password:

$ sudo useradd backupUser -m -G users
$ sudo passwd backupUser

Side Note: you will need to create a Samba user with this SAME username, as I mentioned before.

Install NTFS Driver

$ sudo apt-get install ntfs-3g

Install Samba

Now that you have a drive mounted and you can see it and navigate to it on your Pi, you can move on to installing Samba. Each of these steps has its own frustrations. So if you have gotten to this point, congratulations. The entire process took me about 8 hours.

Before we install Samba, update your app list and upgrade your Linux so that you know that you have all the latest patches.

apt-get update updates the list of available packages and their versions, but it does not install or upgrade any packages.

apt-get upgrade actually installs newer versions of the packages you have. After updating the lists, the package manager knows about available updates for the software you have installed. This is why you first want to update.

Update your Linux applications:

$ sudo apt-get update

Upgrade Linux:

$ sudo apt-get upgrade

It’s common practice to update/upgrade before installing any new applications.

Now let’s install Samba:

$ sudo apt-get install samba samba-common-bin

Once installed, you will modify the Samba config file, /etc/samba/smb.conf. But first, back up the config file in case you need to restore it:

$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old

Side Note: I did actually restore smb.conf at one point, because I spent so much time trying to figure out why my Windows PC couldn’t see the drive – and I kept fiddling with the smb.conf, based on what various Internet people said to do. I can tell you that you need only do the minimum, described below.

BTW, to restore, you copy the old file to the original name:

$ cd /etc/samba/
$ cp smb.conf.old smb.conf

Now edit the config file with nano:

$ sudo nano /etc/samba/smb.conf

You need to change the workgroup = WORKGROUP line to your actual workgroup name. If you don’t know anything about workgroups, then your workgroup is probably WORKGROUP.

Find #security = user line and remove the comment tag: #. This enforces security, which Windows will want you to do. You can look up how to set this up without security – it’s not recommended, since it’s a security risk.

Add this to the end of the file:

comment = Backup Folder
path = /media/1_5TBExt
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no

The bracketed name [Backup] could be anything – this is the name of your shared drive when you navigate to it in Windows. Save by Ctr+X. Press Y, then Enter to save the file and exit nano.

Restart Samba:

$ sudo /etc/init.d/samba restart

I forgot this next step, so don’t forget this step! Create a Samba user of the same name as your Linux user. To create a Samba user:

$ sudo smbpasswd -a backupUser

If you want to view the Samba users:

$ sudo pdbedit -L -v

In case you need to remove a Samba user:

$ sudo pdbedit -x -u username

Here’s where things go wrong. If the drive is owned by root, then you can’t log in from Windows. Change to the user you created: backupUser:

$ su backupUser

Now you will need to mount the drive as this user:

$ sudo mount -t auto /dev/sda1 /media/1_5TBExt

If you get the error: Mount is denied because the NTFS volume is already exclusively opened… it means that the drive is already mounted and assigned. Find out where it’s mounted:

$ lsblk

List the drives:

$ sudo fdisk -l
mmcblk0     179:0   0  7.5G 0       disk
+-mmcblk0p1 179:1   0  56M  0       part /boot
+-mmcblk0p2 179:2   0  7.5G 0       part /
sda           8:0   0  1.4T 0       disk
+-sda1        8:1   0  1.4T 0       part /media/1_5TBExt

If you see your drive mounted, make sure that your user is the owner or that the group has read/write.

$ cd /media
$ ls -l

This will display your drives and the owners. If your drive owner is root:root, you need to change the owner to your backupUser using chmod. Since I don’t have a good grasp of permissions, I’ll let you figure this out. Meanwhile, here’s a pleasant photo for you while you pull your hair:

Rays Forest Dawn Sage Dusk Trees Photography Wallpaper Iphone 4

Go to a Windows PC on your network and connect to your server. In this example, it’s RASPBERRYPI. You should see your “Backup” location as well as the user’s home directory. Make sure you can read and write to this location – save a text file there or something.

If you can see the server and the location but you can’t write to it, then you probably didn’t mount the drive as the backupUser.

I later added another Samba share location for my /home/backupUser/myfiles location so that I could back up my web server files. This required another entry in the smb.conf file for that location.

comment = Backup For Web
path = /home/backupUser/myfiles
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no

Set Up a Web Server

The hard part is done. Getting the drive mounted and the permissions set is a battle. The rest is pretty easy.

The web server will run Apache (that’s my preference), MySQL db, and PHP. If you didn’t update/upgrade today, then do it now:

$ sudo apt-get update 
$ sudo apt-get upgrade

Install Apache

The following command installs Apache 2.4 web server:

$ sudo apt-get install apache2 apache2-utils

Once installed, check the installation by navigating to: http://RASPBERRYPI/ (or whatever your Pi server name is) from anywhere on your network. You should see an html page: It works! The web server files are here:

$ cd /var/www

Side Note: My Apache install didn’t go perfectly. I didn’t update/upgrade before installing it; I did it after installing Apache – and that worked for me.

Install PHP

The following command installs the PHP 5 and some PHP libraries:

$ sudo apt-get install libapache2-mod-php5 php5 php-pear php5-xcache php5-mysql php5-curl php5-gd

Note: the PHP.INI file is here:


After installing, test it to make sure it’s working by creating a index.php file and calling PHP Info. Change the directory to the default document root:

$ cd /var/www/

Create a php file for testing the db connection:

$ sudo nano test.php

and enter

 <? print "PHP is working."; ?>

Test it. For some reason, it won’t work if you create index.php. This is likely an owner problem. The default user for Apache is www-data. The following command changes the ownership to the Apache default user recursively for all files in the selected directory and its subdirs:

$ sudo chown -R www-data:www-data /var/www *

As an aside, while you’re trying to figure out how to set permissions – a major pain – you can clean up the install files by running:

$ sudo apt-get clean

The PHP files need to have their permissions changed to executable – I think. Although I’m not too worried about giving out too much permissions, I do want to know how to set permissions properly.

Once you have installed Apache, gotten the It works! to work and installed PHP and gotten php to work, you can now install MySQL.

Install MySql

Installing PHP was much easier to install on the Pi than it ever was on a Windows PC. MySql also is pretty easy.

$ sudo apt-get install mysql-server

The installation will ask you for a master password. Don’t forget it.

I repeat – don’t forget your master password.

Create a database called “test”:

$ mysql -u root -p
Enter password:**************
mysql> CREATE DATABASE test;
mysql> USE test;
mysql> exit;

Create a PHP file that connects to test:

$ cd /var/www
$ sudo nano dbtest.php

Paste the following, substituting your user and password:

Save the php file, and open mySql again.

$ mysql -u root -p
Enter password:**************
mysql> USE test;

Create a table in test called “people”- just paste this at the mysql prompt:

mysql> CREATE TABLE people (
name varchar(255) NOT NULL,
address varchar(255),

Add a record:

mysql> INSERT INTO people (name, address) VALUES ('Lars','123 Monsen Ave');

You don’t need to enter the IP address; you can use the pi server name:


If all goes well, you should see your people data.

Set Up Scheduled Backup

First, create a .sh file that contains the commands that you want to run. I just added mine in the backupUser home directory.

$ sudo nano backupDaily.sh

In the backupDaily.sh file, enter:

sudo cp /var/www/myfiles/*.txt /home/backupUser/myfiles

Make it executable:

$ chmod +x backupDaily.sh

This file will copy all *.txt files in /var/www/myfiles/ to /home/backupUser/myfiles
It copies the files from the inaccessible web dir to one that is accessible to Windows via Samba

Test it but entering:

$ sh backupDaily.sh

Make sure it copies all the files to the backup location.

Now schedule it using crontab.

I used crontab -e logged in as root because I couldn’t get it to work any other way:

$ crontab -e

At the bottom of the file, add:

0 23 * * * /home/backupUser/backupDaily.sh

This runs the backupDaily.sh file at zero minutes on the 23rd hour (midnight). Note – add a blank line after this line. I read this somewhere: “ALWAYS add a line break to your Cron Job. If your Cron Job does not run it’s probably the reason why” – so I add a line break, just in case.

You can add lines to the script. I added a line to compress the files into a single file with the date and time stamp. Added to the cron script:

tar -vczf DailyBackup_$(date +%Y-%m-%d_%H%M).tar.gz /home/backupUser/myfiles/*.txt

Create a Windows Batch File

Make sure the Windows PC has the credentials saved for the raspberrypi server. On the Windows PC, open a notepad and save a backupcopy.bat file. Add the line:
copy \\RASPBERRYPI\Backups\*.txt D:\MyBackups\pibackup\

I will probably just copy over the gz file eventually.

Change Server Name

To change the server name: At the terminal, type the following command to open the hosts file:

sudo nano /etc/hosts


sudo nano /etc/hostname

Add a User to SU

Login as root or su to get root prompt:


An editor will open. Find the line:

root ALL=(ALL) ALL

and add your new user with super user privileges:

newusername ALL=(ALL) ALL

Type Ctrl+X Type Y to save and close.

Helpful Notes

$ cat /etc/*release
PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)"
NAME="Raspbian GNU/Linux"
VERSION="7 (wheezy)"

Current Directory

$ pwd

User files:

$ cd /home/backupUser/

Change to your user’s dir:

$ cd

Web files:

$ cd /var/www

Disk free space

$ df
/dev/mmcblk0p1 57288 20344 36944 36% /boot percent full
/dev/sda1 1465136000 1125819892 339316108 77% /media/1_5TBExt

Drive mounts:

$ cd /media/


$ sudo shutdown -r

Shut down now

$ sudo shutdown -h now

List users:

$ cat /etc/passwd

To modify the username of a user:

$ usermod -l new_username old_username

To change the password for a user:

$ sudo passwd username

Add user

sudo adduser new_username

Delete user

sudo userdel username

Delete the home directory of the user:

sudo rm -r /home/username

List drives


One thought on “Raspberry Pi (Model B) NAS and Backup

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 )

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