1.1 每天晚上12点打包备份/etc目录到/tmp下面 脚本

1.命令

[root@oldboyedu50-lnb ~]# tar zcf  /tmp/etc-`date +%F`.tar.gz  /etc/

tar: Removing leading `/' from member names

[root@oldboyedu50-lnb ~]# ll /tmp/etc-2018-08-0*

-rw-r--r-- 1 root root 9734087 Aug  6 09:19 /tmp/etc-2018-08-06.tar.gz

-rw-r--r-- 1 root root 9734062 Aug  9 09:39 /tmp/etc-2018-08-09.tar.gz

2.书写脚本

[root@oldboyedu50-lnb ~]# cat /server/scripts/bak-etc.sh

tar zcf  /tmp/etc-`date +%F`.tar.gz  /etc/

 

[root@oldboyedu50-lnb ~]# sh /server/scripts/bak-etc.sh

tar: Removing leading `/' from member names

[root@oldboyedu50-lnb ~]# ll /tmp/

total 9512

-rw-r--r-- 1 root root 9734062 Aug  9 09:41 etc-2018-08-09.tar.gz

-rw-r--r-- 1 root root     434 Aug  8 11:42 name.log

3.书写定时任务-每分钟执行

[root@oldboyedu50-lnb ~]# crontab -l

##print name  oldboy at 2018xxxxx

#* * * * *  echo oldboy >>/tmp/name.log

 

#sync time   oldboy  at 2018xxxxx

*/2 * * * * /usr/sbin/ntpdate  ntp1.aliyun.com

 

#backup etc  oldboy at 2018xxxxxx

* * * * *  /bin/sh /server/scripts/bak-etc.sh

 

 

[root@oldboyedu50-lnb ~]# ll /tmp/

total 4

-rw-r--r-- 1 root root 434 Aug  8 11:42 name.log

4.检查并根据要求把定时任务时间修改过来

[root@oldboyedu50-lnb ~]# ll /tmp/

total 9512

-rw-r--r-- 1 root root 9734062 Aug  9 09:44 etc-2018-08-09.tar.gz

-rw-r--r-- 1 root root     434 Aug  8 11:42 name.log

[root@oldboyedu50-lnb ~]# crontab -l

##print name  oldboy at 2018xxxxx

#* * * * *  echo oldboy >>/tmp/name.log

 

#sync time   oldboy  at 2018xxxxx

*/2 * * * * /usr/sbin/ntpdate  ntp1.aliyun.com

 

#backup etc  oldboy at 2018xxxxxx

00 00 * * *  /bin/sh /server/scripts/bak-etc.sh

1.2 定时任务中-命令或脚本结果(正确及错误)定向到黑洞(>/dev/null 2>&1)或追加到文件中 >>/tmp/oldboy.txt 2>&1

>/dev/null 2>&1 

>>/tmp/oldboy.txt 2>&1

[root@oldboyedu50-lnb ~]# crontab -l

#print name  oldboy at 2018xxxxx

#* * * * *  echo oldboy    >>/tmp/name.log 2>&1

 

#sync time   oldboy  at 2018xxxxx

*/2 * * * * /usr/sbin/ntpdate  ntp1.aliyun.com   >/dev/null 2>&1

 

#backup etc  oldboy at 2018xxxxxx

00 00 * * *  /bin/sh /server/scripts/bak-etc.sh   >/dev/null 2>&1

 

#show time

* * * * *  date +\%F_\%T  >>/tmp/time.log  2>&1

You have new mail in /var/spool/mail/root

1.3 定时任务中没有追加到文件或定向到空

1.定时任务不断给你发送邮件

You have new mail in /var/spool/mail/root

2.邮件软件关闭 , 定时任务不断给你发送邮件 存放在邮件的临时目录 等待发送

inode用光了 定时任务书写不规范导致的

1.4 【企业案例】如果定时任务规则结尾不加>/dev/null 2>&1或者追加到文件中>>/tmp/oldboy 2>&1,很容易导致硬盘inode空间被占满,从而系统服务不正常。

如何删除大量小文件

echo {1..450000}.txt|xargs touch

ls *.txt |xargs rm

ls *.txt |xargs -n数字 rm

缩小范围一点点删除

ls 1*.txt |xargs rm

删除目录

记录好目录权限和所有者

1.5 每分钟显示当前系统的ip地址和系统的时间 追加到/tmp/ip.log

[root@oldboy50-01 ~]# vim /server/scripts/ip.sh

/sbin/ip a s eth0 |awk -F"[  /]+" 'NR==3 {print $3}'

/bin/date +%F_%T

[root@oldboy50-01 ~]# sh /server/scripts/ip.sh

10.0.0.200

2018-08-09_15:36:33

[root@oldboy50-01 ~]# cat /tmp/ip.log

10.0.0.200

2018-08-09_11:29:01

以+开头的行 表示执行过程

[root@oldboyedu50-lnb ~]# sh -x  /server/scripts/bak-etc.sh

+ cd /

++ date +%F

+ tar zcf /tmp/etc-2018-08-09.tar.gz etc/

不是以+开头的行 显示/输出

1.6 每两个小时打包备份 /etc/rc.local /etc/hosts /etc/fstab /etc/sysconfig 备份到/backup目录 脚本

[root@oldboy50-01 ~]# vim /server/scripts/bak-conf.sh

 

/bin/tar zchf /tmp/bak-`date +%F_%T`.tar.gz /etc/rc.local /etc/hosts /etc/fstab /etc/sysconfig

[root@oldboy50-01 ~]# sh //server/scripts/bak-conf.sh

/bin/tar: Removing leading `/' from member names

/bin/tar: Removing leading `/' from hard link targets

[root@oldboy50-01 ~]# crontab -e

00 */2  * * * /bin/sh /server/scripts/bak-conf.sh >/dev/null 2>&1

#backup ip oldboy at 20180809

[root@oldboy50-01 ~]# ll /tmp/

total 9788

-rw-r--r-- 1 root root   61148 Aug  9 12:11 bak-2018-08-09_12:11:01.tar.gz

-rw-r--r-- 1 root root   61148 Aug  9 12:23 bak-2018-08-09_12:23:01.tar.gz

-rw-r--r-- 1 root root   61148 Aug  9 12:24 bak-2018-08-09_12:24:01.tar.gz

-rw-r--r-- 1 root root   61148 Aug  9 15:39 bak-2018-08-09_15:39:36.tar.gz

1.7 每天晚上12点打包备份/etc/目录 备份到 /backup下面  备份到/backup/10.0.0.200

[root@oldboyedu50-lnb /backup]# ip=`hostname -I|awk '{print $1}'`

[root@oldboyedu50-lnb /backup]# echo $ip.log

10.0.0.200.log

 

[root@oldboy50-01 ~]# vim /server/scripts/bak-ip.sh

 

#path

ip=`hostname -I|awk -F' ' '{print $1}'`

 

#mkdir && tar

mkdir -p /backup/$ip

/bin/tar zchf /backup/$ip/etc-`date +%F_%T`-tar.gz /etc

 

[root@oldboy50-01 ~]# crontab -l

#backup ip oldboy at 20180809

00 00 * * * /bin/sh /server/scripts/bak-ip.sh >/dev/null 2>&1

 

[root@oldboy50-01 ~]# tree /backup/

/backup/

└── 10.0.0.200

    ├── etc-2018-08-09_12:48:01-tar.gz

    ├── etc-2018-08-09_12:49:01-tar.gz

    └── etc-2018-08-09-tar.gz

 

1 directory, 3 files