Home > System Tutorial > LINUX > How to build a network storage device using Raspberry Pi

How to build a network storage device using Raspberry Pi

PHPz
Release: 2023-12-31 11:53:56
forward
1227 people have browsed it
Introduction Do you have an idle mobile hard drive and Raspberry Pi? You can use these to build a cheap and low-power NAS. The end result is definitely not as good as a $500 NAS, such as those made by Synology DiskStation. But it's possible to build a low-power network storage platform - and especially for those who are tired of the National Security Agency's data surveillance, you can hide your data in this "little attic".

How to build a network storage device using Raspberry Pi
You need a Raspberry Pi and one or two free external hard drives. The smaller 2.5mm hard drive can be powered directly by the USB interface, but we need a USB splitter with power supply function because the power provided by the Raspberry Pi's USB interface is not enough to power the mobile hard drive. Alternatively you can use a thumbdrive or even an SD card. In fact, what I am using now is built with a USB port mobile hard disk and a thumbdrive, and the operation process is the same.
How to build a network storage device using Raspberry Pi
With only one hard drive, you can build a network storage for sharing files, but with two hard drives, you can set up data backup in case one of the hard drives fails.

Prepare hard disk

The initial step is to format the hard drive on your desktop computer. This is for convenience. If something goes wrong, we can unplug the hard drive from the NAS and read the data on it on a PC.

It can be formatted on the Raspberry Pi, but it will take a few hours. Formatting from the desktop is much faster. Do it now.

Set up SSH, enable the root user, and first set a password for the root user:

sudo -i
passwd root
Copy after login

(Enter your password)

Then run the raspi-config script on the command line. You can use the sudo command, or you can log out of the current user, and then log in as the root user. In the advanced options menu, turn on SSH.
How to build a network storage device using Raspberry Pi
After restarting, you should be able to log in from another machine on the Internet (if you are on Windows, you can use Putty):

SSH root@[IP地址]
Copy after login

After logging in, find out what the number of the connected hard disk is under /dev. The following assumes that two hard drives are used to create a data backup. enter

fdisk -l
Copy after login

The mounted storage devices will be displayed. You should see a screen similar to this:
How to build a network storage device using Raspberry Pi
/dev/mmc is the partition of the Raspberry Pi system, and mmc refers to the SD card. /dev/sda1 and /dev/sdb1 have nothing to do with SD cards. These are USB hard disks that you plug in (originally meant to be SCSI drives, but now used to refer to any SATA port or other storage)

Install the ntfs-3g module, so we can read and write NTFS formatted hard drives.

apt-get install ntfs-3g
Copy after login

Then create a directory and use this directory as a mount point to mount the hard disk. For simplicity, we will refer to them as 1 and 2 below.

mkdir /media/1
mkdir /media/2
mount -t auto /dev/sda1 /media/1
mount -t auto /dev/sdb1 /media/2
mkdir /media/1/shares
mkdir /media/2/shares
Copy after login
Samba

Next we set up Samba. Samba is a network file sharing protocol used by Windows (in fact, the latest OSX Mavericks also supports it)

apt-get install samba
apt-get install samba-common-bin
cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
nano /etc/samba/smb.conf
Copy after login

If you are not familiar with this type of configuration file, the # at the beginning of the line indicates that this line is a comment, so the configuration of this line will not take effect. To enable a function, you can add a line yourself, or you can remove the comment symbol in front of a certain line to make it take effect.

Let's start user security first; press CTRL-W and enter "security" to find the relevant options. Remove the comment symbol in front of this line:

security = user
Copy after login

Then, move to the end of the text (or hold CTRL V until you reach the end) and add network shares, the number depends on your own needs. Use this format:

[test] comment = Test share
path = /media/1/shares
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no
Copy after login

Only the configuration of the first hard drive - we will use the second hard drive to synchronize with the first one later for backup.

How to build a network storage device using Raspberry Pi

Then restart Samba with the following command.

service samba restart
Copy after login

Now add a user to your Raspberry Pi, assuming you don’t want to use the same user to access ("jamie" below is the username)

useradd jamie -m -G users
Copy after login

Enter the following command to set a password for the new user, and confirm after completion.

passwd jamie
Copy after login

Then we add this user to Samba. You need to confirm your password twice.

smbpasswd -a jamie
Copy after login

Then test the network storage function. You should be able to see it on other machines (Windows or Mac), and you should be able to write files to it.

How to build a network storage device using Raspberry Pi

There is still one problem. If you restart the Raspberry Pi, the drive mount will become invalid. To solve this problem, we need to install autofs.

apt-get install autofs
nano /etc/auto.master
Copy after login

Add a line below auto.master:

/media/ /etc/auto.ext-usb –timeout=10,defaults,user,exec,uid=1000
Copy after login

You can now restart safely without damaging the configuration.

data backup

If you have installed two hard drives, we can now use a script to synchronize data. If one hard drive fails, there will be a backup. We use rsync for backup.

apt-get install rsync
crontab -e
Copy after login

The crontab under Linux is a software used to automatically run a certain task. I briefly mentioned it in "Automatically Back Up Your Website". Add this line:

30 5 * * * rsync -av –delete /media/1/shares /media/2/shares/
Copy after login

The format of numbers is like this:
Minutes | Hours | Day of the month | Month | Day of the week

所以我们加进去的那一行,rsync命令会在每天的早晨5点30分运行(*通配符表示“所有”,所以会在“每个月的每一天”运行)

如果你想立刻进行一次备份,只要粘贴这个rsync的命令:

rsync -av –delete /media/1/shares /media/2/shares/
Copy after login

取决于共享文件夹里放的东西,耗费的时间可能只要几秒钟,也可能会长一些,在完成之后会给你一个报告。rsync最棒的地方在于它会记录哪些文件更新了,哪些文件添加近来或是应该被删除。你可以再试一遍那个命令,应该立刻就会完成的,因为rsync知道没有文件发生了变动。

教程到这里就结束了——现在你搭建起了自己的NAS,虽然细节上有待改进,但是现在就可以用起来了。虽然没有市面上出售的NAS那些华丽的特性,但是它也能漂亮地完成任务,而且耗电会少很多。

有问题请在评论区提出,我会尽力帮忙的,但是请确定你在使用最新的树莓派系统镜像。

The above is the detailed content of How to build a network storage device using Raspberry Pi. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template