What you will need:
- 1x [Only registered and activated users can see links. ]
- 2x [Only registered and activated users can see links. ]
- 1x USB Powered Hub – [Only registered and activated users can see links. ] / [Only registered and activated users can see links. ] / [Only registered and activated users can see links. ]
We will assume you have Rasbian installed, and are fully up-to-date.
- So first thing to do is get all the hardware plugged in and powered up
- Plug the powered USB hub into the Raspberry Pi
- Plug each of the external hard drives into the powered USB hub
- Hook up a network cable
- hdmi cable, keyboard and mouse if you’re not going to SSH to the Pi
- Plug the power into the USB hub and Pi, and power it all on!
- With everything powered on its time to start configuring your pi. We will be doing this all via the command line, so either SSH to your Raspberry Pi, or open up a LXTerminal if you have booted to desktop on Rasbian.
- Before we can mount and use our external hard drives, we need to add support for our NTFS formatted hard drives, to do this execute this command:
sudo apt-get install ntfs-3g
- Now we need to check to see if we can see the unmounted partitions from our external hard drives, we do this by running the following command:
sudo fdisk -l
If everything is working you should see something along these lines (looking for number 1,2,3,4 labelled in the image.)
Number 1 – Shows us the first external hard drive
Number 2 – Shows us the first partition on the first hard drive
Number 3 – Shows us the second hard drive
Number 4 – Shows us the first partition on the second hard drive
If you have made multiple partitions on either of the hard drives, you should see extra lines on section 2 and/or 4
Take note of the “device” path for each partition, so in this example we need to remember:
- Before we can mount the partitions, we need to create a directory for each hard drive to mount them to, we are going to create two directories, one called “main” one called “backup” you can name them whatever you like.
sudo mkdir /media/main
sudo mkdir /media/backup
- Now we have our directories created, we can mount each of the hard drive partitions to these directories (if your “device” path is different remember to change it in the command, refer to your notes back on step 4):
sudo mount -t auto /dev/sda1 /media/main
sudo mount -t auto /dev/sdb1 /media/backup
Now we have our hard drives mounted!
- It’s now time to install Samba (this is what we’ll be using to share the hard drives on the network) to do this, run the command:
sudo apt-get install samba samba-common-bin
- Now we have Samba installed, we need to edit the configuration file. Before we do this, we will make a backup of it, just in case we need to revert back to a working state.
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
- With the configuration file backed up, we can now edit it
sudo nano /etc/samba/smb.conf
- First thing to check is the workgroup. The default workgroup is set to “WORKGROUP” which will work if you haven’t changed your windows workgroup. If you have changed it, simply update it here
- Now we need to enable user authentication for our samba shares, otherwise, anyone that has access to the network will be able to view the shares. Simply uncomment (remove the #) from the line that reads “# security = user”
- Now we need to add our “main” partition as a share. To do this we need to add an entirely new block of code. So scroll to the very bottom of the config file, and add the following (if you named your directory different to “main” back in step 5, then you will need to update the line “path = “):
comment = Main Folder
path = /media/main/
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no
Here’s a breakdown of each line in the above block of code:
[Main] – Anything between the square brackets, in this case “Main”, is the name of the share displayed on the network.
comment – This is just a description of the share
path – This is the directory that is used for this share
valid users – This specifies which users can access the share, in our case we have specified a group (by using the @ symbol followed by the group name)
force group – This will make sure any files added via the samba share will have the specified group
create mask – This sets the permission of any files added to the samba share
directory mask – This sets the permission of any folders added to the samba share
read only – This makes sure that the files are writable
- With the samba configuration done, we need to restart the samba server so that these new settings take effect.
sudo /etc/init.d/samba restart
- Time to create a user that will have access to this share. The following commands will create a user and add it to the group “users” (as specified in our samba config) on your pi and allow you to set a password. We will create a user called “backup” and give it a password. You will be asked to enter the password twice.
sudo useradd backup -m -G users
sudo passwd backup
- Now we need to add the user “backup” as a Samba user, do this with the following command:
sudo smbpasswd -a backup
You should now have a working Samba share. To test this jump onto a Windows computer and browse the network, you should see a server called “RASPBERRYPI” If you double click the server, you should then see a folder called “Main” (as specified in step 12, between the square brackets [Main])
If you double click on the “Main” folder, you should be prompted for a username and password. Simply enter the username “backup” and the password you used back on step 14.
Viloa! You have successfully accessed the samba share. You should be able to add/remove files.
- Now, if our Pi were to restart, we would lose the samba share because the external hard drives won’t re-mount. To fix this we need to tell our Pi to mount the external hard drives on boot. To do this, edit the following file:
sudo nano /etc/fstab
And the add the following lines (notice that the /media/<folder name> are the same for each mount as previously stated on step 6):
/dev/sda1 /media/main auto noatime 0 0
/dev/sda2 /media/backup auto noatime 0 0
- Now if our Pi reboots, the external hard drives will be automatically mounted and our Samba share will continue to work!
- Now it’s time to setup some redundancy. We are going to use rsync to backup all our files on our “main” hard drive over to our “backup” hard drive. First we need to install the rsync package:
sudo apt-get install rsync
- With rsync installed, we are going to setup a cron job to automatically do the backup for us. Start by entering the following command to open up the cron scheduling table:
- Then at the very bottom of the document, add the following line:
0 5 * * * rsync -av --delete /media/main /media/backup
This will run the backup every day at 5am and deleting anything on the backup drive which is no longer on the main drive.
- That’s it! You now have a PiNAS setup that’ll mirror your files to a backup drive.