运维常用

1、在/var/log下拷贝小于100K的文件到/tmp下,如果有失败的发送邮件 用shell/perl/python来实现。
脚本代码
#!/bin/bash
dir=/var/log
tmp=/root/Desktop/
line=`find $dir -type f -size -100 |wc -l `
#line=`ls -l $dir |awk '$5 < 10000 {print $NF}' |wc -l  `
for ((i=1;i<=$line;i++))
do
file=`find $dir -type f -size -100 |head -n $i|tail -n 1`
#file=`ls -l $dir |awk '$5 < 10000 {print $NF}' |head -n $i | tail -n 1`
cp $file $tmp
   if [ $? -eq 1 ] ; then
      echo "copy faild"
      #mail -s " $i ,copy fail" [email protected]
   fi
done
 
 
 
二、如何将本地 80 端口的请求转发到 8080 端口,当前主机 IP 192.168.2.1
iptables -t nat -A PREROUTIGN -d 192.168.2.1 -p tcp --dport 80 -j DNAT --to 192.168.2.1:8080
 
 
三、用tar打包压缩upload目录,但是不打包以.log结尾的文件
 
tar zcvf upload.tar.gz upload/ --exclude=*.log
 
四、mysql备份恢复,以test数据库为例。
不带用户名
[root@mail ~]# mysqldump test > test.sql
[root@mail ~]# mysql < test.sql
带用户名
[ root@mail ~]# mysqldump -uroot -p123456 test > test.sql
[root@mail ~]# mysql  -uroot -p123456 < test.sql
 五、
有个文件如下:
http://a.domain.com/1.html
http://b.domain.com/1.html
http://c.domain.com/1.html
http://a.domain.com/2.html
http://b.domain.com/2.html
http://a.domain.com/3.html
要求:得到主机名(和域名),并统计哪个网址出现的次数,并排序。可以 shell C
得到的结果应该是 :
3 a.domain.com
2 b.domain.com
1 c.domain.com
 
[root@mail ~]# cut -d/ -f 3 file |sort -n |uniq -c
      3 a.domain.com
      2 b.domain.com
      1 c.domain.com
 
[root@mail ~]# awk -F/ '{print $3}' a  |sort -n |uniq -c
      3 a.domain.com
      2 b.domain.com
      1 c.domain.com
 
六、如何让history显示历史命令的时间
编辑/etc/profile文件
添加 HISTTIMEFORMAT=`date +%"F  %T "`
          export HISTTIMEFORMAT
          source /etc/profile
 
七、实现rm删除文件实现回收站的功能。
1、脚本方式,比较麻烦
#!/bin/bash
unalias rm
mkdir ~/.Trash &>/dev/null
for i in $*
do
time=`date +%F%T`
 mv $i ~/.Trash/$i.$time 2>/dev/null
done
 
把文件保存为rm,然后将文件放在/bin目录下,将原有的重命名。
 
2、在/etc/bashrc 文件中添加
rm() {
 alias rm='rm -i'
 unalias rm
 mkdir ~/.Trash &>/dev/null
 for i in $*
 do
 mv $i  ~/.Trash/$i.$RANDOM  &>/dev/null
 done
 }
 

你可能感兴趣的:(职场,运维,休闲)