免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1133 | 回复: 0
打印 上一主题 下一主题

使用rsync备份和恢复文件脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-21 17:15 |只看该作者 |倒序浏览
Note:
To use the following scripts, you need to install:
   * tcl, expect, ssh, rsync on local machine
   * ssh, rsync on remote server
and ssh, rsync commands must in both sides' PATH environment.
Suggested versions:
tcl: 8.4.15
expect: 5.43.0
rsync: 3.0.2
How to install:
tcl:
     cd unix
     ./configure --prefix=/usr
     make && make install
     make install-private-headers
     ln -sv tclsh8.4 /usr/bin/tclsh
expect:
     ./configure --prefix=/usr --with-tcl=/usr/lib \
     --with-tclinclude=/usr/include --with-x=no
     make
     make SCRIPTS="" install
1. bak.sh is used for backing up contents under /mnt to remote server:
usage:
./bak.sh remoteip directory user password
e.g.
./bak.sh 192.168.22.110 /cas_bak root 123456
change SRC in the script if you want to backup other directory.
2. restore.sh is used for restoring files back:
usage:
./restore.sh remoteip directory user password
e.g.
./restore.sh 192.168.22.110 /cas_bak root 123456
change DEST in the script if you want to restore to other directory.
RETURN VALUE:
100: backup/restore success
101: No route to host
102: password incorrect
103: connection refused
104: incorrect command line arguments
bak.sh
#!/usr/bin/expect -f
set SRC /mnt/
set timeout 5
proc do_ssh_login {host dir username pass} {
    global SRC
    set done 1
    if { [string index $dir end] == "/" } {
        set dir [string trimright $dir "/"]
    }
    if { [string index $SRC end] == "/" } {
        spawn rsync -azP -e ssh $SRC $username@$host:$dir
    } else {
        spawn rsync -azP -e ssh $SRC/ $username@$host:$dir
    }
    send_user "connecting to $host\n"
    while {$done} {
        expect {
            "*No route to host" {
                send_user "No route to host!\n"
                close
                exit 101
            }
            "*(yes/no)?" {send "yes\n"}
            "?assword:"  {send "$pass\n"}
            "*Permission denied*" {
                send_user "password incorrect!\n"
                close
                exit 102
            }
            "*Connection refused*" {
                send_user "connection refused!\n"
                close
                exit 103
            }
            "*speedup is*" {
                send_user "backup complete!\n"
                close
                exit 100
            }
        }
    }
}
if {$argc != 4} {
    puts stderr "Usage: ./bak.sh host dir username password\n"
        exit 104
}
set host [lindex $argv 0]
set dir [lindex $argv 1]
set username [lindex $argv 2]
set passwd [lindex $argv 3]
do_ssh_login $host $dir $username $passwd
restore.sh
#!/usr/bin/expect -f
set DEST /mnt
set timeout 5
proc do_ssh_login {host dir username pass} {
    global DEST
    set timeout_case 0
    set done 1
        
    if { [string index $dir end] != "/" } {
        spawn rsync -azP -e ssh $username@$host:$dir/ $DEST
    } else {
        spawn rsync -azP -e ssh $username@$host:$dir $DEST
    }
    send_user "connecting to $host\n"
    while {$done} {
        expect {
            "*No route to host" {
                send_user "No route to host!\n"
                close
                exit 101
            }
            "*(yes/no)?" {send "yes\n"}
            "?assword:"  {send "$pass\n"}
            "*Permission denied*" {
                send_user "password incorrect!\n"
                close
                exit 102
            }
            "*Connection refused*" {
                send_user "connection refused!\n"
                close
                exit 103
            }
            "*speedup is*" {
                send_user "restore complete!\n"
                close
                exit 100
            }
        }
    }
}
if {$argc != 4} {
    puts stderr "Usage: ./restore.sh host dir username password\n"
    exit 104
}
set host [lindex $argv 0]
set dir [lindex $argv 1]
set username [lindex $argv 2]
set passwd [lindex $argv 3]
do_ssh_login $host $dir $username $passwd


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/87077/showart_2057005.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP