Shell脚本:使用rsync备份文件/目录

Shell脚本:使用rsync备份文件/目录

本文我们介绍一个shell脚本,用来使用rsync命令将你本地Linux机器上的文件/目录备份到远程Linux服务器上。使用该脚本会以交互的方式实施备份,你需要提供远程备份服务器的主机名/ip地址和文件夹位置。我们使用一个单独的列表文件,在这个文件中你需要列出要备份的文件/目录。我们添加了两个脚本,第一个脚本在每次拷贝完一个文件后询问密码(如果你启用了ssh密钥验证,那么就不会询问密码),而第二个脚本中,则只会提示一次输入密码。

我们打算备份bckup.txt,dataconfig.txt,docs和orcledb。

  1. <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>
  2. <span class="pln">total </span><span class="lit">12</span>
  3. <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>
  4. <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>
  5. <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>
  6. <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>
  7. <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文件包含了需要备份的文件/目录的详情

  1. <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>
  2. <span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">oracledb</span>
  3. <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>
  4. <span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">docs</span>
  5. <span class="pun">[</span><span class="pln">root@Fedora21 tmp</span><span class="pun">]#</span>

脚本 1:

  1. <span class="com">#!/bin/bash</span>
  2. <span class="com"># 将备份列表文件的路径保存到变量中</span>
  3. <span class="pln">backupf</span><span class="pun">=</span><span class="str">'/tmp/bckup.txt'</span>
  4. <span class="com"># 输入一个提示信息</span>
  5. <span class="pln">echo </span><span class="str">"Shell Script Backup Your Files / Directories Using rsync"</span>
  6. <span class="com"># 检查是否输入了目标服务器,如果为空就再次提示用户输入</span>
  7. <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>
  8. <span class="com"># 提示用户输入目标服务器地址并保存到变量</span>
  9. <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>
  10. <span class="com"># 结束循环</span>
  11. <span class="kwd">done</span>
  12. <span class="com"># 检查是否输入了目标文件夹,如果为空就再次提示用户输入</span>
  13. <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>
  14. <span class="com"># 提示用户输入目标文件夹并保存到变量</span>
  15. <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>
  16. <span class="com"># 结束循环</span>
  17. <span class="kwd">done</span>
  18. <span class="com"># 逐行读取备份列表文件</span>
  19. <span class="kwd">for</span><span class="pln"> line </span><span class="kwd">in</span><span class="str">`cat $backupf`</span>
  20. <span class="com"># 对每一行都进行处理</span>
  21. <span class="kwd">do</span>
  22. <span class="com"># 显示要被复制的文件/文件夹名称</span>
  23. <span class="pln">echo </span><span class="str">"Copying $line ... "</span>
  24. <span class="com"># 通过 rsync 复制文件/文件夹到目标位置</span>
  25. <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>
  26. <span class="com"># 显示完成</span>
  27. <span class="pln">echo </span><span class="str">"DONE"</span>
  28. <span class="com"># 结束</span>
  29. <span class="kwd">done</span>

运行带有输出结果的脚本

  1. <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>
  2. <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>
  3. <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>
  4. <span class="typ">Destination</span><span class="typ">Folder</span><span class="pun">:</span><span class="pun">/</span><span class="pln">tmp</span>
  5. <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>
  6. <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>
  7. <span class="str">ECDSA key fingerprint is 96:11:61:17:7f:fa:......</span>
  8. <span class="str">Are you sure you want to continue connecting (yes/no)? yes</span>
  9. <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>
  10. <span class="str">root@104.*.*.41'</span><span class="pln">s password</span><span class="pun">:</span>
  11. <span class="pln">DONE</span>
  12. <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>
  13. <span class="pln">root@104</span><span class="pun">.*.*.</span><span class="lit">41</span><span class="str">'s password:</span>
  14. <span class="str">DONE</span>
  15. <span class="str">Copying /tmp/docs ...</span>
  16. <span class="str">root@104.*.*.41'</span><span class="pln">s password</span><span class="pun">:</span>
  17. <span class="pln">DONE</span>
  18. <span class="pun">[</span><span class="pln">root@Fedora21 tmp</span><span class="pun">]#</span>

脚本 2:

  1. <span class="com">#!/bin/bash</span>
  2. <span class="com"># 将备份列表文件的路径保存到变量中</span>
  3. <span class="pln">backupf</span><span class="pun">=</span><span class="str">'/tmp/bckup.txt'</span>
  4. <span class="com"># 输入一个提示信息</span>
  5. <span class="pln">echo </span><span class="str">"Shell Script Backup Your Files / Directories Using rsync"</span>
  6. <span class="com"># 检查是否输入了目标服务器,如果为空就再次提示用户输入</span>
  7. <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>
  8. <span class="com"># 提示用户输入目标服务器地址并保存到变量</span>
  9. <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>
  10. <span class="com"># 结束循环</span>
  11. <span class="kwd">done</span>
  12. <span class="com"># 检查是否输入了目标文件夹,如果为空就再次提示用户输入</span>
  13. <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>
  14. <span class="com"># 提示用户输入目标文件夹并保存到变量</span>
  15. <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>
  16. <span class="com"># 结束循环</span>
  17. <span class="kwd">done</span>
  18. <span class="com"># 检查是否输入了目标服务器密码,如果为空就再次提示用户输入</span>
  19. <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>
  20. <span class="com"># 提示用户输入密码并保存到变量</span>
  21. <span class="com"># 使用 -s 选项不回显输入的密码</span>
  22. <span class="pln">read </span><span class="pun">-</span><span class="pln">sp </span><span class="str">"Password : "</span><span class="pln"> password</span>
  23. <span class="com"># 结束循环</span>
  24. <span class="kwd">done</span>
  25. <span class="com"># 逐行读取备份列表文件</span>
  26. <span class="kwd">for</span><span class="pln"> line </span><span class="kwd">in</span><span class="str">`cat $backupf`</span>
  27. <span class="com"># 对每一行都进行处理</span>
  28. <span class="kwd">do</span>
  29. <span class="com"># 显示要被复制的文件/文件夹名称</span>
  30. <span class="pln">echo </span><span class="str">"Copying $line ... "</span>
  31. <span class="com"># 使用 expect 来在脚本中输入密码</span>
  32. <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>
  33. <span class="com"># 推荐设置超时为 -1 </span>
  34. <span class="kwd">set</span><span class="pln"> timeout </span><span class="pun">-</span><span class="lit">1</span>
  35. <span class="com"># 通过 rsync 复制文件/文件夹到目标位置,使用 expect 的组成部分 spawn 命令</span>
  36. <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>
  37. <span class="com"># 上一行命令会等待 “password” 提示</span>
  38. <span class="pln">expect </span><span class="str">"*?assword:*"</span>
  39. <span class="com"># 在脚本中提供密码</span>
  40. <span class="pln">send </span><span class="str">"${password}\r"</span>
  41. <span class="com"># 等待文件结束符(远程服务器处理完了所有事情)</span>
  42. <span class="pln">expect eof</span>
  43. <span class="com"># 结束 expect 脚本</span>
  44. <span class="pln">EOD</span>
  45. <span class="com"># 显示结束</span>
  46. <span class="pln">echo </span><span class="str">"DONE"</span>
  47. <span class="com"># 完成</span>
  48. <span class="kwd">done</span>

运行第二个带有输出结果的脚本的屏幕截图

Shell脚本:使用rsync备份文件/目录

希望这些脚本对你备份会有帮助!!

Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里


via: http://linoxide.com/linux-shell-script/shell-script-backup-files-directories-rsync/

作者:Yevhen Duma 译者:GOLinux 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

相关推荐