本文我们介绍一个shell脚本,用来使用rsync命令将你本地Linux机器上的文件/目录备份到远程Linux服务器上。使用该脚本会以交互的方式实施备份,你需要提供远程备份服务器的主机名/ip地址和文件夹位置。我们使用一个单独的列表文件,在这个文件中你需要列出要备份的文件/目录。我们添加了两个脚本,第一个脚本 在每次拷贝完一个文件后询问密码(如果你启用了ssh密钥验证,那么就不会询问密码),而第二个脚本中,则只会提示一次输入密码。
我们打算备份bckup.txt,dataconfig.txt,docs和orcledb。
1 2 3 4 5 6 7 8 [root@Fedora21 tmp] total 12 -rw-r--r--. 1  root root 0  May 15  10:43 bckrsync.sh -rw-r--r--. 1  root root 0  May 15  10:44 bckup.txt -rw-r--r--. 1  root root 0  May 15  10:46 dataconfig.txt drwxr-xr-x. 2  root root 4096  May 15  10:45 docs drwxr-xr-x. 2  root root 4096  May 15  10:44 oracledb
 
bckup.txt文件包含了需要备份的文件/目录的详情
1 2 3 4 5 6 [root@Fedora21  tmp]/tmp/oracledb /tmp/dataconfig .txt/tmp/docs  [root@Fedora21  tmp]
 
脚本 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #!/bin/bash  backupf='/tmp/bckup.txt' echo  "Shell Script Backup Your Files / Directories Using rsync" while  [ x$desthost  = "x"  ]; do read  -p "Destination backup Server : "  desthostdone while  [ x$destpath  = "x"  ]; do read  -p "Destination Folder : "  destpathdone for  line in  `cat  $backupf `do echo  "Copying $line  ... "  rsync -ar "$line "  "$desthost " :"$destpath " echo  "DONE" done 
 
运行带有输出结果的脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [root@Fedora21 tmp] Shell Script Backup Your Files / Directories Using rsync Destination backup Server : 104.*.*.41  Destination Folder : /tmp  Copying /tmp/oracledb  ...  The authenticity of host '104.*.*.41  (104.*.*.41) ' can't be established. ECDSA key fingerprint is 96:11 :61 :17 :7f :fa :......  Are you sure you want to continue connecting (yes/no) ? yes Warning: Permanently added '104.*.*.41 ' (ECDSA)  to the list of known hosts. root@104.*.*.41 's password: DONE Copying /tmp/dataconfig.txt  ...  root@104.*.*.41 's password: DONE Copying /tmp/docs  ...  root@104.*.*.41 's password: DONE [root@Fedora21 tmp]
 
脚本 2: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #!/bin/bash  backupf='/tmp/bckup.txt' echo  "Shell Script Backup Your Files / Directories Using rsync" while  [ x$desthost  = "x"  ]; do read  -p "Destination backup Server : "  desthostdone while  [ x$destpath  = "x"  ]; do read  -p "Destination Folder : "  destpathdone while  [ x$password  = "x"  ]; do read  -sp "Password : "  passworddone for  line in  `cat  $backupf `do echo  "Copying $line  ... "  /usr/bin/expect << EOD # 推荐设置超时为 -1  set timeout -1 # 通过 rsync 复制文件/文件夹到目标位置,使用 expect 的组成部分 spawn 命令 spawn rsync -ar ${line} ${desthost}:${destpath} # 上一行命令会等待 “password” 提示 expect "*?assword:*" # 在脚本中提供密码 send "${password}\r" # 等待文件结束符(远程服务器处理完了所有事情) expect eof # 结束 expect 脚本 EOD echo  "DONE" done 
 
运行第二个带有输出结果的脚本的屏幕截图 
希望这些脚本对你备份会有帮助!!
 
via: http://linoxide.com/linux-shell-script/shell-script-backup-files-directories-rsync/ 
作者:Yevhen Duma  译者:GOLinux  校对:wxy 
本文由 LCTT  原创翻译,Linux中国  荣誉推出