2014-3-10 08:24:43 [显示全部楼层]
3776浏览
查看: 3776|回复: 0

用树莓派构建低能耗网络存储设备

[复制链接]
本文由 极客范 - SarahCla 翻译自 Jason Fitzpatrick。欢迎加入极客翻译小组,同我们一道翻译与分享。转载请参见文章末尾处的要求。

把一个树莓派和一些廉价的外设结合起来,你就可以拥有一个非常低耗并且永远在线的网络存储设备。继续看下文,告诉你如何构建树莓派NAS。

为何我们要这样做?

拥有一个在线网络存储,可以让你方便的在内外网存取数据。当然,也可用它作为备份设备。但是,享受方便的同时,你也得为其消耗的巨大电量买单。例如,我们的办公服务器,运行24小时*7天,一年大约就会消耗200美元电量。而一个树莓派网络存储设备,每年大约只会消耗5美元。这么一个有更多存储空间,更强处理能力(例如:可以在合理时间内转码一个多TB的视频)的成熟的网络服务器方案,也许我们是一个给你提供的吧。当然,对大多数人来说,一般也只会把在线电脑作为文件服务器和备份仓库来使用。这些任务,对于树莓派来说,毫无压力,再说,它还能给你节约不少开销。

需要的材料?

这个教程基于我们前面的教程,假设你已经完成了前面的章节,也就是说,你已经有了块树莓派,成功启动,挂载了鼠标和键盘,安装好了Raspbian系统。
除了这些基础设施,你还需要跟着教程从头开始做,好了,准备下面的硬件:

  • 方案一:一个(至少一个)USB硬盘,用来做简单的网络备份和文件服务;
  • 方案二:两个(至少两个)USB硬盘,用来做本地数据冗余。

如果你只是想要一个简单的网络存取设备,一个硬盘就够了。我们非常推荐使用至少两个硬盘,以便能做本地数据冗余。为了配合本篇教程,我们使用一对儿希捷Backup Plus 1TB便携式外置硬盘。它们个头娇小,不需要外接电源,且容易买到。

你可以用现有的硬盘,不过,如果有可能的话,有小的低功耗的硬盘当然更好,因为本篇讲述的就是如何构建一个小型的低功耗的NAS,你当然得配合一下不是?所以,忘了现有的普通硬盘吧,把前面那句掐了,别播。

在继续之前,温馨小提示,如何配置树莓派,当前有多种设计选择。大部分的用户可能会选择严格按照我们的教程来做,当然,你也可以根据你的需求进行调整。

首先,我们使用 NTFS 格式的硬盘。假设树莓派NAS由于某些原因挂了,我们想通过USB 3.0 接口而非网络接口快速的拷贝资料,有NTFS格式的硬盘,这事儿就相当简单了。把硬盘从NAS上取下来,直接接到我们使用的电脑上,搞定。

第二,我们使用Samba作网络共享,目的仍然是追求方便,Samb可以方便的让树莓派与我们常用的windows网络环境相契合。

准备及挂载外接硬盘

一旦你攒齐了硬件,先按照树莓派上手教程速度做好(跑上Raspian),现在开始我们的树莓派NAS之旅。

第一步,把硬盘挂载到树莓派上(选配的USB集线器、外接电源等)。硬盘挂接好,树莓派上电,我们可以开始工作了。

注意:我们用了两块硬盘。如果你决定只使用一块盘,那么就直接忽略本节中对第二快盘的处理部分。

我们会直接在终端界面上执行所有的操作。当然,你也可以用Raspian系统中的LXTerminal或者用类似putty的工具通过SSH连接树莓派,这些方式都可以。

进入命令行,首先需要让Rasbian系统支持NTFS格式的磁盘,执行下面的语句:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo apt[color=rgb(0, 111, 224) !important]-[color=teal !important]get [color=teal !important]install ntfs[color=rgb(0, 111, 224) !important]-[color=rgb(0, 153, 153) !important]3g



下载包,解包,安装,通常会用一到两分钟。一旦NTFS包安装完毕,我们就可以查找新挂上的硬盘的未挂载分区了。

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo fdisk[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-l



你至少会看到两个硬盘,如果你接了两个硬盘做数据镜像(和教程的环境一样),那么你至少能看到三个硬盘,就像下面这样:

第一个盘 /dev/mmcb1k0 是SD卡,它接在树莓派上,搭载了Rasbian 系统。这个盘我们就别去动它了。

第二个盘 /dev/sda 是我们的第一个 1TB 的外接硬盘。第三个盘,/dev/sdb 是我们接的另一块硬盘。我们关心的是它们的实际分区,分别是: /sda1/ 和 /sdb1/。请记下硬盘的名字。

我们需要生成一个目录来挂载硬盘。为了简便,我们分别为两块硬盘,创建名为 USBHDD1 及 USBHDD2 的两个目录。首先,命令行中执行下面的命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2


[color=teal !important]sudo mkdir[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/[color=teal !important]USBHDD1
[color=teal !important]sudo mkdir[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD2



创建完目录,接着挂载硬盘到目录。继续,命令行中执行如何命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2


[color=teal !important]sudo mount[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-t[color=rgb(0, 111, 224) !important] auto[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/dev[color=rgb(0, 111, 224) !important]/sda1[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/[color=teal !important]USBHDD1
[color=teal !important]sudo mount[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-t[color=rgb(0, 111, 224) !important] auto[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/dev[color=rgb(0, 111, 224) !important]/sdb1[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD2



这里我们必须让两个硬盘一个挂载到USBHDD1目录,另一个挂载到USBHDD2目录。下面会在两个硬盘中各自增加一个特殊目录,共享目录(为了保持事物的整洁,并隔离我们对两个硬盘的操作)。执行命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2


[color=teal !important]sudo mkdir[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD1[color=rgb(0, 111, 224) !important]/[color=teal !important]shares
[color=teal !important]sudo mkdir[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD2[color=rgb(0, 111, 224) !important]/shares



接下来,我们安装Samba,通过它就可以从网络上对硬盘进行存取了。执行命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo apt[color=rgb(0, 111, 224) !important]-[color=teal !important]get [color=teal !important]install [color=teal !important]samba samba[color=rgb(0, 111, 224) !important]-common[color=rgb(0, 111, 224) !important]-bin



当询问是否继续时,输入Y。这时你可以休息一下了,喝杯咖啡,等着它解压,安装。Samba安装完成,可以开始配置。记得先备份一下Samba的配置文件,避免人品不好改错了,无法恢复。命令行中,庄严的写下:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo cp[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/samba[color=rgb(0, 111, 224) !important]/[color=rgb(0, 45, 122) !important]smb[color=rgb(51, 51, 51) !important].conf[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/samba[color=rgb(0, 111, 224) !important]/[color=rgb(0, 45, 122) !important]smb[color=rgb(51, 51, 51) !important].[color=rgb(0, 45, 122) !important]conf[color=rgb(51, 51, 51) !important].old



这个命令会把配置文件备份成同目录下的文件 smb.conf.old。完成备份,可以开始编辑Samba配置文件。执行命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo nano[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/samba[color=rgb(0, 111, 224) !important]/[color=rgb(0, 45, 122) !important]smb[color=rgb(51, 51, 51) !important].conf



这个命令会用 nano 文本编辑器打开配置文件。如果这是你第一次使用 nano ,强烈建议你先看看 Linux 的命令行文本编辑器新手指南。你可能会在终端窗口中看到下面的场景:

Nano 完全是键盘控制,使用箭头键移动光标到你想要编辑的位置。按向下的箭头键,可以看到一些有用的注释或修改记录。

首先是工作组标识,默认 workgroup = WORKGROUP。如果你有不同的工作名称,定位并修改它,如果相同,就不管它了。

除非你允许任何人都可以自由存(就像允许 Wi-Fi 蹭网一样) ,否则我们就需要开启Samba存储的认证功能。向下翻页直到找到如下的部分:

删掉 security = user 行前面的 # 号 (先定位到 # 当他高亮的时候按 del 键) 这样就为 Samba 共享目录开启了用户/密码验证。

下面,我们要为配置文件增加一整个段落。翻页到文件尾,输入下面的文字:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2

[color=rgb(170, 170, 170) !important]3

[color=rgb(170, 170, 170) !important]4

[color=rgb(170, 170, 170) !important]5

[color=rgb(170, 170, 170) !important]6

[color=rgb(170, 170, 170) !important]7

[color=rgb(170, 170, 170) !important]8


[color=rgb(51, 51, 51) !important][Backup
[color=rgb(0, 45, 122) !important]comment[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=teal !important]Backup [color=teal !important]Folder
[color=rgb(0, 45, 122) !important]path[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD1[color=rgb(0, 111, 224) !important]/[color=teal !important]shares
[color=teal !important]valid [color=rgb(0, 45, 122) !important]users[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=rgb(51, 51, 51) !important]@[color=teal !important]users
[color=teal !important]force [color=rgb(0, 45, 122) !important]group[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=teal !important]users
[color=teal !important]create [color=rgb(0, 45, 122) !important]mask[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]0660
[color=teal !important]directory [color=rgb(0, 45, 122) !important]mask[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]0771
[color=teal !important]read [color=rgb(0, 45, 122) !important]only[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] no



注意:定义在第一行的方括号中的名字就会是网络共享目录的名字。如果你不喜欢这个名字“Backup”,那么现在就改掉它吧。

退出文档编辑,按 CTRL+X 。询问你是否保存修改并覆盖现有的配置文件时,按Y。退出到命令窗口,执行命令重启 Samba :

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


sudo[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/[color=rgb(0, 45, 122) !important]init[color=rgb(51, 51, 51) !important].d[color=rgb(0, 111, 224) !important]/[color=teal !important]samba restart



到这里,我们需要创建一个可以使用Samba共享目录的用户。我会创建名为backups的账号,密码为backups4ever。命令如下:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2


[color=teal !important]sudo [color=teal !important]useradd backups[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-m[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-G[color=rgb(0, 111, 224) !important] [color=teal !important]users
[color=teal !important]sudo [color=teal !important]passwd backups



命令会让你输入两次密码以确认。密码确认之后,需要把 “backups” 账号注册为 Samba 和合法用户。继续上命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo smbpasswd[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-a[color=rgb(0, 111, 224) !important] backups



提示输入密码时,需要输入backups账户的密码。因为已经把它注册为认证用户,这次就不用重启 Samba 守护进程了。现在,我们可以跳到任一台联网的Samba兼容机上测试共享目录了。

在一台Windows机器上,我们打开一个Windows文件浏览器,点击网络,确认主机名 RASPBERRYPI 在 WORKGROUPS 工作组中,点击共享目录 Backups:

弹出输入框时,输入你在上一步中创建的证书 (如果你是严格跟着教程一步步做下来的,那么用户名是 backups ,密码是 backups4ever).

一旦你的证书被接受,你会看到一个空的文件夹,因为我们还没有往里面放东西嘛。为了确认它工作良好,我们在里面创建一个简单的文件来测试它 (我们的环境是 Windows 7 桌面). 像这样创建一个文本文件:

现在,我们可以通过命令行方式来工作了。检查一下通过 Windows 桌面创建的文件是否确实已经在共享目录中成功创建。输入命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2


cd[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD1[color=rgb(0, 111, 224) !important]/[color=teal !important]shares
ls



可以看到 hello-is-it-me-you-are-looking-for.txt 确实在目录中了。祝贺,我们的共享目录创建成功!

在离开本教程小节之前,我们还有件事情要做。配置树莓派,让它在重启是可以自动挂载外接硬盘。还是在命令行中打开 nano 编辑文件:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo nano[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/fstab



这个操作会在 nano 中打开文件系统表,我们需要在里面增加一些入口,在 nano 中加入下面的行:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2


[color=rgb(0, 111, 224) !important]/dev[color=rgb(0, 111, 224) !important]/sda1[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/[color=teal !important]USBHDD1 [color=teal !important]auto noatime[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]0[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]0
[color=rgb(0, 111, 224) !important]/dev[color=rgb(0, 111, 224) !important]/sda2[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/[color=teal !important]USBHDD2 [color=teal !important]auto noatime[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]0[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]0



还是按 CTRL+X 退出, 按 Y 保存, 自动覆盖原来的文件。

如果你只用了一块硬盘,不做冗余,那么,好,你的超低功耗NAS就闪亮登场了。

为树莓派NAS配置简单的数据冗余

到目前为止,我们的树莓派NAS已经挂接到网络中了,文件传输也可以使用,但很明显,还有件事情我们忘了做。第二块盘已经配置了,但它还没有用上。

在这一小节中,我们会使用两个简单但强大的 Linux 工具,rsync 和 cron,用它们来配置树莓派NAS的数据镜像,从主盘的 /shares/ 文件夹到从盘的 /shares/ 。 这不是一个像 RAID 一样的实时的数据镜像,而是为了数据安全,增加的一层日常(或半日常)的数据备份流程。

首先,我们需要在 Rasbian 中安装 rsync。如果这是你第一次使用 rsync,建议你先了解一下它的命令,推荐看这篇

在命令行中输入:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo apt[color=rgb(0, 111, 224) !important]-[color=teal !important]get [color=teal !important]install rsync



安装好 rsync,接着创建一个 cron 工作,让它负责自动从 USBHDD1 向 USBHDD2 拷贝文件,输入命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


crontab[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-e



这个命令会在 nano 中打开 cron 的计划任务表,经过刚才的步骤,你应该已经对这个界面比较熟悉了。继续,翻到文档尾,输入下面的一行:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=rgb(0, 153, 153) !important]0[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]5[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]*[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]*[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]*[color=rgb(0, 111, 224) !important] rsync[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-av[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]--delete[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD1[color=rgb(0, 111, 224) !important]/shares[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD2[color=rgb(0, 111, 224) !important]/shares[color=rgb(0, 111, 224) !important]/



这个命令表明在早晨5:00(0 5 部分),每天(* * *,通配符代表 年,月,日 )我们希望能运行 rsync 去对比两个目录,拷贝 HDD1 中的所有文件到 HDD2 ,删除备份路径下存在的但主路径中已经没有了的文件。如果我们在 HDD1 中删除了一个电影文件,那么我们系统在下一个同步时,备份路径下的该文件也同样被删除。

配置这个命令需要注意的是,要选择好运行的时间,避免的网络繁忙的时候去执行。例如: 如果你把树莓派NAS作为备份机来使用,并且安装了自动化的软件会在每天凌晨5:00开始向NAS目录中拷贝文件,那么你就需要调整配置的执行时间,或者调整cron任务的执行时间。也就是说,你不要在远程备份的同时,又做本地的同步。

完成了 crontab 的配置,敲 CTRL+X 退出并保存。如果想要立刻运行 rsync ,减轻初始 cron 工作对系统的影响,那么直接运行下面的命令就可以了:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


rsync[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-av[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]--delete[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD1[color=rgb(0, 111, 224) !important]/shares[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/media[color=rgb(0, 111, 224) !important]/USBHDD2[color=rgb(0, 111, 224) !important]/shares[color=rgb(0, 111, 224) !important]/



好咯!现在你只需要过两天检查一下树莓派,验证计划任务按照预期执行了,即,在 /USBHDD1/shares/ 路径下的文件都应该出现在 /USBHDD2/shares/ 目录下。从现在开始,所有你放到树莓派NAS下的文件都会被按天自动镜像到另一个硬盘。

我们做的这些树莓派项目中,有你喜欢的吗?多多少少,我们都希望通过评论获知到你对派的想法。



原文链接: Jason Fitzpatrick 翻译: 极客范- SarahCla

译文链接: http://www.geekfan.net/5003/

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail