Friday, March 29, 2024

Network Storage with Raspberry Pi

Presented here is a system which 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. -- Pooja Juyal

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.

Fig. 1: Add NTFS support
Fig. 1: Add NTFS support

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:

- Advertisement -

$ sudo apt-get
install ntfs-3g

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:

Fig. 2: Partition of the external hard drives
Fig. 2: Partition of the external hard drives

$ sudo fdisk –l

- Advertisement -

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:

Fig. 3: Make the directories to mount the drives
Fig. 3: Make the directories to mount the drives

$ 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
/media/HDD1
$ sudo mount -t auto /dev/sdb1
/media/HDD2

‘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:

Fig. 4: Create the shared folder
Fig. 4: Create the shared folder

$ 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:

Fig. 5: Installing samba
Fig. 5: Installing samba

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

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:

Fig. 6: Backup of the configuration file
Fig. 6: Backup of the configuration file

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

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:

Fig. 7: Edit the configuration file using nano editor
Fig. 7: Edit the configuration file using nano editor

$ 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:

Fig. 8: Adding the lines to configuration file
Fig. 8: Adding the lines to configuration file

[Backup]
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:

Fig. 9: Restart samba
Fig. 9: Restart samba

$ 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
password123

Reconfirm the password when prompted. After confirming the password, it is time to add ‘user1’ as a legitimate samba user. Enter the following command:

sudo smbpasswd
-a user1

Enter the password when prompted as shown in Fig. 10.

Fig. 10: Add user1 as legitimate samba user
Fig. 10: Add user1 as legitimate samba user

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:

Fig. 11: Configuration for automatic mounting of drives
Fig. 11: Configuration for automatic mounting of drives

/dev/sda1 /media/HDD1 auto noatime
0 0
/dev/sdb2 /media/HDD2 auto noatime
0 0

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:

Fig. 12: Installing the rsync
Fig. 12: Installing the rsync

$ 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:

Fig. 13: For automatic process of copying files
Fig. 13: For automatic process of copying files

$ crontab -e

This will open up cron scheduling table. Add following line at the end of the file:

0 5 * * * rsync -av ––delete /media/
HDD1/shares /media/HDD2/shares/

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.

To read other interesting Electronics Projects: click here

This Project was first published on 29 December 2014 and was updated on 20 May 2020.

SHARE YOUR THOUGHTS & COMMENTS

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Calculators