centos7 编写shell 脚本

2021-01-29  本文已影响0人  李小二的倔强

chmod a+x test.sh

编写脚本

自动备份数据并且打成.tar.gz

[root@localhost cct]# backdata.sh
[root@localhost cct]# chmod a+x backdata.sh 
[root@localhost cct]# vi backdata.sh

#获取当前日期
current_time=$(date +%Y%m%d)

# 判断目录是不是已经存在,如果不存在则创建,存在则输出“dir exist”
dirname=backup

echo "the dir name is $dirname"
if [ ! -d $dirname  ];then
  mkdir $dirname
else
  echo dir exist
fi

# backup data
./mongodb/bin/mongodump -h 127.0.0.1:27017 -d cold-chain-tracing -o $dirname

cd $dirname/cold-chain-tracing

tar -zcvf ../$current_time.tar.gz *

cd ../../

rm -rf $dirname/cold-chain-tracing

设置定时任务

1、安装 crontabs服务并设置开机自启:

  $ yum install crontabs
  $ systemctl enable crond
  $ systemctl start crond

2、配置定时规则

    $ vim /etc/crontab

在配置文件中配置你的定时执行规则

    * 6 * * * root /home/backup/showdoc/backup.sh

3、保存生效

    $ crontab /etc/crontab

4、查看任务

    $ crontab -l
上一篇 下一篇

猜你喜欢

热点阅读