在expect脚本中同步VirtualBox目录时rsync挂起

时间:2016-06-07 14:08:11

标签: expect rsync

我的期望脚本在r同步VirtualBox目录时无法继续。

这是脚本:

 #!/usr/bin/expect -f

         spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/james/Documents/ username@server:/home/username/BAK/Documents/
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

         spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/james/AndroidDev/ username@server:/home/username/BAK/AndroidDev/
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

         spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/james/Android/ username@server:/home/username/BAK/Android/
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

        spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/james/Videos/ username@server:/home/username/BAK/Videos/ ;
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

         spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats /home/james/VirtualBox\ VMs/ username@server:/home/username/BAK/"VirtualBox\ VMs/" ;
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

         spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/james/Pictures/ username@server:/home/username/BAK/Pictures ;
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

         spawn scp -v /home/james/.vimrc username@server:/home/username/BAK/ ;
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

         spawn scp -v /home/james/.bashrc username@server:/home/username/BAK/ ;
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

         spawn scp -v /home/james/.profile username@server:/home/username/BAK/ ;
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

         spawn scp -v /home/james/.xsession* username@server:/home/username/BAK/ ;
         expect "password:"
         send "thepassword\r"
         interact
         sleep 1

如果我将spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy --stats /home/rschostag/VirtualBox\ VMs/ remote@server:/home/directory/BAK/VirtualBox更改为spawn scp -vr /home/rschostag/VirtualBox\ VMs/ remote@server:/home/directory/BAK/VirtualBox,则可行。但是,我不想每次都进行完整备份。

逐步备份此目录或以其他方式使rsync命令正常工作的最佳方法是什么?

谢谢!

根据要求,请查看expect -d script的输出如下:

$ sudo expect -d rsync_backup
expect version 5.45
argv[0] = expect  argv[1] = -d  argv[2] = rsync_backup  
set argc 0
set argv0 "rsync_backup"
set argv ""
executing commands from command file rsync_backup
spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy --stats /home/james/Documents/ username@server:/home/username/BAK/Documents/
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {5064}

expect: does "" (spawn_id exp6) match glob pattern "password:"? no
username@server password: 
expect: does "username@server password: " (spawn_id exp6) match glob pattern "password:"? yes
expect: set expect_out(0,string) "password:"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "username@server password:"
send: sending "thepassword\r" to { exp6 }
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp6 sent <\r\n>

