这个脚本的原型来自之前提到的db备份脚本,但做了一些改动略有不同。
这次的脚本支持逗号分隔的kv输入,具体输入文本格式如下
+------------------+--------------+----------------+
| folder full path | separator(,) | folder descript|
+------------------+--------------+----------------+
| /var/aaaaa/ | , | aaaa11111 |
+------------------+--------------+----------------+
发送到邮箱的标题命名:web_bak_${folder descript}_${data}
具体shell代码如下:
#!/bin/bash
#need 2 var
if (($# != 2)); then
echo "need input [Web Site Name Full Path in File] [mode]"
echo "mode -> 0, <Web Site Name Full Path in File> read!"
echo "mode -> 1, do Web Site bak!"
exit;
fi
if (($2 < 0 && $2 > 1)); then
echo "err mode!"
exit
fi
#filter some lines:
# 1. pure tab line
# 2. pure \r\n
# 3. pure space line
WebSiteFullPathInFilePath=$1
sed /^[[:space:]]*$/d $WebSiteFullPathInFilePath > $WebSiteFullPathInFilePath".tmp"
####################################################
#pre-info
#
# +----------+--------------+-------------+
# | fullpath | separator(,) | description |
# +----------+--------------+-------------+
#
####################################################
loop_count=0
bak_WebSiteFullPathInFilePath_target=1
bak_WebSiteDescriptionFilePath_target=1
echo "WebSiteFullPathInFilePath: "$WebSiteFullPathInFilePath" <<<>>> WebSiteFullPathInFilePath_tmp: "$WebSiteFullPathInFilePath".tmp"
echo ============ output all web site info $(date +"%Y%m%d") ==================
while read -r line
do
#echo $line
stringlen=${#line}
keyend=$(expr index $line ',')
key=${line:0:$keyend-1}
value=${line:$keyend:$stringlen-$keyend}
#echo "stringlen: "$stringlen" keyend: "$keyend" key: "$key" value: "$value
bak_WebSiteFullPathInFilePath_target[loop_count]=$key
bak_WebSiteDescriptionFilePath_target[loop_count]=$value
((loop_count++))
done < $WebSiteFullPathInFilePath".tmp"
i=0
for ((;i<$loop_count && $loop_count>0;i++))
do
echo "website name: "${bak_WebSiteDescriptionFilePath_target[$i]}
echo -e "\twebsite full path: "${bak_WebSiteFullPathInFilePath_target[$i]}"\n"
done
echo "total - "$i
echo ============ end ==================
if (($2 == 0)); then
echo "test over"
exit;
fi
if (($i<=0)); then
echo "don't have website full path!"
exit
fi
#do work
i=0
for ((;i<$loop_count && $loop_count>0;i++))
do
WebSiteFullPath=${bak_WebSiteFullPathInFilePath_target[$i]}
WebSiteDescription=${bak_WebSiteDescriptionFilePath_target[$i]}
BakName_tmp=website_bak_${WebSiteDescription}_$(date +"%Y%m%d")
#mkdir
mkdir /tmp/website_bak
mkdir /tmp/website_bak/${WebSiteDescription}
#del bak file before 3 days
rm -rf /tmp/website_bak/${WebSiteDescription}/website_bak_${WebSiteDescription}_$(date -d -3day +"%Y%m%d").tar.gz
#compact file
tar zcf /tmp/website_bak/${WebSiteDescription}/$BakName_tmp.tar.gz $WebSiteFullPath
#Send mail
mutt -s $BakName_tmp "email@163.com" -a /tmp/website_bak/${WebSiteDescription}/$BakName_tmp.tar.gz < /var/auto_script/emptytext.txt
done
同样作为定时任务,将命令执行放在crontab中
在创建自动任务之前需要安装的安装包:
cron
使用crontab来对当前用户的定时任务进行编辑
crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
crontab -l //列出当前(某个)用户cron服务的详细内容
crontab -r //删除当前(某个)用户的cron服务
crontab -e //编辑当前(某个)用户的cron服务
这里使用root创建的用户,所以直接在root用户下,执行crontab -e编辑好以后然后退出编辑器即可。
如果不放心还可以执行以下/etc/init.d/cron restart
编辑好以后的脚本如下
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
25 22 * * 4 /var/auto_script/db_bak/dbbak.sh /var/auto_script/db_bak/dbname.txt 1 >> /var/auto_script/db_bak/autoworker_log.txt
其中的25代表时间的25分钟
22代表时间的22点
4代表每周星期4
/var/auto_script/db_bak/dbbak.sh /var/auto_script/db_bak/dbname.txt 1 >> /var/auto_script/db_bak/autoworker_log.txt 则是命令行
对于cron自身的配置来说,他的配置放在/etc/crontab,内容如下:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
51 * * * * root cd / && run-parts --report /etc/cron.hourly
40 1 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
48 5 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
28 2 23 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
对于cron的配置来说,尽量不要去修改,网上有些文章说,如果要执行周期性任务,可以到/etc/cron.*目录下保存脚本,我这边看到的是,大部分都是系统类任务;最好不要去这样做。混淆了脚本的分类很麻烦
首先需要安装这几个包:
mutt、uuencode、sendmail
脚本如下:
#!/bin/bash
#need 2 var
if (($# != 2)); then
echo "need input [DBNamefile] [mode]"
echo "mode -> 0, test DBNamefile read!"
echo "mode -> 1, do DB bak!"
exit;
fi
if (($2 < 0 && $2 > 1)); then
echo "err mode!"
exit
fi
#filter some lines:
# 1. pure tab line
# 2. pure \r\n
# 3. pure space line
DBNameFilePath=$1
sed /^[[:space:]]*$/d $DBNameFilePath > $DBNameFilePath".tmp"
#pre-info
loop_count=0
bak_DBName_target=1
echo "DBNameFilePath: "$DBNameFilePath" <<<>>> DBNameFilePath_tmp: "$DBNameFilePath".tmp"
echo ============ output all db Name $(date +"%Y%m%d") ==================
while read -r line
do
#echo $line
bak_DBName_target[loop_count]=$line
((loop_count++))
done < $DBNameFilePath".tmp"
i=0
for ((;i<$loop_count && $loop_count>0;i++))
do
echo ${bak_DBName_target[$i]}
done
echo "total - "$i
echo ============ end ==================
if (($2 == 0)); then
echo "test over"
exit;
fi
if (($i<=0)); then
echo "empty db name!"
exit
fi
#do work
i=0
for ((;i<$loop_count && $loop_count>0;i++))
do
DBName_Tmp=${bak_DBName_target[$i]}
DataBakName_tmp=Data_bak_${DBName_Tmp}_$(date +"%Y%m%d")
#del db before 3 days
rm -rf /home/backup/Data_$(date -d -3day +"%Y%m%d").tar.gz
#expory mysql db
mysqldump -u root -ppassword $DBName_Tmp > /tmp/mysql_db_bak/$DataBakName_tmp.sql
#compact db file
tar zcf /tmp/mysql_db_bak/$DataBakName_tmp.tar.gz /tmp/mysql_db_bak/$DataBakName_tmp.sql
#del temporary sql
rm -rf /tmp/mysql_db_bak/$DataBakName_tmp.sql
#Send mail
mutt -s $DataBakName_tmp "email@163.com" -a /tmp/mysql_db_bak/$DataBakName_tmp.tar.gz < mailbody.txt
done
上面脚本的用法是:
./a.sh 1.txt 0
1.txt是保存要备份数据库名称的文件。