linux中mysql备份shell脚本代码 & 相关自动化脚本
第一步:在你的linux服务器中定义备份目录:
mkdir /var/lib/mysqlbackup cd /var/lib/mysqlbackup
第二步:下面是最重要的一步了,就是写定时备份脚本。
vi dbbackup.sh
代码文件如下
#!/bin/sh # mysql data backup script # # use mysqldump --help,get more detail. # BakDir=/root/back/ysqlbackup LogFile=/root/back/mysql/mysqlbak.log DATE=`date +%Y%m%d%H%M%S` echo " " >> $LogFile echo " " >> $LogFile echo "-------------------------------------------" >> $LogFile echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile echo "--------------------------" >> $LogFile cd $BakDir DumpFile=$DATE.sql GZDumpFile=$DATE.sql.tar.gz /usr/local/bin/mysqldump -u user -pname --database -h 127.0.0.1 db> $DumpFile echo "Dump Done" >> $LogFile tar czvf $GZDumpFile $DumpFile >> $LogFile 2>&1 echo "[$GZDumpFile]Backup Success!" >> $LogFile rm -f $DumpFile #cd $BakDir/daily cd $BakDir echo "Backup Done!" echo "please Check $BakDir Directory!" echo "copy it to your local disk or ftp to somewhere !!!" find $BakDir -ctime +30 -exec rm {} ; echo "delete file over 30 days"
友情提示:其中user,name和db请根据自己的情况修改
保存退出,然后把这个文件赋予可执行的权限:
#chmod 777 mysqlautobackup.sh
用crontab定时执行备份脚本代码:
crontab -e
若每天下午3点20备份,添加如下代码,
代码:
20 15 * * * /var/lib/mysqlbackup/dbbackup.sh
这样就搞定了,每天会定时帮你备份mysql数据库了。
linux下监视进程 崩溃挂掉后自动重启的shell脚本
如何保证服务一直运行?如何保证即使服务挂掉了也能自动重启?在写服务程序时经常会碰到这样的问题。在Linux系统中,强大的shell就可以很灵活的处理这样的事务。
下面的shell通过一个while-do循环,用ps -ef|grep 检查loader进程是否正在运行,如果没有运行,则启动,这样就保证了崩溃挂掉的进程重新被及时启动。
必须注意两点:
1、ps |grep 一个进程时必须加上其路劲,否则容易grep到错误的结果;
2、必须用 -v 从结果中去除grep命令自身,否则结果非空。
#!/bin/sh #===================== #YuanHui.HE #khler@163.com #===================== while : do echo "Current DIR is " $PWD stillRunning=$(ps -ef |grep "$PWD/loader" |grep -v "grep") if [ "$stillRunning" ] ; then echo "TWS service was already started by another way" echo "Kill it and then startup by this shell, other wise this shell will loop out this message annoyingly" kill -9 $pidof $PWD/loader else echo "TWS service was not started" echo "Starting service ..." $PWD/loader echo "TWS service was exited!" fi sleep 10 done
如果启动此shell时发现进程已经存在,说明以别的方式启动了进程而不是此shell,那么它会持续提醒找到进程,解决办法是,要么只用此shell启动服务,要么一经发现以其他方式启动的服务即kill掉,上面的语句就是这么干的:
kill -9 $pidof $PWD/loader
判断文件是否存在的shell脚本代码
实现代码一、
#!/bin/sh # 判断文件是否存在 # link:www.aspku.com # date:2013/2/28 myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 if [ ! -x "$myPath"]; then mkdir "$myPath" fi # 这里的-d 参数判断$myPath是否存在 if [ ! -d "$myPath"]; then mkdir "$myPath" fi # 这里的-f参数判断$myFile是否存在 if [ ! -f "$myFile" ]; then touch "$myFile" fi # 其他参数还有-n,-n是判断一个变量是否是否有值 if [ ! -n "$myVar" ]; then echo "$myVar is empty" exit 0 fi # 两个变量判断是否相等 if [ "$var1" = "$var2" ]; then echo '$var1 eq $var2' else echo '$var1 not eq $var2' fi
实现代码二、
#shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文件,目录是否存在或者具有权限 folder="/var/www/" file="/var/www/log" # -x 参数判断 $folder 是否存在并且是否具有可执行权限 if [ ! -x "$folder"]; then mkdir "$folder" fi # -d 参数判断 $folder 是否存在 if [ ! -d "$folder"]; then mkdir "$folder" fi # -f 参数判断 $file 是否存在 if [ ! -f "$file" ]; then touch "$file" fi # -n 判断一个变量是否有值 if [ ! -n "$var" ]; then echo "$var is empty" exit 0 fi # 判断两个变量是否相等 if [ "$var1" = "$var2" ]; then echo '$var1 eq $var2' else echo '$var1 not eq $var2' fi
-f 和-e的区别
Conditional Logic on Files
-a file exists.
-b file exists and is a block special file.
-c file exists and is a character special file.
-d file exists and is a directory.
-e file exists (just the same as -a).
-f file exists and is a regular file.
-g file exists and has its setgid(2) bit set.
-G file exists and has the same group ID as this process.
-k file exists and has its sticky bit set.
-L file exists and is a symbolic link.
-n string length is not zero.
-o Named option is set on.
-O file exists and is owned by the user ID of this process.
-p file exists and is a first in, first out (FIFO) special file or
named pipe.
-r file exists and is readable by the current process.
-s file exists and has a size greater than zero.
-S file exists and is a socket.
-t file descriptor number fildes is open and associated with a
terminal device.
-u file exists and has its setuid(2) bit set.
-w file exists and is writable by the current process.
-x file exists and is executable by the current process.
-z string length is zero.
是用 -s 还是用 -f 这个区别是很大的!
在指定目录查找指定后缀文件的shell脚本代码
#!bin/sh # 在指定位置查找指定后缀的文件,包括子目录 # 用法: # findf $1 $2 # 第一个参数为后缀 # 查找指定后缀的文件并打印出来 # link:www.aspku.com # date:2013/2/26 f() { list=`find $2|grep "/.$1/>"` for i in $list do echo $i done } # 打印用法 print() { echo "用法:" echo "$1 /$1 /$2" echo "第一个参数为指定的后缀名,如'h'" echo "第二个参数为指定的目录,如果省略此参数则默认为当前目录" exit -1 } # 在当前目录查找 f1() { f "$1" "*" } # 在指定的目录查找 f2() { cd $2 f "$1" "*" } if [ "$#" -lt "1" ] then echo "给定的参数太少,最少需要一个参数." print "$0" fi if [ "$#" -gt "2" ] then echo "给定的参数太多,最多需要二个参数." print "$0" fi if [ "$#" -eq "1" ] then f1 $1 exit 0 fi if [ "$#" -eq 2 ] then f2 $1 $2 exit 0 fi