spawn id exp6 sent <sending incremental file list\r\n>
sending incremental file list
spawn id exp6 sent <Scripts/rsync.log\r\n>
Scripts/rsync.log
spawn id exp6 sent <\r          2.29K   0%    0.00kB/s    0:00:00  >
          2.29K   0%    0.00kB/s    0:00:00  spawn id exp6 sent <\r          5.26M 100%   78.42MB/s    0:00:00 (xfr#1, ir-chk=1561/3024)\r\n>
          5.26M 100%   78.42MB/s    0:00:00 (xfr#1, ir-chk=1561/3024)
spawn id exp6 sent <projects/JOBS/jobsearch_production/\r\n>
projects/JOBS/jobsearch_production/
spawn id exp6 sent <projects/JOBS/jobsearch_production/db.sqlite3\r\n>
projects/JOBS/jobsearch_production/db.sqlite3
spawn id exp6 sent <\r          5.41K   0%    0.00kB/s    0:00:00  >
          5.41K   0%    0.00kB/s    0:00:00  spawn id exp6 sent <\r          7.35M 100%   73.71MB/s    0:00:00 (xfr#2, ir-chk=1025/6252)\r\n>
          7.35M 100%   73.71MB/s    0:00:00 (xfr#2, ir-chk=1025/6252)
spawn id exp6 sent <\r\nNumber of files: 10,529 (reg: 8,419, dir: 2,096, link: 14)>

Number of files: 10,529 (reg: 8,419, dir: 2,096, link: 14)spawn id exp6 sent <\r\nNumber of created files: 0>

Number of created files: 0spawn id exp6 sent <\r\nNumber of regular files transferred: 2>

Number of regular files transferred: 2spawn id exp6 sent <\r\nTotal file size: 1.73G bytes>

Total file size: 1.73G bytesspawn id exp6 sent <\r\nTotal transferred file size: 12.61M bytes>

Total transferred file size: 12.61M bytesspawn id exp6 sent <\r\nLiteral data: 62.70K bytes>

Literal data: 62.70K bytesspawn id exp6 sent <\r\nMatched data: 12.55M bytes>

Matched data: 12.55M bytesspawn id exp6 sent <\r\nFile list size: 0>

File list size: 0spawn id exp6 sent <\r\nFile list generation time: 0.001 seconds>

File list generation time: 0.001 secondsspawn id exp6 sent <\r\nFile list transfer time: 0.000 seconds>

File list transfer time: 0.000 secondsspawn id exp6 sent <\r\nTotal bytes sent: 333.42K>

Total bytes sent: 333.42Kspawn id exp6 sent <\r\nTotal bytes received: 40.66K>

Total bytes received: 40.66Kspawn id exp6 sent <\r\n\r\n>


spawn id exp6 sent <sent 333.42K bytes  received 40.66K bytes  32.53K bytes/sec\r\ntotal size is 1.73G  speedup is 4,616.35>
sent 333.42K bytes  received 40.66K bytes  32.53K bytes/sec
total size is 1.73G  speedup is 4,616.35spawn id exp6 sent <\r\n>

interact: received eof from spawn_id exp6
tty_set: raw = 0, echo = 1
spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy --stats /home/james/AndroidDev/ username@server:/home/username/BAK/AndroidDev/
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {5069}

expect: does "" (spawn_id exp7) match glob pattern "password:"? no
username@server password: 
expect: does "username@server password: " (spawn_id exp7) match glob pattern "password:"? yes
expect: set expect_out(0,string) "password:"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "username@server password:"
send: sending "thepassword\r" to { exp7 }
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp7 sent <\r\n>

spawn id exp7 sent <sending incremental file list>
sending incremental file listspawn id exp7 sent <\r\n>

spawn id exp7 sent <\r\n>

spawn id exp7 sent <Number of files: 2,758 (reg: 2,273, dir: 485)>
Number of files: 2,758 (reg: 2,273, dir: 485)spawn id exp7 sent <\r\n>

spawn id exp7 sent <Number of created files: 0>
Number of created files: 0spawn id exp7 sent <\r\nNumber of regular files transferred: 0\r\nTotal file size: 1.00G bytes>

Number of regular files transferred: 0
Total file size: 1.00G bytesspawn id exp7 sent <\r\nTotal transferred file size: 0 bytes>

Total transferred file size: 0 bytesspawn id exp7 sent <\r\nLiteral data: 0 bytes\r\nMatched data: 0 bytes>

Literal data: 0 bytes
Matched data: 0 bytesspawn id exp7 sent <\r\nFile list size: 0\r\nFile list generation time: 0.001 seconds>

File list size: 0
File list generation time: 0.001 secondsspawn id exp7 sent <\r\nFile list transfer time: 0.000 seconds>

File list transfer time: 0.000 secondsspawn id exp7 sent <\r\nTotal bytes sent: 74.25K\r\n>

Total bytes sent: 74.25K
spawn id exp7 sent <Total bytes received: 2.43K\r\n\r\nsent 74.25K bytes  received 2.43K bytes  30.67K bytes/sec>
Total bytes received: 2.43K

sent 74.25K bytes  received 2.43K bytes  30.67K bytes/secspawn id exp7 sent <\r\ntotal size is 1.00G  speedup is 13,102.55>

total size is 1.00G  speedup is 13,102.55spawn id exp7 sent <\r\n>

interact: received eof from spawn_id exp7
tty_set: raw = 0, echo = 1
spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy --stats /home/james/Android/ username@server:/home/username/BAK/Android/
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {5074}

expect: does "" (spawn_id exp8) match glob pattern "password:"? no
username@server password: 
expect: does "username@server password: " (spawn_id exp8) match glob pattern "password:"? yes
expect: set expect_out(0,string) "password:"
expect: set expect_out(spawn_id) "exp8"
expect: set expect_out(buffer) "username@server password:"
send: sending "thepassword\r" to { exp8 }
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp8 sent <\r\n>

spawn id exp8 sent <sending incremental file list\r\n>
sending incremental file list
spawn id exp8 sent <\r\nNumber of files: 3 (dir: 3)>

Number of files: 3 (dir: 3)spawn id exp8 sent <\r\n>

spawn id exp8 sent <Number of created files: 0>
Number of created files: 0spawn id exp8 sent <\r\nNumber of regular files transferred: 0>

Number of regular files transferred: 0spawn id exp8 sent <\r\nTotal file size: 0 bytes>

Total file size: 0 bytesspawn id exp8 sent <\r\nTotal transferred file size: 0 bytes>

Total transferred file size: 0 bytesspawn id exp8 sent <\r\n>

spawn id exp8 sent <Literal data: 0 bytes\r\nMatched data: 0 bytes\r\nFile list size: 0>
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 0spawn id exp8 sent <\r\n>

spawn id exp8 sent <File list generation time: 0.001 seconds\r\nFile list transfer time: 0.000 seconds\r\nTotal bytes sent: 83>
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 83spawn id exp8 sent <\r\nTotal bytes received: 21>

Total bytes received: 21spawn id exp8 sent <\r\n\r\n>


spawn id exp8 sent <sent 83 bytes  received 21 bytes  69.33 bytes/sec\r\ntotal size is 0  speedup is 0.00>
sent 83 bytes  received 21 bytes  69.33 bytes/sec
total size is 0  speedup is 0.00spawn id exp8 sent <\r\n>

interact: received eof from spawn_id exp8
tty_set: raw = 0, echo = 1
spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy --stats /home/james/Videos/ username@server:/home/username/BAK/Videos/
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {5079}

expect: does "" (spawn_id exp9) match glob pattern "password:"? no
username@server password: 
expect: does "username@server password: " (spawn_id exp9) match glob pattern "password:"? yes
expect: set expect_out(0,string) "password:"
expect: set expect_out(spawn_id) "exp9"
expect: set expect_out(buffer) "username@server password:"
send: sending "thepassword\r" to { exp9 }
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp9 sent <\r\n>

spawn id exp9 sent <sending incremental file list\r\n>
sending incremental file list
spawn id exp9 sent <\r\nNumber of files: 30 (reg: 29, dir: 1)\r\nNumber of created files: 0>

Number of files: 30 (reg: 29, dir: 1)
Number of created files: 0spawn id exp9 sent <\r\nNumber of regular files transferred: 0>

Number of regular files transferred: 0spawn id exp9 sent <\r\nTotal file size: 1.35G bytes>

Total file size: 1.35G bytesspawn id exp9 sent <\r\nTotal transferred file size: 0 bytes>

Total transferred file size: 0 bytesspawn id exp9 sent <\r\nLiteral data: 0 bytes>

Literal data: 0 bytesspawn id exp9 sent <\r\nMatched data: 0 bytes\r\nFile list size: 0>

Matched data: 0 bytes
File list size: 0spawn id exp9 sent <\r\nFile list generation time: 0.001 seconds>

File list generation time: 0.001 secondsspawn id exp9 sent <\r\nFile list transfer time: 0.000 seconds>

File list transfer time: 0.000 secondsspawn id exp9 sent <\r\nTotal bytes sent: 587\r\n>

Total bytes sent: 587
spawn id exp9 sent <Total bytes received: 11\r\n\r\nsent 587 bytes  received 11 bytes  398.67 bytes/sec\r\n>
Total bytes received: 11

sent 587 bytes  received 11 bytes  398.67 bytes/sec
spawn id exp9 sent <total size is 1.35G  speedup is 2,263,533.89\r\n>
total size is 1.35G  speedup is 2,263,533.89
interact: received eof from spawn_id exp9
tty_set: raw = 0, echo = 1
spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy --stats /home/james/VirtualBox VMs/ username@server:/home/username/BAK/"VirtualBox VMs/"
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {5084}

expect: does "" (spawn_id exp10) match glob pattern "password:"? no
username@server password: 
expect: does "username@server password: " (spawn_id exp10) match glob pattern "password:"? yes
expect: set expect_out(0,string) "password:"
expect: set expect_out(spawn_id) "exp10"
expect: set expect_out(buffer) "username@server password:"
send: sending "thepassword\r" to { exp10 }
tty_raw_noecho: was raw = 0  echo = 1
spawn id exp10 sent <\r\n>

spawn id exp10 sent <sending incremental file list\r\n>
sending incremental file list

这是它挂起的地方,并没有提供我可以识别的错误消息。

我移动了远程VirtualBox目录并再次启动备份,并在运行备份几天后最终完成。备份的总大小为71 GB。我用expect -d运行它,它在终端中输出了一个非常详细的输出,所以这单独是有用的,因为没有-d的脚本可能看起来像是挂起只是因为它没有在输出中打印任何内容。所以,我相信,除了没有任何进展指示之外,从来没有任何错误,这让我相信它可能不起作用。

感谢您就expect -d选项向我提供建议。

1 个答案:

答案 0 :(得分:0)

@glennjackman在评论中回答了我的问题,但没有正式回答这个问题。

以下是适用的新脚本:

#!/usr/bin/expect -f

        spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/ryan/Documents/ username@server:/home/username/BAK/Documents/
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/ryan/AndroidDev/ username@server:/home/username/BAK/AndroidDev/
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/ryan/Android/ username@server:/home/username/BAK/Android/
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/ryan/Videos/ username@server:/home/username/BAK/Videos/ ;
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats /home/ryan/VirtualBox\ VMs/ username@server:/home/username/BAK/"VirtualBox\ VMs/" ;
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn rsync -vruEtl --progress -h --log-file=rsync.log --fuzzy  --stats  /home/ryan/Pictures/ username@server:/home/username/BAK/Pictures ;
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn scp -v /home/ryan/.vimrc username@server:/home/username/BAK/ ;
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn scp -v /home/ryan/.bashrc username@server:/home/username/BAK/ ;
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn scp -v /home/ryan/.profile username@server:/home/username/BAK/ ;
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1

        spawn scp -v /home/ryan/.xsession* username@server:/home/username/BAK/ ;
        expect "password:"
        send "thepassword\r"
        expect eof
        sleep 1