想问一个shell自动备份脚本的问题。

想问一个shell自动备份脚本的问题。,第1张

我们在修改一个文件的参数时,首先要vi这个文件名,然后我们可以通过:s来替换自己想替换的东西,但是整个过程都是自己改的,能不能通过编写一个脚本让系统自动的修改这个配置文件然后自己保存呢?例如我要修改ssconf这个文件中的ip=1011111 我怎么通过编写一个自己定义好的语句让系统自动vi并且修改成10222222然后自动保存,在执行下一个语句呢?请高手支招。谢谢大家的帮助。

正确的写法是:

这是我在网上CSDN,博客园看到基本全是错误的,还有人是随便转发的吧。

-exec rm {} ; 表示执行一段shell命令,exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{},一个空格和一个,最后是一个分号。

/dev/null 2>&1 把标准出错重定向到标准输出,然后扔到/DEV/NULL下面去。通俗的说,就是把所有标准输出和标准出错都扔到垃圾桶里面;其中的&表示让该命令在后台执行。

1、计划任务,每天1点备份

0 1 /data/backup/backup-mysqlsh;

2、backup-mysqlsh内容,将每天备份的内容打包

#!/bin/sh

ymd=`date +%Y%m%d`

cd /data/backup

mysqldump -uuser001 -p12345678 db001 > db001sql

tar zcf db001-${ymd}sqlgz db001sql

3、restoresh恢复数据

mysql -uuser001 -p12345678 -Ddb001 < db001sql

在ORACLE数据库中,RMAN备份的脚本非常多,下面介绍一例shell脚本如何通过RMAN备份,以及FTP上传RMAN备份文件以及归档日志文件的脚本。

fullbacksh 里面调用RMAN命令做数据库备份,它使用的cmdfile为/home/oracle/backup/bin/fullbackrcv,同时在/home/oracle/backup/logs目录下生成日志文件。

1: [oracle@DB-Server bin]$ more fullbacksh

2:

3: #!/bin/bash

4:

5: export ORACLE_BASE=/u01/app/oracle

6:

7: export ORACLE_SID=gps

8:

9: ORACLE_HOME=$ORACLE_BASE/product/1020/db_1; export ORACLE_HOME

10:

11: TMP=/tmp; export TMP

12:

13: TMPDIR=$TMP; export TMPDIR

14:

15: PATH=/usr/sbin:$PATH; export PATH

16:

17: PATH=$ORACLE_HOME/bin:$PATH; export PATH

18:

19: LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH

20:

21: CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;

22:

23: export CLASSPATH

24:

25: TODAY=`date +%Y_%m_%d`

26:

27: rman nocatalog target / cmdfile /home/oracle/backup/bin/fullbackrcv log /home/oracle/backup/logs/fullbackup_$TODAYlog

28:

29: /home/oracle/backup/bin/ftpbackupsh

30:

fullbackrcv文件非常简单, 如下所示:

1: [oracle@DB-Server bin]$ more /home/oracle/backup/bin/fullbackrcv

2:

3: run{

4:

5: allocate channel c4 type disk;

6:

7: backup as compressed backupset

8:

9: skip inaccessible

10:

11: tag fullbackupwitharchivelog

12:

13: (database);

14:

15: backup current controlfile;

16:

17: backup spfile;

18:

19: sql "alter system archive log current";

20:

21: delete noprompt obsolete;

22:

23: release channel c4;

24:

25: }

26:

RMAN生成的备份文件,需要通过FTP上传到FTP服务器,一则数据库服务器没有这么多空间存储多天的备份,二则是出于容灾、数据安全需要。

下面脚本中FTP服务器,用户名密码均使用xxx替代,在实际环境中,使用具体的信息替代即可。

1: [oracle@DB-Server bin]$ more ftpbackupsh

2:

3: #!/bin/sh、

4:

5: rm -f /home/oracle/netrc

6:

7: date_yesterday=`date -d'yesterday' +%Y_%m_%d`

8:

9: date_today=`date +%Y_%m_%d`

10:

11: echo "default login xxxx password xxxxxx" >> /home/oracle/netrc

12:

13: echo "macdef init" >> /home/oracle/netrc

14:

15: echo "binary" >> /home/oracle/netrc

16:

17: echo "cd archivelog" >> /home/oracle/netrc

18:

19: echo "mkdir $date_yesterday" >> /home/oracle/netrc

20:

21: echo "cd $date_yesterday" >> /home/oracle/netrc

22:

23: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_yesterday" >> /home/oracle/netrc

24:

25: echo "mput " >> /home/oracle/netrc

26:

27: echo "cd " >> /home/oracle/netrc

28:

29: echo "mkdir $date_today" >>/home/oracle/netrc

30:

31: echo "cd $date_today" >>/home/oracle/netrc

32:

33: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" >>/home/oracle/netrc

34:

35: echo "mput ">>/home/oracle/netrc

36:

37: echo "cd " >>/home/oracle/netrc

38:

39: echo "cd /backupset" >> /home/oracle/netrc

40:

41: echo "mkdir $date_today" >> /home/oracle/netrc

42:

43: echo "cd $date_today" >> /home/oracle/netrc

44:

45: echo "lcd /u04/flash_recovery_area/gps/backupset/$date_today" >> /home/oracle/netrc

46:

47: echo "mput " >> /home/oracle/netrc

48:

