Chinaunix首页 | 论坛 | 博客
  • 博客访问: 142601
  • 博文数量: 24
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 291
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 09:36
文章分类

全部博文(24)

文章存档

2010年(4)

2009年(13)

2008年(7)

我的朋友

分类: LINUX

2009-09-21 17:15:52

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

阅读(1639) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~