Skip to content

Commit bf5a9f1

Browse files
committed
add translated for 'Backup Photos While Traveling With an Ipad Pro and a Raspberry Pi'
1 parent e7a84f4 commit bf5a9f1

3 files changed

Lines changed: 395 additions & 634 deletions

File tree

‎Hardware/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121

2222
无人机盛行的当下,想看看如何用node.js和python对其进行编程,做一些好玩的事情吗?看看作者如何玩玩这一big toy吧!(搞得我也想玩了,但是无人机还是有点贵呢……)
2323

24+
- [旅程中带着Ipad Pro和Raspberry Pi备份照片](./旅程中带着Ipad Pro和Raspberry Pi备份照片.md)
25+
26+
很久以来,Lenin都一直在寻求找到一个理想的旅行照片备份解决方法。然后他发现,Ipad Pro+Raspberry Pi是个不错的方案。
Lines changed: 392 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,392 @@
1+
原文:[Backup Photos While Traveling With an Ipad Pro and a Raspberry Pi](http://www.movingelectrons.net/blog/2016/06/26/backup-photos-while-traveling-with-a-raspberry-pi.html "Permalink to Backup Photos While Traveling
2+
With an Ipad Pro and a Raspberry Pi" )
3+
4+
---
5+
6+
![旅行照片备份装置](http://www.movingelectrons.net/images/bkup_photos_main.jpg)
7+
8+
旅行时备份照片 —— 装置。
9+
10+
## 介绍
11+
12+
很久以来,我都一直在寻求找到一个理想的旅行照片备份解决方法。旅行中,当SD卡满了的时候,仅仅只是将它们扔到你的相机包里,是非常危险的,这让你太暴露了:SD卡可能会丢失或被偷,传输中数据可能会损坏,或者SD卡也可能坏掉。备份到另一个介质上 —— 即使只是另一张SD卡 —— 然后在旅行中将其放在一个(更)安全的地方,是一种最佳实践。理想情况下,备份到远程位置是上上之策,但取决于你旅行的地方以及该地的网络可用性,这可能是不实际的。
13+
14+
对于这个理想的备份过程,我的需求是:
15+
16+
1. 使用一个iPad而不是一台笔记本电脑来管理该过程。我喜欢轻旅行,由于我大多数的旅程都是商旅(即不涉及到照相),我讨厌带着我的商务笔记本的同时还带着个人笔记本。然而,我总是带着iPad,因此将它用作工具理所因当。
17+
2. 实际使用越少硬件设备越好。
18+
3. 设备间的连接应该是**安全的**。我将在酒店或机场使用这个装置,因此,设备间不公开且加密的连接是理想的。
19+
4. 整个过程应该是坚固且**可靠**。我尝试过其他选项,使用路由器/组合设备,但是[不尽人意](http://bit.ly/1MVVtZi)。
20+
21+
## 安装程序
22+
23+
我想到了一个装置,它满足上面的要求,并且未来也有足够的灵活性可供扩展。它涉及一下装置的使用:
24+
25+
1. [iPad Pro 9.7](http://www.amazon.com/dp/B01D3NZIMA/?tag=movinelect0e-20)英寸。这是截止到写这篇文章的时候最强大、体积最小并且最轻量的iOS设备。**Apple pencil**可有可无,但它是我的装置的一部分,因为路上,我还会在iPad Pro上写点东西。所有繁重的工作将由Raspberry Pi完成,因此任何其他能够通过SSH连接的设备都符合需求。
26+
2. [Raspberry Pi 3](http://www.amazon.com/dp/B01CD5VC92/?tag=movinelect0e-20),上面装了Raspbian系统。
27+
3. 用于Raspberry Pi[Micro SD](http://www.amazon.com/dp/B010Q57T02/?tag=movinelect0e-20)卡,以及一个Raspberry Pi [盒子](http://www.amazon.com/dp/B01F1PSFY6/?tag=movinelect0e-20)。
28+
4. [128 GB Pen Drive](http://amzn.to/293kPqX)。你可以使用更大容量的,但是128 GB对我来说够用了。你还可以使用一个便携式外置硬盘,例如[这个](http://amzn.to/290syFY),但是Raspberry Pi通过其USB端口可能没法提供足够的功率,这意味着你不得不再用一个[供电的USB集线器](http://amzn.to/290syFY),并带上所需的电缆,这与使用一个轻便简约的设置的初衷相悖。
29+
5. [SD卡读卡器](http://amzn.to/290syFY)
30+
6. [SD卡](http://amzn.to/290syFY)。我用好几个,因为我不会在一个满了之后才用另一个。这让我在多个卡之间都放上单个旅程中的照片。
31+
32+
下图显示了这些设备将如何相互交互。
33+
34+
![旅行照片备份装置过程图](http://www.movingelectrons.net/images/bkup_photos_diag.jpg)
35+
36+
旅行时备份照片 - 过程图
37+
38+
Raspberry Pi将会配置为一个**安全热点**。它将创建自己的WPA2-encrypted WiFi网络,以供iPad Pro连接。虽然网上有很多教程教你用Raspberry Pi创建一个Ad Hoc (即,计算机到计算机)连接,这会更容易建立;但是那种连接是未加密的,你旁边的其他设备很容易就可以连接到它。因此,我决定使用WiFi选项。
39+
40+
通过SD卡读卡器,相机的SD卡将会连到Raspberry Pi的一个USB口上。此外,高容量的Pen Drive (我用了128 GB)将会永久插到Raspberry Pi的一个USB口上。我选择了[Sandisk Ultra Fit](http://amzn.to/293kPqX),因为它小。**主要想法是让Raspberry Pi在一个Python脚本的帮助下,备份SD卡的照片到Pen Drive**。备份过程是增量的,意味着每次该脚本运行的时候,只有改动 (即,拍了新的照片)才会被添加到备份文件夹,这让整个过程相当快。如果你拍了很多照片,或者以_RAW_格式拍摄,这将是一个巨大的优势。iPad将会用来触发该Python脚本,并且在需要的时候用来浏览SD卡和Pen Drive。
41+
42+
有个福利,如果Raspberry Pi通过有线连接(即通过以太网端口)连接到因特网,那么**它将能够与连接到它自己的WiFi网络上的设备共享因特网连接**。
43+
44+
## 1\. Raspberry Pi配置
45+
46+
这是该我们卷起袖子开始忙起来的部分了,我们将使用Raspbian的_命令行接口_ (CLI)。我会试着尽可能清晰,以使得整个过程变得容易。
47+
48+
49+
### 安装和配置Raspbian系统
50+
51+
连接键盘、鼠标和一个LCD显示器到Raspberry Pi。将微型SD插入到Raspberry Pi的插槽,并继续按照[官网](https://www.raspberrypi.org/downloads/noobs/)上的说明安装Raspbian。
52+
53+
安装完成后,进入CLI(Raspbian中的终端),并输入:
54+
55+
```python
56+
57+
sudo apt-get update
58+
sudo apt-get upgrade
59+
60+
```
61+
62+
这将升级树莓派上的所有软件。我配置了Raspberry Pi,使其连接到本地网络,并为了安全起见,修改了默认密码。
63+
64+
默认情况下,Raspbian上启用了SSH,所以下面所有部分都是在远程计算机上完成的。我还配置了RSA身份验证,但那是可选的。[这里](https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md)你可以看到更多有关信息。
65+
66+
这里是Mac上的[iTerm](https://www.iterm2.com)到Raspberry Pi的SSH连接的截图:
67+
68+
### 创建加密(WPA2)访问点
69+
70+
基于[这篇](https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/)文章,我进行了安装,并根据实际情况对其进行了优化。
71+
72+
**1\. 安装包**
73+
74+
我们需要输入以下命令以安装所需的包:
75+
76+
```python
77+
78+
sudo apt-get install hostapd
79+
sudo apt-get install dnsmasq
80+
81+
```
82+
83+
**hostapd**允许使用内置的WiFi作为接入点。**dnsmasq**是一个易于配置的DHCP和DNS服务器的组合。
84+
85+
**2\. 编辑dhcpcd.conf**
86+
87+
通过以太网连接到Raspberry Pi。Raspbery Pi上的接口配置由`dhcpcd`来处理,因此首先,我们让它忽略`wlan0`,因为将为它配置一个静态IP地址。
88+
89+
使用`sudo nano /etc/dhcpcd.conf`打开一个dhcpcd配置文件,然后添加以下行到文件底部:
90+
91+
`denyinterfaces wlan0`
92+
93+
**注意**:这必须在已经添加的任何接口行**之上**。
94+
95+
**3\. 编辑接口**
96+
97+
现在,我们需要配置静态IP。使用`sudo nano /etc/network/interfaces`打开接口配置文件,编辑`wlan0`部分,如下所示:
98+
99+
```python
100+
101+
allow-hotplug wlan0
102+
iface wlan0 inet static
103+
address 192.168.1.1
104+
netmask 255.255.255.0
105+
network 192.168.1.0
106+
broadcast 192.168.1.255
107+
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
108+
109+
```
110+
111+
此外,编辑`wlan1`部分:
112+
113+
```python
114+
115+
#allow-hotplug wlan1
116+
#iface wlan1 inet manual
117+
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
118+
119+
```
120+
121+
**重要提示:** 使用`sudo service dhcpcd restart`重启`dhcpcd`,然后使用`sudo ifdown eth0; sudo ifup wlan0`重新加载`wlan0`的配置。
122+
123+
**4\. 配置hostapd**
124+
125+
接下来,我们需要配置`hostapd`。使用`sudo nano /etc/hostapd/hostapd.conf`创建一个新的配置文件,并添加以下内容:
126+
127+
```python
128+
129+
interface=wlan0
130+
131+
# Use the nl80211 driver with the brcmfmac driver
132+
driver=nl80211
133+
134+
# This is the name of the network
135+
ssid=YOUR_NETWORK_NAME_HERE
136+
137+
# Use the 2.4GHz band
138+
hw_mode=g
139+
140+
# Use channel 6
141+
channel=6
142+
143+
# Enable 802.11n
144+
ieee80211n=1
145+
146+
# Enable QoS Support
147+
wmm_enabled=1
148+
149+
# Enable 40MHz channels with 20ns guard interval
150+
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
151+
152+
# Accept all MAC addresses
153+
macaddr_acl=0
154+
155+
# Use WPA authentication
156+
auth_algs=1
157+
158+
# Require clients to know the network name
159+
ignore_broadcast_ssid=0
160+
161+
# Use WPA2
162+
wpa=2
163+
164+
# Use a pre-shared key
165+
wpa_key_mgmt=WPA-PSK
166+
167+
# The network passphrase
168+
wpa_passphrase=YOUR_NEW_WIFI_PASSWORD_HERE
169+
170+
# Use AES, instead of TKIP
171+
rsn_pairwise=CCMP
172+
173+
```
174+
175+
现在,我们还需要告诉`hostapd`,开机启动时应该到哪里寻找配置文件。通过`sudo nano /etc/default/hostapd`打开默认的配置文件,找到`#DAEMON_CONF=""`一行,然后将其改为`DAEMON_CONF="/etc/hostapd/hostapd.conf"`。
176+
177+
**5\. 配置dnsmasq**
178+
179+
自带的dnsmasq配置文件涵盖大量的关于如果使用它的信息,但是我们无需使用所有的选秀。我推荐将其重命名(而不是删掉它),然后创建一个新的配置文件:
180+
181+
```python
182+
183+
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
184+
sudo nano /etc/dnsmasq.conf
185+
186+
```
187+
188+
将以下内容粘贴到新的文件中:
189+
190+
```python
191+
192+
interface=wlan0 # Use interface wlan0
193+
listen-address=192.168.1.1 # Explicitly specify the address to listen on
194+
bind-interfaces # Bind to the interface to make sure we aren't sending things elsewhere
195+
server=8.8.8.8 # Forward DNS requests to Google DNS
196+
domain-needed # Don't forward short names
197+
bogus-priv # Never forward addresses in the non-routed address spaces.
198+
dhcp-range=192.168.1.50,192.168.1.100,12h # Assign IP addresses in that range with a 12 hour lease time
199+
200+
```
201+
202+
**6\. 设置IPV4转发**
203+
204+
我们需要做的最后一件事是启用包转发。通过`sudo nano /etc/sysctl.conf`打开sysctl.conf文件,然后移除包含`net.ipv4.ip_forward=1`的行的行首的#。这在下次重启时将会启用它。
205+
206+
我们还需要通过在`wlan0`接口和`eth0`接口之间配置NAT,来共享Raspberry Pi的互联网连接,以使得我们的设备可以通过WiFi进行连接。我们可以编写以下脚本来做到这点。
207+
208+
```python
209+
210+
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
211+
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
212+
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
213+
214+
```
215+
216+
我将该脚本命名为`hotspot-boot.sh`,并让它可执行:
217+
218+
`sudo chmod 755 hotspot-boot.sh`
219+
220+
该脚本应该在Raspberry Pi启动的时候执行。有很多种方式可以来做到,而下面是我选择的方式:
221+
222+
1. 将该文件放到`/home/pi/scripts`。
223+
2. 输入`sudo nano /etc/rc.local`以编辑`rc.local`文件,然后**在`exit 0`行之前**放置脚本调用 (详情点击[这里](https://www.raspberrypi.org/documentation/linux/usage/rc-local.md))。
224+
225+
这是编辑后,`rc.local`文件的样子。
226+
227+
```python
228+
229+
#!/bin/sh -e
230+
#
231+
# rc.local
232+
#
233+
# This script is executed at the end of each multiuser runlevel.
234+
# Make sure that the script will "exit 0" on success or any other
235+
# value on error.
236+
#
237+
# In order to enable or disable this script just change the execution
238+
# bits.
239+
#
240+
# By default this script does nothing.
241+
242+
# Print the IP address
243+
_IP=$(hostname -I) || true
244+
if [ "$_IP" ]; then
245+
printf "My IP address is %s\n" "$_IP"
246+
fi
247+
248+
sudo /home/pi/scripts/hotspot-boot.sh &
249+
250+
exit 0
251+
252+
```
253+
254+
### 安装Samba和NTFS兼容性
255+
256+
我们还需要安装下面的包以启用Samba协议,并允许[文件浏览器](https://itunes.apple.com/us/app/filebrowser-access-files-on/id364738545?mt=8&uo=4&at=11lqkH)应用可以看到连接到Raspberry Pi的设备成为共享文件夹。另外,万一我们决定连接一个便携式硬盘驱动器到Raspberry Pi,`ntfs-3g`提供了NTFS兼容。
257+
258+
```python
259+
260+
sudo apt-get install ntfs-3g
261+
sudo apt-get install samba samba-common-bin
262+
263+
```
264+
265+
你可以看看[这篇](http://www.howtogeek.com/139433/how-to-turn-a-raspberry-pi-into-a-low-power-network-storage-device/)文章,了解如何配置Samba。
266+
267+
**重要提示:** 该引用文章还包含了在Raspberry Pi上挂载外置硬盘的内容。截止到写这篇文章的时候,不用那么做,因为**Raspbian (Jessie)的当前版本在启动的时候,自动挂载SD卡和Pendrive到`/media/pi/`**。该文章还包含了我们不会使用的一些冗余特性。
268+
269+
## 2\. Python脚本
270+
271+
现在,已经配置好了Raspberry Pi,我们需要开始写实际备份/拷贝我们照片的脚本了。注意到**这个脚本只是提供某些程度的自动化给备份过程**。如果你具备使用Linux/Raspbian CLI的基本知识,那么可以SSH到Raspberry Pi,然后通过创建所需文件夹并使用`cp`或者`rsync`命令拷贝设备中的所有照片到另一个地方即可。我们将在脚本种使用`rsync`方法,因为它非常可靠,并允许**增量备份**。
272+
273+
此过程依赖两个文件:脚本自身,以及配置文件`backup_photos.conf`。后者仅需包含几行表示目的驱动器(Pendrive)挂载处以及文件夹挂载处的信息。如下所示:
274+
275+
```python
276+
277+
mount folder=/media/pi/
278+
destination folder=PDRIVE128GB
279+
280+
```
281+
282+
**重要提示:** 不要在`=`符号以及单词之间添加任何空格,这样会搞挂脚本 (这绝对是个改进的机会)。
283+
284+
下面是这个Python脚本,我将其命名为`backup_photos.py`,并把它放在`/home/pi/scripts/`下面。我在代码间加了注释,使其更容易读懂。
285+
286+
```python
287+
288+
#!/usr/bin/python3
289+
290+
import os
291+
import sys
292+
from sh import rsync
293+
294+
'''
295+
The script copies an SD Card mounted on /media/pi/ to a folder with the same name
296+
created in the destination drive. The destination drive's name is defined in
297+
the .conf file.
298+
299+
300+
Argument: label/name of the mounted SD Card.
301+
'''
302+
303+
CONFIG_FILE = '/home/pi/scripts/backup_photos.conf'
304+
ORIGIN_DEV = sys.argv[1]
305+
306+
def create_folder(path):
307+
308+
print ('attempting to create destination folder: ',path)
309+
if not os.path.exists(path):
310+
try:
311+
os.mkdir(path)
312+
print ('Folder created.')
313+
except:
314+
print ('Folder could not be created. Stopping.')
315+
return
316+
else:
317+
print ('Folder already in path. Using that instead.')
318+
319+
320+
321+
confFile = open(CONFIG_FILE,'rU')
322+
#IMPORTANT: rU Opens the file with Universal Newline Support,
323+
#so \n and/or \r is recognized as a new line.
324+
325+
confList = confFile.readlines()
326+
confFile.close()
327+
328+
329+
for line in confList:
330+
line = line.strip('\n')
331+
332+
try:
333+
name , value = line.split('=')
334+
335+
if name == 'mount folder':
336+
mountFolder = value
337+
elif name == 'destination folder':
338+
destDevice = value
339+
340+
341+
except ValueError:
342+
print ('Incorrect line format. Passing.')
343+
pass
344+
345+
346+
destFolder = mountFolder+destDevice+'/'+ORIGIN_DEV
347+
create_folder(destFolder)
348+
349+
print ('Copying files...')
350+
351+
# Comment out to delete files that are not in the origin:
352+
# rsync("-av", "--delete", mountFolder+ORIGIN_DEV, destFolder)
353+
rsync("-av", mountFolder+ORIGIN_DEV+'/', destFolder)
354+
355+
print ('Done.')
356+
357+
```
358+
359+
## 3\. iPad Pro配置
360+
361+
由于所有繁重的工作将会在Raspberry Pi上完成,并且不会通过iPad Pro传输任何文件,这是[我之前尝试的工作流程之一](http://bit.ly/1MVVtZi)的一个巨大的劣势;我们仅需在iPad之上安装[Prompt 2](https://itunes.apple.com/us/app/prompt-2/id917437289?mt=8&uo=4&at=11lqkH),以通过SSH访问Raspeberry Pi。一旦连接了,你可以运行手动Python脚本,或者拷贝文件。
362+
363+
![Prompt截图](http://www.movingelectrons.net/images/bkup_photos_ipad&rpi_prompt.jpg)
364+
365+
iPad使用Prompt通过SSH连接到Raspberry Pi。
366+
367+
由于我们安装了Samba,因此可以以一种更图形化的方式访问连接到Raspberry Pi的USB设备。**你可以播放视频,在设备之间拷贝和移动文件。** [文件浏览器](https://itunes.apple.com/us/app/filebrowser-access-files-on/id364738545?mt=8&uo=4&at=11lqkH)非常适合做这件事。
368+
369+
## 4\. 把它们都放在一起
370+
371+
假设`SD32GB-03`是连接到Raspberry Pi的USB口之一的SD卡的标签。另外,假设`PDRIVE128GB`是Pendrive的标签,它也连接到设备,并在上面提到的`.conf`文件中定义。如果我们想在SD卡中备份照片,那么我们将需要经过以下步骤:
372+
373+
1. 打开Raspberry Pi,让设备自动挂载。
374+
2. 连接到Raspberry Pi生成的WiFi网络。
375+
3. 使用[Prompt](https://itunes.apple.com/us/app/prompt-2/id917437289?mt=8&uo=4&at=11lqkH)应用,通过SSH连接到Raspberry Pi。
376+
4. 一旦连接上了,输入以下内容:
377+
378+
`python3 backup_photos.py SD32GB-03`
379+
380+
第一次备份照片花了几分钟,这取决于使用的卡数量。这意味着,**你需要保持iPad到Raspberry Pi的连接。你可以在运行脚本前使用[nohup](https://en.m.wikipedia.org/wiki/Nohup)命令来解决这个问题**。
381+
382+
`nohup python3 backup_photos.py SD32GB-03 &`
383+
384+
![运行Python脚本后,iterm的截图](http://www.movingelectrons.net/images/bkup_photos_ipad&rpi_finished.png)
385+
386+
运行Python脚本后,iTerm的截图。
387+
388+
## 进一步定制
389+
390+
我安装了一个VNC服务器,以便从另一台电脑,或者使用iPad的[Remoter应用](https://itunes.apple.com/us/app/remoter-pro-vnc-ssh-rdp/id519768191?mt=8&uo=4&at=11lqkH)来访问Raspbian的图形界面。我期待安装[BitTorrent Sync](https://getsync.com),以用来在路上的时候备份照片到一个远程位置,这将会是比较理想的配置。一旦我有了一个可行的解决方案,我将扩展本文。
391+
392+
随意在下面发表评论/提出问题,或者与我联系。我的联系方式在本页的下方(Ele注:请到原文找)。

0 commit comments

Comments
 (0)