49: echo "cd " >> /home/oracle/netrc

50:

51: echo "cd /autobackup" >> /home/oracle/netrc

52:

53: echo "mkdir $date_today" >> /home/oracle/netrc

54:

55: echo "cd $date_today" >> /home/oracle/netrc

56:

57: echo "lcd /u04/flash_recovery_area/gps/autobackup/$date_today" >> /home/oracle/netrc

58:

59: echo "mput " >> /home/oracle/netrc

60:

61: echo "quit" >> /home/oracle/netrc

62:

63: echo "" >> /home/oracle/netrc

64:

65: chmod 600 /home/oracle/netrc

66:

67: ftp -i -v xxxxxxxxxxxx 8021 >>/home/oracle/backup/logs/ftp$date_todaylog 2>&1

68:

另外,关于归档日志也需要每隔2小时上传一次到FTP服务器,2小时上传一次归档日志的shell脚本如下所示:

1: [oracle@DB-Server bin]$ more ftp2hourssh

2:

3: #!/bin/sh

4:

5: rm -f /home/oracle/netrc

6:

7: date_yesterday=`date -d'yesterday' +%Y_%m_%d`

8:

9: date_today=`date +%Y_%m_%d`

10:

11: echo "default login xxxx password xxxx" >> /home/oracle/netrc

12:

13: echo "macdef init" >> /home/oracle/netrc

14:

15: echo "binary" >> /home/oracle/netrc

16:

17: echo "cd archivelog" >> /home/oracle/netrc

18:

19: echo "mkdir $date_today" >>/home/oracle/netrc

20:

21: echo "cd $date_today" >>/home/oracle/netrc

22:

23: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" >>/home/oracle/netrc

24:

25: echo "mput ">>/home/oracle/netrc

26:

27: echo "quit" >> /home/oracle/netrc

28:

29: echo "" >> /home/oracle/netrc

30:

31: chmod 600 /home/oracle/netrc

32:

33: ftp -i -v xxxxxxxxxxxx 8021 >>/home/oracle/backup/logs/ftp2hours$date_todaylog 2>&1

34:

最后需要将RMAN备份生成的日志文件,以及FTP上传备份文件以及归档日志的记录通过邮件形式发送给DBA或系统管理员,

1: [oracle@DB-Server bin]$ more chkbackandmailsh

2: #!/bin/bash

3: rm -f /home/oracle/backup/bin/sendmailpl

4: date_today=`date +%Y_%m_%d`

5: subject="Oracle Backup Alert Service on $date_today"

6: content="Dear colleagues,

7:

8: Attached please find the logs of xxx(xxxxxxxxxxxx) oracle database backup and transfer to FTP Server(xxxxxxxxxxxx), please

9: review the file and check whether the backup succeeded or not,and double check all backups have been dumped to tape, many tha

10: nks

11:

12:

13:

14:

15: Best regards

16: Oracle Alert Services

17:

18: "

19: file="/home/oracle/backup/logs/fullbackup_$date_todaylog,/home/oracle/backup/logs/ftp$date_todaylog"

20: echo "#!/usr/bin/perl" >> /home/oracle/backup/bin/sendmailpl

21: echo "use Mail::Sender;" >> /home/oracle/backup/bin/sendmailpl

22: echo "\$sender = new Mail::Sender {smtp => 'xxxxxxxxxxxx', from => 'xxxx@xxxcom'}; ">> /home/oracle/backup/bin/sendmai

23: lpl

24: echo "\$sender->MailFile({to => 'xxx@esquelcom',">> /home/oracle/backup/bin/sendmailpl

25: echo "cc=>'xxx@xxxcom,xxx@xxxcom,xxx@xxxcom'," >> /home/oracle/backup/b

26: in/sendmailpl

27: echo "subject => '$subject',">> /home/oracle/backup/bin/sendmailpl

28: echo "msg => '$content',">> /home/oracle/backup/bin/sendmailpl

29: echo "file => '$file'});">> /home/oracle/backup/bin/sendmailpl

30: perl /home/oracle/backup/bin/sendmailpl

最后在Crontab 作业里面配置调用这些shell脚本。例如如下所示,在1:01分执行fullbacksh ,每隔两个小时(例如0:50、2:50)执行一次ftp2hourssh, 在每天早上8:40执行chkbackandmailsh 发送fullbacksh 以及ftp2hoursh的执行日志记录。

把以前写的一个发你看看

脚本要求:

编写shell脚本,每天凌晨1点备份td_oa库,到mysql-back目录,并将备份文件压缩,压缩名称为备份的日期,只保留连续七天的备份文件

#!/bin/bash

/usr/local/mysql/bin/mysqldump

-uroot

-p123456

td_oa

>

td_oasql

&>/dev/null

a=`/bin/date

+%Y-%m-%d`

#在这里加上时间,压缩的时候加上时间,会失败。

echo

$a

cd

/gxl

/bin/tar

-czf

$atar

/fjp/td_oasql

b="$atar"

cp

-p

$b

/root/fjp

for

c

in

`find

/root/fjp

-ctime

+7`

do

/bin/rm

-rf

$c

done

自己根据自己的要求修改!

以上就是关于想问一个shell自动备份脚本的问题。全部的内容,包括:想问一个shell自动备份脚本的问题。、shell脚本删除mysql过期备份踩过的坑、shell怎么对mysql数据库进行自动备份等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9285293.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存