Welcome

首页 / 数据库 / SQLServer / perl脚本备份还原sql server

perl脚本备份还原sql server2014-03-08 csdn博客 ocpyang写了一个perl脚本灵活来备份或还原。

1.将config.txt保存到c盘根目录

username:sa

password:passwd123!

根据实际情况修改该文件内容

2.执 行bcp.pl

#!C:Perl64in

#通过BCP备份和还原SQLSERVER指定的表

use 5.014;

#加载用户和密码配置文 件

my $username ;

my $passwd;

##获取用户名

my $dir="c:";

$dir=$dir."/";

my $filename="config.txt";

my $keysword="username:";

my $count="50";

my $begincount=index($keysword,":");

my ($file);

open(FILEH,$dir.$filename);

while(my $test=<FILEH>){

  if((index($test,$keysword))>=0){

my $test1=substr ($test,$begincount+1,$count);

   #print "$test1";

chomp($username=$test1);

  }

}

close(FILEH);

##获取密码

my $dir="c:";

$dir=$dir."/";

my $filename="config.txt";

my $keysword="password:";

my $count="50";

my $begincount=index($keysword,":");

my ($file);

open(FILEH,$dir.$filename);

while(my $test=<FILEH>){

  if((index($test,$keysword))>=0){

my $test2=substr ($test,$begincount+1,$count);

   #print "$test2";

chomp($passwd=$test2);

  }

}

close(FILEH);

print "----------------------------------------------------------------- -- ";

print "请首先检查用户和密码配置文件config.txt保存路径是否为 $dir$filename ";

print "----------------------------------------------------------------- -- ";

print "重要提示:备份表操作请输入0;还原表操作请输入1 ";

print "请输入操作代码:";

chomp(my $inp=<STDIN>);

print "你输入的操作代码为$inp ";

#判断输入为0即备份操作

if ($inp eq 0){

print "服务器IP:(不输入默认为 localhost)";

chomp(my $a = <STDIN>);

if ($a=="") {

$a="localhost";

}else{

print "你输入的服务器IP为:$a ";

}

print "用户名:";

chomp(my $b = <STDIN>);

print "你输入的用户名为:$b ";

if($b eq $username) {

 print "用户通过! ";

} else {

 print "对不起,用户名错误 ";

 exit;

}

print "用户密码: ";

chomp (my $readPsw = <STDIN>);

if($readPsw eq $passwd) {

 print "密码正确! ";

} else {

 print "对不起,密码错误 ";

 exit;

}

print "重要提示:备份表的表名格式为:test.dbo.t1 ";

print "备份的表:";

chomp(my $d = <STDIN>);

print "你输入的备份的表:$d ";

print "保存路径:";

chomp(my $e = <STDIN>);

print "你输入的保存路径:$e ";

if (-e $e){

print "$e已经存在!是否删除?(y删除;n不删除) ";

chomp(my $i=<STDIN>);

given ($i)

{

when ("y") {

unlink $e;

print "开始备份表! ";

system (" bcp $d out $e -c -t "," -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");

}

when  ("n")

{

print "-----------备份已经终止----------- ";

print "指定文件存在,请重新输入文件名! ";

}

default {print "只能输入y或n! ";}

}

}

else{

{

system (" bcp $d out $e -c -t "," -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");

}

}

}

#判断输入 为1还原操作

elsif($inp eq 1){

print "服务器IP:(不输入默认为localhost) ";

chomp(my $a = <STDIN>);

if ($a=="") {

$a="localhost";

}else{

print "你输入的服务器IP为:$a ";

}

print "用户名:";

chomp(my $b = <STDIN>);

print "你输入的用户名为:$b ";

if($b eq $username) {

 print "用户通过! ";

} else {

 print "对不起,用户名错误 ";

 exit;

}

print "用户密码: ";

chomp (my $readPsw = <STDIN>);

if($readPsw eq $passwd) {

 print "密码正确! ";

} else {

 print "对不起,密码错误 ";

 exit;

}

print "需要还原的表:";

chomp(my $d = <STDIN>);

print "你输入的备份的表:$d ";

print "备份保存路径:";

chomp(my $e = <STDIN>);

if (-e $e) {

print "你输入的备份保存路径:$e ";

system (" bcp $d in $e -c -t "," -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");

}

else {

print "对不起,指定的备份文件$e不存在,请核实! ";

}

}

else{

print "请参考重要提示! ";

}

system "pause";