---
title: "Linux生产服务器Shell脚本分享"
author: "bwhzhl"
date: "2010-10-26T11:18:19Z"
tags: []
categories: ["操作系统"]
slug: "linux-shell"
draft: false
lastmod: "2010-10-26T11:18:19Z"
id: 794142
url: "https://permiss.iteye.com/blog/794142"
---

**MySQL的热备份脚本**

#!/bin/bash  
PATH=/usr/local/sbin:/usr/bin:/bin

\# The Directory of Backup  
BACKDIR=/usr/mysql\_backup  
\# The Password of MySQL  
ROOTPASS=password

\# Remake the Directory of Backup  
rm -rf $BACKDIR  
mkdir -p $BACKDIR

\# Get the Name of Database  
DBLIST=\`ls -p /var/lib/mysql | grep / | tr -d /\`  
\# 偷瞄了下我同事写脚本的方式，发现他直接将DBLIST一个个手动敲上去了  
\# 数据库少的情况是可以的，如果数据库是成百以上呢，呵呵

\# Backup with Database  
for dbname in $DBLIST  
do  
mysqlhotcopy $dbname -u root -p $ROOTPASS $BACKDIR | logger -t mysqlhotcopy  
done

 

**FTP同步服务器**

FTPOLDDATE=\`date +%Y-%m-%d -d '-60 days'\`  
#在FTP定义60天间的日期变量，是为了在FTP端保存60天的数据，逾期删除。

HOST=192.168.4.199  
FTP\_USERNAME=db  
FTP\_PASSWORD=password

cd ${BACKDIR}/${DATE}

ftp -i -n -v &lt;&lt; !  
open ${HOST}  
user ${FTP\_USERNAME} ${FTP\_PASSWORD}  
bin  
cd ${FTPOLDDATE}  
mdelete *  
cd ..  
rmdir ${FTPOLDDATE}  
mkdir ${DATE}  
cd ${DATE}  
mput *  
bye  
!

 

 

 **Keepalived的监控切换脚本**

#!/bin/bash  
while  :  
do  
nginxpid=\`ps -C nginx --no-header | wc -l\`  
if \[ $nginxpid -eq 0 ];then  
/usr/local/nginx/sbin/nginx  
sleep 5  
  if \[ $nginxpid -eq 0 ];then  
  /etc/init.d/keepalived stop  
  fi  
fi  
sleep 5   
done

 

**批量生成生成帐户脚本**

#!/bin/bash  
for name in tom jerry joe jane  
do  
      useradd $name  
      echo redhat | passwd --stdin $name  
done