An always-on network backup device accessible to a computer from both outside and inside the network is a highly desirable thing. As the system will always be on, power consumption is an important consideration. These requirements are fairly met by this Raspberry Pi-based network storage system as it consumes less power than its off-the-shelf counterparts.
Presented here is a system that uses two USB hard drives plugged into Raspberry Pi to store data. One of the hard drive stores data directly and the other is used to back up the data.
For a simple network, one drive is sufficient but it is recommended to use two for local redundancy. External hard drives should be low-power and have sufficient capacity for future addition of data. In this case, two NTFS-formatted hard drives are used and samba software helps to implement network sharing. Follow the steps mentioned below in the exact sequence to set up and configure the network storage system with Raspberry Pi.
Mounting the external hard disk
Connect the hard drives to the Raspberry Pi and power it up. It is a good idea to use a USB hub with separate power supply. The Raspberry Pi should have a working operating system (Raspbian) configured suitably for remote access to follow all the steps. You can refer ‘Remote Access to Raspberry Pi’ published in June 2013 issue.
Now access Raspberry Pi using SSH and run following commands to add the support for NTFS-formatted drives to Raspbian operating system as shown in Fig. 1:
$ sudo apt-get
It will take a few minutes for the package to download, unpack and install. Once the NTFS package is installed, look for the un-mounted partitions of the attached external hard drives by executing the following command as shown in Fig. 2:
$ sudo fdisk –l
If only one external hard drive is connected, you will see two disks—one external hard disk and one SD card inside Raspberry Pi. In case two external hard drives are connected, you will see three disks. Carefully make note of the hard drives’ names.
Before mounting the drives, we need to create a directory to mount the drives to. For the sake of simplicity, let us create a directory called HDD1 and HDD2 for each drive. To create these directories, write the commands mentioned below and shown in Fig. 3:
$ sudo mkdir /media/HDD1
$ sudo mkdir /media/HDD2
you can mount the drives to each location by executing the following commands:
$ sudo mount -t auto /dev/sda1
$ sudo mount -t auto /dev/sdb1
‘sda1’ and ‘sdb1’are names of the external drives; replace them with the ones you get as shown in Fig. 2.
Now we have both the drives mounted; we just need to create a folder in both directories to hold all the shared folders. refer Fig. 4 and write the commands:
$ sudo mkdir /media/HDD1/shares
$ sudo mkdir /media/HDD2/shares
Setting up the network
Install samba for accessing the storage from anywhere on the network using the command below as shown in Fig. 5:
$ sudo apt-get install samba
Once installation is complete, we need to make some changes in samba’s configuration file. But before that, create back up of the configuration file (in case we make any mistake) by executing the command below as shown in Fig. 6:
$ sudo cp /etc/samba/smb.conf /etc/
This will create a backup of the configuration file with the name ‘smb.conf.old’ in the same directory.
Now to edit the samba config file, open it in nano editor by executing the command below as shown in Fig. 7:
$ sudo nano /etc/samba/smb.conf
The first entry in this file is workgroup identifier; by default it is ‘workgroup = WORKGROUP.’ If you are using a different name for your home workgroup, go ahead and change that now; otherwise leave it as the default.
Under authentication section, add following line or remove the ‘#’ symbol if it is already there. otherwise, anyone with network access will be able to access the shared data. This will enable username and password verification.
security = user
Now scroll down to the bottom of the file and add following commands as shown in Fig. 8:
comment = Backup Folder
path = /media/HDD1/shares
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no
After adding this, press ‘CTRL+O’ to save the file and ‘CTRL+X’ to exit.
Restart samba as shown in Fig. 9 with the command below:
$ sudo /etc/init.d/samba restart
Add in a user who can access Raspberry Pi’s samba with the commands below. In this case, user is ‘user1’ and password is ‘password123.’ You can choose any username and password.
$ sudo useradd
user1 –m -G users
$ sudo passwd
Reconfirm the password when prompted. After confirming the password, it is time to add ‘user1’ as a legitimate samba user. Enter the following command:
Enter the password when prompted as shown in Fig. 10.
You can now use any samba-capable machine on network and test connectivity to the network using the username and password created earlier.
Configuring Pi for automatic mounting of the drives
We need to configure our Raspberry Pi such that when it restarts, it automatically mounts the external hard drives. To do so, we need to start the nano editor and make following changes:
sudo nano /etc/fstab
And add the following lines in the file as shown in Fig. 11:
/dev/sda1 /media/HDD1 auto noatime
/dev/sdb2 /media/HDD2 auto noatime
After adding these lines, press ‘CTRL+O’ to save the file and ‘CTRL+X’ to exit.
Configuring Raspberry Pi for data redundancy
To configure Raspberry Pi to perform data mirroring from the share folder of the primary disk to the share folder of the secondary disk, two tools have to be used—‘cron’ and ‘rsync.’
Install rsync using the command:
$ sudo apt-get install rsync
Once rsync is installed, set up cron to automate the process of copying files from HDD1 to HDD2. Run the command:
$ crontab -e
This will open up cron scheduling table. Add following line at the end of the file:
0 5 * * * rsync -av ––delete /media/
In section 0 5 * * *, first two digits define time and the three stars are for place of year, month and day as wild card entries.
Thus each day at 5:00 am, rsync will compare the share folders of the primary and secondary drives and copy all new files to secondary drive. It will also delete those from the secondary drive which are no longer present in the primary drive. Now save and exit the editor.
The author is working as an assistant manager at Samtel Avionics Ltd.