1.crontab -e
00 01 * * * /data/backup/backup_root.sh #每天凌晨1点备份
2.文件
backup.sh
#!/bin/bash
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_2
export ORACLE_SID=orcl11g
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
PATH=$PATH:/usr/sbin:$ORACLE_HOME/bin
rq=`date -d "now" +%Y-%m-%d`
cd /data/backup
expdp mportalyl/yl123456@10.3.4.127 directory=dump dumpfile=/data/backup/exp_$rq.dmp schemas=mportal logfile=/data/backup/log_$rq.log
yesterday ='date -d "yesterday" +%Y-%m-%d'
rm -rf /data/backup/exp_$yesterday.dmp
rm -rf /data/backup/log_$yesterday.log
backup_root.sh
#!/bin/bash
su - oracle -c "cd /data/backup;./backup.sh"
3.设置开机自启动
vi /etc/crontab
00 01 * * * /data/backup/backup_root.sh #每天凌晨1点备份
4.启动定时任务的命令
systemctl start crond.service
systemctl status crond.service
systemctl stop crond.service
systemctl restart crond.service
5.给文件授权
chmod +x /data/backup/backup.sh
6.如何定时任务报错,有问题,可能是配置文件中的格式问题,换行符
sed -i 's/\r$//' /data/backup/backup.sh
sed -i 's/\r$//' /data/backup/backup_root.sh