6.15 Shell编程之IP匹配及磁盘邮件告警
在Linux中,我们经常需要备份系统重要文件,例如/etc、/boot分区、重要网站数据等等,如果每天都完整备份的话,会占用很大的空间,那我们改如何来备份呢?
这里采用如下方法来备份:
每周日进行完整备份,其余每天为增量备份。
那使用什么命令来备份呢,我们使用tar命令:
全备份:
tar -g /tmp/snapshot -czvf /tmp/2014_full_system_data.tar.gz /data/sh/
增量备:
tar -g /tmp/snapshot -czvf /tmp/2014_add01_system_data.tar.gz /data/sh/
#!/bin/sh
#Automatic Backup Linux System Files
#Author wugk 2013-11-22
#Define Variable
SOURCE_DIR=(
∗
)
T
A
R
G
E
T
D
I
R
=
/
d
a
t
a
/
b
a
c
k
u
p
/
Y
E
A
R
=
‘
d
a
t
e
+
M
O
N
T
H
=
‘
d
a
t
e
+
D
A
Y
=
‘
d
a
t
e
+
W
E
E
K
=
‘
d
a
t
e
+
A
N
A
M
E
=
‘
d
a
t
e
+
F
I
L
E
S
=
s
y
s
t
e
m
b
a
c
k
u
p
.
t
g
z
C
O
D
E
=
* ) TARGET_DIR=/data/backup/ YEAR=`date +%Y` MONTH=`date +%m` DAY=`date +%d` WEEK=`date +%u` A_NAME=`date +%H%M` FILES=system_backup.tgz CODE=
∗)TARGETDIR=/data/backup/YEAR=‘date+MONTH=‘date+DAY=‘date+WEEK=‘date+ANAME=‘date+FILES=systembackup.tgzCODE=?
if
[ -z “$*” ];then
echo -e “\033[32mUsage:\nPlease Enter Your Backup Files or Directories\n--------------------------------------------\n\nUsage: { $0 /boot /etc}\033[0m”
exit
fi
#Determine Whether the Target Directory Exists
if
[ ! -d
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ];then
mkdir -p
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY
echo -e “\033[32mThe KaTeX parse error: Undefined control sequence: \0 at position 34: … Successfully !\̲0̲33[0m" fi #EXEC…WEEK” -eq “7” ];then
rm -rf $TARGET_DIR/snapshot
cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES
S
O
U
R
C
E
D
I
R
[
@
]
[
"
{SOURCE_DIR[@]} [ "
SOURCEDIR[@]["CODE" == “0” ]&&echo -e “--------------------------------------------\n\033[32mThese Full_Backup System Files Backup Successfully !\033[0m”
fi
}
#Perform incremental BACKUP Function Command
Add_Backup()
{
if
[ $WEEK -ne “7” ];then
cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;tar -g $TARGET_DIR/snapshot -czvf
A
N
A
M
E
A_NAME
ANAMEFILES
S
O
U
R
C
E
D
I
R
[
@
]
[
"
{SOURCE_DIR[@]} [ "
SOURCEDIR[@]["CODE" == “0” ]&&echo -e “-----------------------------------------\n\033[32mThese Add_Backup System Files
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY/KaTeX parse error: Expected group after '_' at position 7: {YEAR}_̲A_NAME$FILES Backup Successfully !\033[0m”
fi
}
sleep 3
Full_Backup;Add_Backup
6.16 Shell编程之系统硬件信息数据库收集
首先我们创建数据库表,格式如下:
CREATE TABLE audit_audit_system
(
id
int(11) NOT NULL AUTO_INCREMENT,
ip_info
varchar(50) NOT NULL,
serv_info
varchar(50) NOT NULL,
cpu_info
varchar(50) NOT NULL,
disk_info
varchar(50) NOT NULL,
mem_info
varchar(50) NOT NULL,
load_info
varchar(50) NOT NULL,
mark_info
varchar(50) NOT NULL,
PRIMARY KEY (id
),
UNIQUE KEY ip_info
(ip_info
),
UNIQUE KEY ip_info_2
(ip_info
)
);
然后编写脚本如下:
#!/bin/sh
#auto get system info
#author wugk 2014-08-29
echo -e “\033[34m \033[1m”
cat <<EOF
++++++++++++++++++++++++++++++++++++++++++++++
++++++++Welcome to use system Collect+++++++++
++++++++++++++++++++++++++++++++++++++++++++++
EOF
ip_info=ifconfig |grep "Bcast"|tail -1 |awk '{print $2}'|cut -d: -f 2
cpu_info1=cat /proc/cpuinfo |grep 'model name'|tail -1 |awk -F: '{print $2}'|sed 's/^ //g'|awk '{print $1,$3,$4,$NF}'
cpu_info2=cat /proc/cpuinfo |grep "physical id"|sort |uniq -c|wc -l
serv_info=hostname |tail -1
disk_info=fdisk -l|grep "Disk"|grep -v "identifier"|awk '{print $2,$3,$4}'|sed 's/,//g'
mem_info=free -m |grep "Mem"|awk '{print "Total",$1,$2"M"}'
load_info=uptime |awk '{print "Current Load: "$(NF-2)}'|sed 's/\,//g'
mark_info=‘BeiJing_IDC’
echo -e “\033[32m-------------------------------------------\033[1m”
echo IPADDR:
i
p
i
n
f
o
e
c
h
o
H
O
S
T
N
A
M
E
:
{ip_info} echo HOSTNAME:
ipinfoechoHOSTNAME:serv_info
echo CPU_INFO:
c
p
u
i
n
f
o
1
X
{cpu_info1} X
cpuinfo1X{cpu_info2}
echo DISK_INFO:
d
i
s
k
i
n
f
o
e
c
h
o
M
E
M
I
N
F
O
:
disk_info echo MEM_INFO:
diskinfoechoMEMINFO:mem_info
echo LOAD_INFO:$load_info
echo -e “\033[32m-------------------------------------------\033[0m”
echo -e -n “\033[36mYou want to write the data to the databases? \033[1m” ;read ensure
if [ “
e
n
s
u
r
e
"
=
=
"
y
e
s
"
−
o
"
ensure" == "yes" -o "
ensure"=="yes"−o"ensure” == “y” -o “KaTeX parse error: Undefined control sequence: \0 at position 87: …--" echo -e '\̲0̲33[31mmysql -ua…{ip_info}‘,‘
s
e
r
v
i
n
f
o
′
,
′
serv_info','
servinfo′,′{cpu_info1} X
c
p
u
i
n
f
o
2
′
,
′
{cpu_info2}','
cpuinfo2′,′disk_info’,‘
m
e
m
i
n
f
o
′
,
′
mem_info','
meminfo′,′load_info’,’$mark_info’)” ‘’’ \033[0m ’
else
echo “wait exit…”
exit
fi
读取数据库信息:
mysql -uroot -p123 -e ‘use wugk1 ;select * from audit_audit_system;’|sed ‘s/-//g’|grep -v “id”
这样,我们可以把数据库的内容在shell脚本里面调用出来。
Shell编程之磁盘报警高级脚本
Mail -s “邮件主题” -c”抄送地址” -b “密送地址” -f 发送人邮件地址 –F 发件人姓名 < 要发送的邮件内容
6.17 全备和增量备份Linux系统脚本编写
#!/bin/sh
#Automatic Backup Linux System Files
#Author wugk 2013-11-22
#Define Variable
SOURCE_DIR=(
∗
)
T
A
R
G
E
T
D
I
R
=
/
d
a
t
a
/
b
a
c
k
u
p
/
Y
E
A
R
=
‘
d
a
t
e
+
M
O
N
T
H
=
‘
d
a
t
e
+
D
A
Y
=
‘
d
a
t
e
+
W
E
E
K
=
‘
d
a
t
e
+
F
I
L
E
S
=
s
y
s
t
e
m
b
a
c
k
u
p
.
t
g
z
C
O
D
E
=
* ) TARGET_DIR=/data/backup/ YEAR=`date +%Y` MONTH=`date +%m` DAY=`date +%d` WEEK=`date +%u` FILES=system_backup.tgz CODE=
∗)TARGETDIR=/data/backup/YEAR=‘date+MONTH=‘date+DAY=‘date+WEEK=‘date+FILES=systembackup.tgzCODE=?
if
[ -z “$*” ];then
echo -e “Please Enter Your Backup Files or Directories\n--------------------------------------------\nExample $0 /boot /etc …”
exit
fi
#Determine Whether the Target Directory Exists
if
[ ! -d
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ];then
mkdir -p
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY
echo “This KaTeX parse error: Expected 'EOF', got '#' at position 39: …essfully !" fi #̲EXEC Full_Backu…WEEK” -eq “7” ];then
rm -rf $TARGET_DIR/snapshot
cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES echo ${SOURCE_DIR[@]}
[ “$CODE” == “0” ]&&echo -e “--------------------------------------------\nThese Full_Backup System Files Backup Successfully !”
fi
}
#Perform incremental BACKUP Function Command
Add_Backup()
{
cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;
if
[ -f
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY/$FILES ];then
read -p "These $FILES Already Exists, overwrite confirmation yes or no ? : " SURE
if [ $SURE == “no” -o $SURE == “n” ];then
sleep 1 ;exit 0
fi
#Add_Backup Files System
if
[ $WEEK -ne “7” ];then
cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;tar -g $TARGET_DIR/snapshot -czvf $KaTeX parse error: Expected group after '_' at position 1: _̲FILES echo ${SOURCE_DIR[@]}
[ “$CODE” == “0” ]&&echo -e “-----------------------------------------\nThese Add_Backup System Files Backup Successfully !”
fi
else
if
[ $WEEK -ne “7” ];then
cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES echo ${SOURCE_DIR[@]}
[ “$CODE” == “0” ]&&echo -e “-------------------------------------------\nThese Add_Backup System Files Backup Successfully !”
fi
fi
}
Full_Backup;Add_Backup