一、Shell 案例
1. 案例——系统备份脚本
- Tar工具全备、增量备份网站,Shell脚本 实现自动打包备份。
#!/bin/bash
SOURCE_DIR=(
$*
)
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=$?
if
[ -z "$*" ];then
echo -e "\033[32mUsage:\nPlease Enter Your Backup Files or Directories\n--------------------------------------------\n\nUsage: { $0 /boot /etc}\033[0m"
exit
fi
if
[ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then
mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY
echo -e "\033[32mThe $TARGET_DIR Created Successfully !\033[0m"
fi
Full_Backup()
{
if
[ "$WEEK" -eq "7" ];then
rm -rf $TARGET_DIR/snapshot
cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES ${SOURCE_DIR[@]}
[ "$CODE" == "0" ]&&echo -e "--------------------------------------------\n\033[32mThese Full_Backup System Files Backup Successfully !\033[0m"
fi
}
Add_Backup()
{
if
[ $WEEK -ne "7" ];then
cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $A_NAME$FILES ${SOURCE_DIR[@]}
[ "$CODE" == "0" ]&&echo -e "-----------------------------------------\n\033[32mThese Add_Backup System Files $TARGET_DIR/$YEAR/$MONTH/$DAY/${YEAR}_$A_NAME$FILES Backup Successfully !\033[0m"
fi
}
sleep 3
Full_Backup;Add_Backup
2. 案例——服务器信息自动收集
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:${ip_info}
echo HOSTNAME:$serv_info
echo CPU_INFO:${cpu_info1} X${cpu_info2}
echo DISK_INFO:$disk_info
echo MEM_INFO:$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 [ "$ensure" == "yes" -o "$ensure" == "y" -o "$ensure" == "Y" ];then
echo "--------------------------------------------"
echo -e '\033[31mmysql -uaudit -p123456 -D audit -e ''' "insert into audit_system values('','${ip_info}','$serv_info','${
cpu_info1} X${cpu_info2}','$disk_info','$mem_info','$load_info','$mark_info')" ''' \033[0m '
mysql -uroot -p123456 -D test -e "insert into audit_system values('','${ip_info}','$serv_info','${cpu_info1} X${cpu_info2}
','$disk_info','$mem_info','$load_info','$mark_info')"
else
echo "Please wait,exit......"
exit
fi
3. 案例——批量部署 lnmp 架构
Nginx_url=https://nginx.org/download/nginx-1.20.1.tar.gz
Nginx_prefix=/usr/local/nginx
Mysql_version=mysql-5.5.20.tar.gz
Mysql_dir=mysql-5.5.20
Mysql_url=https://downloads.mysql.com/archives/get/p/23/file/mysql-5.5.20.tar.gz
Mysql_prefix=/usr/local/mysql/
Php_version=php-7.2.10.tar.gz
Php_prefix=/usr/local/php-7.2.10/
function nginx_install() {
if [[ "$1" -eq "1" ]];then
if [ $? -eq 0 ];then
make && make install
fi
fi
}
function mysql_install() {
if [[ "$1" -eq "2" ]];then
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
echo -e "\033[32mThe $Mysql_dir Server Install Success !\033[0m"
else
echo -e "\033[32mThe $Mysql_dir Make or Make install ERROR,Please Check......"
exit 0
fi
/bin/cp support-files/my-small.cnf /etc/my.cnf
/bin/cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
fi
}
function php_install(){
if [[ "$1" -eq "3" ]];then
if [ $? -eq 0 ];then
make ZEND_EXTRA_LIBS='-liconv' && make install
if [[ "$1" -eq "3" ]];then
wget $Php_url && tar xf $Php_version && cd $Php_dir && yum install bxml2* bzip2* libcurl* libjpeg* libpng* freetype* gmp* libm
crypt* readline* libxslt* -y && ./configure --prefix=$Php_prefix --disable-fileinfo --enable-fpm --with-config-file-path=/etc --wi
-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --w
ith-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --enable-
mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --without-pear --enable-bcmath
if [ $? -eq 0 ];then
make ZEND_EXTRA_LIBS='-liconv' && make install
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32mThe $Php_version Server Install Success !\033[0m"
else
echo -e "\033[32mThe $Php_version Make or Make install ERROR,Please Check......"
exit 0
fi
fi
}
PS3="Please enter you select install menu:"
select i in nginx mysql php quit
do
"lnmp.sh" 113L, 3516C written
[root@web-server01~/script]
chkconfig --add mysqld
chkconfig mysqld on
fi
}
function php_install(){
if [[ "$1" -eq "3" ]];then
if [ $? -eq 0 ];then
make ZEND_EXTRA_LIBS='-liconv' && make install
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32mThe $Php_version Server Install Success !\033[0m"
else
echo -e "\033[32mThe $Php_version Make or Make install ERROR,Please Check......"
exit 0
fi
fi
}
PS3="Please enter you select install menu:"
select i in nginx mysql php quit
do
case $i in
nginx)
nginx_install 1
;;
mysql)
mysql_install 2
;;
php)
php_install 3
;;
quit)
exit
esac
done
4. 案例——MySQL 备份脚本
#!/bin/bash
username='root'
password='123456'
backup_dir='/data/backup/'
db_name_array="all"
current_time=$(date +%Y%m%d_%H%M%S)
filepath=$backup_dir$db_name'_'$current_time'.sql'
for db_name in ${db_name_array[*]}
do
mysqldump -u$username -p$password --all-databases | gzip > $backup_dir$db_name'_'$current_time'.sql.gz'
done
find $backup_dir -name "*.sql.gz" -atime +7 -exec rm -rf {} \;
- [Warning] Using a password on the command line interface can be insecure
vim /etc/mysql/my.cnf
[mysqldump]
user=root
password=123456
#!/bin/bash
db_name=test
backup_dir=/root/mysql/backup/
current_time=$(date +%Y-%m-%d_%H-%M-%S)
/usr/local/mysql/bin/mysqldump $db_name > $backup_dir$db_name'_'$current_time'.sql'
echo 'bk' = $?
find $backup_dir -name "*.sql" -atime +15 -exec rm -rf {} \;
echo 'rm' = $?
二、crontab 定时任务
crontab [-u username] [-l|-e|-r]
参数 |
描述 |
-u |
只有 root 才能进行这个任务,也即帮其他用户新建、删除 crontab 工作调度。 |
-e |
编辑 crontab 的工作内容。 |
-l |
查阅 crontab 的工作内容。 |
-r |
删除所有 crontab 的工作内容,若仅要删除一项,请用 -e 去编辑。 |
1. 设置定时任务
- 直接输入命令
crontab -e
- 编辑文件
/etc/crontab
crontab -e
0 */10 * * * /root/mysql_backup.sh
crontab -l
* * * * * /root/mysql/backup/mysql_backup.sh
* * * * * sleep 30; /root/mysql/backup/mysql_backup.sh
*/1 * * * * /root/mysql/backup/mysql_backup.sh
* * * * * /root/mysql/backup/mysql_backup.sh
0 */4 * * * /root/mysql/backup/mysql_backup.sh
@yearly /scripts/script.sh
@yearly /scripts/script.sh
@yearly /scripts/script.sh
@yearly /scripts/script.sh
@yearly /scripts/script.sh
@reboot /scripts/script.sh
0 10 * * * /root/mysql/backup/mysql_backup.sh
0 5,17 * * * /root/mysql/backup/mysql_backup.sh
0 4,17 * * sun,mon /root/mysql/backup/mysql_backup.sh
0 17 * * sun /root/mysql/backup/mysql_backup.sh
* * * jan,may,aug * /root/mysql/backup/mysql_backup.sh
0 17 * * sun,fri /root/mysql/backup/mysql_backup.sh
0 2 * * sun [ $(date +%d) -le 07 ] && /root/mysql/backup/mysql_backup.sh
* * * * * /scripts/script.sh; /scripts/scrit2.sh
vi /etc/crontab
2. 定时任务格式
2. 安装 crontab
service crond status
yum -y install vixie-cron
yum -y install crontabs
vixie-cron
软件包是 cron的主程序。
crontabs
软件包是用来 安装、卸装、或 列举用来驱动 cron 守护进程 的表格的程序。
service crond start
service crond stop
service crond restart
service crond reload
service crond status
chkconfig --level 345 crond on