Shell脚本:使用rsync备份文件/目录
本文我们介绍一个shell脚本,用来使用rsync命令将你本地Linux机器上的文件/目录备份到远程Linux服务器上。使用该脚本会以交互的方式实施备份,你需要提供远程备份服务器的主机名/ip地址和文件夹位置。我们使用一个单独的列表文件,在这个文件中你需要列出要备份的文件/目录。我们添加了两个脚本,第一个脚本在每次拷贝完一个文件后询问密码(如果你启用了ssh密钥验证,那么就不会询问密码),而第二个脚本中,则只会提示一次输入密码。
我们打算备份bckup.txt,dataconfig.txt,docs和orcledb。
<span class="pun">[</span><span class="pln">root@Fedora21 tmp</span><span class="pun">]#</span><span class="pln"> ls </span><span class="pun">-</span><span class="pln">l</span>
<span class="pln">total </span><span class="lit">12</span>
<span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--.</span><span class="lit">1</span><span class="pln"> root root </span><span class="lit">0</span><span class="typ">May</span><span class="lit">15</span><span class="lit">10</span><span class="pun">:</span><span class="lit">43</span><span class="pln"> bckrsync</span><span class="pun">.</span><span class="pln">sh</span>
<span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--.</span><span class="lit">1</span><span class="pln"> root root </span><span class="lit">0</span><span class="typ">May</span><span class="lit">15</span><span class="lit">10</span><span class="pun">:</span><span class="lit">44</span><span class="pln"> bckup</span><span class="pun">.</span><span class="pln">txt</span>
<span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--.</span><span class="lit">1</span><span class="pln"> root root </span><span class="lit">0</span><span class="typ">May</span><span class="lit">15</span><span class="lit">10</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> dataconfig</span><span class="pun">.</span><span class="pln">txt</span>
<span class="pln">drwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x</span><span class="pun">.</span><span class="lit">2</span><span class="pln"> root root </span><span class="lit">4096</span><span class="typ">May</span><span class="lit">15</span><span class="lit">10</span><span class="pun">:</span><span class="lit">45</span><span class="pln"> docs</span>
<span class="pln">drwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x</span><span class="pun">.</span><span class="lit">2</span><span class="pln"> root root </span><span class="lit">4096</span><span class="typ">May</span><span class="lit">15</span><span class="lit">10</span><span class="pun">:</span><span class="lit">44</span><span class="pln"> Oracledb</span>
bckup.txt文件包含了需要备份的文件/目录的详情
<span class="pun">[</span><span class="pln">root@Fedora21 tmp</span><span class="pun">]#</span><span class="pln"> cat </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">bckup</span><span class="pun">.</span><span class="pln">txt</span>
<span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">oracledb</span>
<span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">dataconfig</span><span class="pun">.</span><span class="pln">txt</span>
<span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">docs</span>
<span class="pun">[</span><span class="pln">root@Fedora21 tmp</span><span class="pun">]#</span>
脚本 1:
<span class="com">#!/bin/bash</span>
<span class="com"># 将备份列表文件的路径保存到变量中</span>
<span class="pln">backupf</span><span class="pun">=</span><span class="str">'/tmp/bckup.txt'</span>
<span class="com"># 输入一个提示信息</span>
<span class="pln">echo </span><span class="str">"Shell Script Backup Your Files / Directories Using rsync"</span>
<span class="com"># 检查是否输入了目标服务器,如果为空就再次提示用户输入</span>
<span class="kwd">while</span><span class="pun">[</span><span class="pln"> x$desthost </span><span class="pun">=</span><span class="str">"x"</span><span class="pun">];</span><span class="kwd">do</span>
<span class="com"># 提示用户输入目标服务器地址并保存到变量</span>
<span class="pln">read </span><span class="pun">-</span><span class="pln">p </span><span class="str">"Destination backup Server : "</span><span class="pln"> desthost</span>
<span class="com"># 结束循环</span>
<span class="kwd">done</span>
<span class="com"># 检查是否输入了目标文件夹,如果为空就再次提示用户输入</span>
<span class="kwd">while</span><span class="pun">[</span><span class="pln"> x$destpath </span><span class="pun">=</span><span class="str">"x"</span><span class="pun">];</span><span class="kwd">do</span>
<span class="com"># 提示用户输入目标文件夹并保存到变量</span>
<span class="pln">read </span><span class="pun">-</span><span class="pln">p </span><span class="str">"Destination Folder : "</span><span class="pln"> destpath</span>
<span class="com"># 结束循环</span>
<span class="kwd">done</span>
<span class="com"># 逐行读取备份列表文件</span>
<span class="kwd">for</span><span class="pln"> line </span><span class="kwd">in</span><span class="str">`cat $backupf`</span>
<span class="com"># 对每一行都进行处理</span>
<span class="kwd">do</span>
<span class="com"># 显示要被复制的文件/文件夹名称</span>
<span class="pln">echo </span><span class="str">"Copying $line ... "</span>
<span class="com"># 通过 rsync 复制文件/文件夹到目标位置</span>
<span class="pln">rsync </span><span class="pun">-</span><span class="pln">ar </span><span class="str">"$line"</span><span class="str">"$desthost"</span><span class="pun">:</span><span class="str">"$destpath"</span>
<span class="com"># 显示完成</span>
<span class="pln">echo </span><span class="str">"DONE"</span>
<span class="com"># 结束</span>
<span class="kwd">done</span>
运行带有输出结果的脚本
<span class="pun">[</span><span class="pln">root@Fedora21 tmp</span><span class="pun">]#</span><span class="pun">./</span><span class="pln">bckrsync</span><span class="pun">.</span><span class="pln">sh</span>
<span class="typ">Shell</span><span class="typ">Script</span><span class="typ">Backup</span><span class="typ">Your</span><span class="typ">Files</span><span class="pun">/</span><span class="typ">Directories</span><span class="typ">Using</span><span class="pln"> rsync</span>
<span class="typ">Destination</span><span class="pln"> backup </span><span class="typ">Server</span><span class="pun">:</span><span class="lit">104.</span><span class="pun">*.*.</span><span class="lit">41</span>
<span class="typ">Destination</span><span class="typ">Folder</span><span class="pun">:</span><span class="pun">/</span><span class="pln">tmp</span>
<span class="typ">Copying</span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">oracledb </span><span class="pun">...</span>
<span class="typ">The</span><span class="pln"> authenticity of host </span><span class="str">'104.*.*.41 (104.*.*.41)'</span><span class="pln"> can</span><span class="str">'t be established.</span>
<span class="str">ECDSA key fingerprint is 96:11:61:17:7f:fa:......</span>
<span class="str">Are you sure you want to continue connecting (yes/no)? yes</span>
<span class="str">Warning: Permanently added '</span><span class="lit">104.</span><span class="pun">*.*.</span><span class="lit">41</span><span class="str">' (ECDSA) to the list of known hosts.</span>
<span class="str">root@104.*.*.41'</span><span class="pln">s password</span><span class="pun">:</span>
<span class="pln">DONE</span>
<span class="typ">Copying</span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">dataconfig</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">...</span>
<span class="pln">root@104</span><span class="pun">.*.*.</span><span class="lit">41</span><span class="str">'s password:</span>
<span class="str">DONE</span>
<span class="str">Copying /tmp/docs ...</span>
<span class="str">root@104.*.*.41'</span><span class="pln">s password</span><span class="pun">:</span>
<span class="pln">DONE</span>
<span class="pun">[</span><span class="pln">root@Fedora21 tmp</span><span class="pun">]#</span>
脚本 2:
<span class="com">#!/bin/bash</span>
<span class="com"># 将备份列表文件的路径保存到变量中</span>
<span class="pln">backupf</span><span class="pun">=</span><span class="str">'/tmp/bckup.txt'</span>
<span class="com"># 输入一个提示信息</span>
<span class="pln">echo </span><span class="str">"Shell Script Backup Your Files / Directories Using rsync"</span>
<span class="com"># 检查是否输入了目标服务器,如果为空就再次提示用户输入</span>
<span class="kwd">while</span><span class="pun">[</span><span class="pln"> x$desthost </span><span class="pun">=</span><span class="str">"x"</span><span class="pun">];</span><span class="kwd">do</span>
<span class="com"># 提示用户输入目标服务器地址并保存到变量</span>
<span class="pln">read </span><span class="pun">-</span><span class="pln">p </span><span class="str">"Destination backup Server : "</span><span class="pln"> desthost</span>
<span class="com"># 结束循环</span>
<span class="kwd">done</span>
<span class="com"># 检查是否输入了目标文件夹,如果为空就再次提示用户输入</span>
<span class="kwd">while</span><span class="pun">[</span><span class="pln"> x$destpath </span><span class="pun">=</span><span class="str">"x"</span><span class="pun">];</span><span class="kwd">do</span>
<span class="com"># 提示用户输入目标文件夹并保存到变量</span>
<span class="pln">read </span><span class="pun">-</span><span class="pln">p </span><span class="str">"Destination Folder : "</span><span class="pln"> destpath</span>
<span class="com"># 结束循环</span>
<span class="kwd">done</span>
<span class="com"># 检查是否输入了目标服务器密码,如果为空就再次提示用户输入</span>
<span class="kwd">while</span><span class="pun">[</span><span class="pln"> x$password </span><span class="pun">=</span><span class="str">"x"</span><span class="pun">];</span><span class="kwd">do</span>
<span class="com"># 提示用户输入密码并保存到变量</span>
<span class="com"># 使用 -s 选项不回显输入的密码</span>
<span class="pln">read </span><span class="pun">-</span><span class="pln">sp </span><span class="str">"Password : "</span><span class="pln"> password</span>
<span class="com"># 结束循环</span>
<span class="kwd">done</span>
<span class="com"># 逐行读取备份列表文件</span>
<span class="kwd">for</span><span class="pln"> line </span><span class="kwd">in</span><span class="str">`cat $backupf`</span>
<span class="com"># 对每一行都进行处理</span>
<span class="kwd">do</span>
<span class="com"># 显示要被复制的文件/文件夹名称</span>
<span class="pln">echo </span><span class="str">"Copying $line ... "</span>
<span class="com"># 使用 expect 来在脚本中输入密码</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">expect </span><span class="pun"><<</span><span class="pln"> EOD</span>
<span class="com"># 推荐设置超时为 -1 </span>
<span class="kwd">set</span><span class="pln"> timeout </span><span class="pun">-</span><span class="lit">1</span>
<span class="com"># 通过 rsync 复制文件/文件夹到目标位置,使用 expect 的组成部分 spawn 命令</span>
<span class="pln">spawn rsync </span><span class="pun">-</span><span class="pln">ar $</span><span class="pun">{</span><span class="pln">line</span><span class="pun">}</span><span class="pln"> $</span><span class="pun">{</span><span class="pln">desthost</span><span class="pun">}:</span><span class="pln">$</span><span class="pun">{</span><span class="pln">destpath</span><span class="pun">}</span>
<span class="com"># 上一行命令会等待 “password” 提示</span>
<span class="pln">expect </span><span class="str">"*?assword:*"</span>
<span class="com"># 在脚本中提供密码</span>
<span class="pln">send </span><span class="str">"${password}\r"</span>
<span class="com"># 等待文件结束符(远程服务器处理完了所有事情)</span>
<span class="pln">expect eof</span>
<span class="com"># 结束 expect 脚本</span>
<span class="pln">EOD</span>
<span class="com"># 显示结束</span>
<span class="pln">echo </span><span class="str">"DONE"</span>
<span class="com"># 完成</span>
<span class="kwd">done</span>
运行第二个带有输出结果的脚本的屏幕截图
希望这些脚本对你备份会有帮助!!
Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里
via: http://linoxide.com/linux-shell-script/shell-script-backup-files-directories-rsync/
作者:Yevhen Duma 译者:GOLinux 校对:wxy
相关推荐
彼岸随笔 2020-10-20
yutou0 2020-10-17
jarrygao 2020-11-02
shipinsky 2020-09-23
touchfuture 2020-09-16
hongsheyoumo 2020-09-04
tianhuak 2020-11-24
huha 2020-10-16
lianshaohua 2020-09-23
laisean 2020-11-11
zhangjie 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
以梦为马不负韶华 2020-10-20
CARBON 2020-10-20
lianshaohua 2020-10-20
JohnYork 2020-10-16