Linux自动化脚本

MySQL自动备份数据到另一台服务器

注:
1.用到了SSH秘钥信任(scp免密码)
2.编写好shell脚本后使用Linux的自带的定时任务crontab来实现调用,将每天运行的结果都追加输出到一个文件里(这里需要人工确认没问题后清理文件,或输出时选择覆盖文件)。
3.因数据库是使用的docker启动,这里的MySQLdump使用的是docker命令导出。

SSH秘钥信任(scp免密码)

#1.在当前用户目录下创建.ssh文件夹(如有忽略此步骤)
mkdir -p ~/.ssh
#2.文件夹授权
chmod 700 ~/.ssh
#3.在~/.ssh目录生成密钥文件(此步骤会生成rsa公私钥 id_rsa、id_rsa.pub)
cd ~/.ssh
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
#4.将id_rsa.pub里的内容复制到authorized_keys里,并传入到另一台服务器上
cat id_rsa.pub > authorized_keys
scp authorized_keys root@172.16.0.126:/root/.ssh/
#5.修改刚刚传给另一台服务器的文件的权限
chmod 600 ~/.ssh/authorized_keys
#6.重新使用scp命令测试就不会提示输入密码了~

定时任务:crontab -e

1 6 * * * /bin/sh /home/estsh/init-system/shell/mysql-backup.sh >> /home/estsh/init-system/shell/mysql-backup.log

数据库备份脚本:mysql-backup.sh

#!/bin/sh
today=`date +%Y-%m-%d_%H%M%S`
#126服务器存放路径
destPath=/home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup-123

echo -e "\r\n进入到当前目录开始备份"
cd /home/estsh/init-system/shell/
echo `date +%Y-%m-%d_%H:%M:%S`" 开始备份impp_i3_andon"
docker exec dbmaster mysqldump -uroot -pestsh123 impp_i3_andon > impp_i3_andon.sql
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 开始备份impp_i3_block_form"
docker exec dbmaster mysqldump -uroot -pestsh123 impp_i3_block_form > impp_i3_block_form.sql
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 开始备份impp_i3_block_softswith"
docker exec dbmaster mysqldump -uroot -pestsh123 impp_i3_block_softswitch > impp_i3_block_softswitch.sql
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 开始备份impp_i3_core"
docker exec dbmaster mysqldump -uroot -pestsh123 impp_i3_core > impp_i3_core.sql
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 开始备份impp_i3_mes"
docker exec dbmaster mysqldump -uroot -pestsh123 impp_i3_mes > impp_i3_mes.sql
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 开始备份impp_i3_mes_pcn"
docker exec dbmaster mysqldump -uroot -pestsh123 impp_i3_mes_pcn > impp_i3_mes_pcn.sql
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 开始备份impp_i3_schedule"
docker exec dbmaster mysqldump -uroot -pestsh123 impp_i3_schedule > impp_i3_schedule.sql
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 开始备份impp_i3_wms"
docker exec dbmaster mysqldump -uroot -pestsh123 impp_i3_wms > impp_i3_wms.sql
sleep 1


echo `date +%Y-%m-%d_%H:%M:%S`" 开始压缩sql文件"
zip impp_i3_andon-$today.zip impp_i3_andon.sql
zip impp_i3_block_form-$today.zip impp_i3_block_form.sql
zip impp_i3_block_softswitch-$today.zip impp_i3_block_softswitch.sql
zip impp_i3_core-$today.zip impp_i3_core.sql
zip impp_i3_mes-$today.zip impp_i3_mes.sql
zip impp_i3_mes_pcn-$today.zip impp_i3_mes_pcn.sql
zip impp_i3_schedule-$today.zip impp_i3_schedule.sql
zip impp_i3_wms-$today.zip impp_i3_wms.sql
sleep 1


echo `date +%Y-%m-%d_%H:%M:%S`" 开始内网传输压缩包到126服务器"
scp impp_i3_andon-$today.zip root@172.16.0.126:$destPath/impp_i3_andon/
scp impp_i3_block_form-$today.zip root@172.16.0.126:$destPath/impp_i3_block_form/
scp impp_i3_block_softswitch-$today.zip root@172.16.0.126:$destPath/impp_i3_block_softswitch/
scp impp_i3_core-$today.zip root@172.16.0.126:$destPath/impp_i3_core/
scp impp_i3_mes-$today.zip root@172.16.0.126:$destPath/impp_i3_mes/
scp impp_i3_mes_pcn-$today.zip root@172.16.0.126:$destPath/impp_i3_mes_pcn/
scp impp_i3_schedule-$today.zip root@172.16.0.126:$destPath/impp_i3_schedule/
scp impp_i3_wms-$today.zip root@172.16.0.126:$destPath/impp_i3_wms/
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 开始删除源sql文件"
rm -rf impp_i3_andon-$today.zip impp_i3_andon.sql
rm -rf impp_i3_block_form-$today.zip impp_i3_block_form.sql
rm -rf impp_i3_block_softswitch-$today.zip impp_i3_block_softswitch.sql
rm -rf impp_i3_core-$today.zip impp_i3_core.sql
rm -rf impp_i3_mes-$today.zip impp_i3_mes.sql
rm -rf impp_i3_mes_pcn-$today.zip impp_i3_mes_pcn.sql
rm -rf impp_i3_schedule-$today.zip impp_i3_schedule.sql
rm -rf impp_i3_wms-$today.zip impp_i3_wms.sql
sleep 1

echo `date +%Y-%m-%d_%H:%M:%S`" 定时任务全部执行完毕..."

删除历史文件:del-old-file.sh

#!/bin/sh
jarBackupDays=15
mysqlBackupDays=7

#echo `date +%Y-%m-%d_%H:%M:%S`" 开始执行删除历史jar文件操作,历史文件保留"$jarBackupDays"天"
#find /home/qmestest/estsh/estsh-i3plus-server/app/i3plus-mes/jar/backup/ -mtime +$jarBackupDays -name 'i3plus-mes-apiservice-*' -exec rm -rf {} \;
#echo "i3plus-mes历史jar文件删除完成"

#echo `date +%Y-%m-%d_%H:%M:%S`" 开始删除i3plus-mes-pcn历史jar文件"
#find /home/qmestest/estsh/estsh-i3plus-server/app/i3plus-mes-pcn/jar/backup/ -mtime +$jarBackupDays -name 'i3plus-mes-pcn-apiservice-*' -exec rm -rf {} \;
#echo "i3plus-mes-pcn历史jar文件删除完成"


echo `date +%Y-%m-%d_%H:%M:%S`" 开始删除MySQL备份文件操作,历史文件保留"$mysqlBackupDays"天"
find /home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup/impp_i3_andon/ -mtime +$mysqlBackupDays -name 'impp_i3_andon-*.zip' -exec rm -rf {} \;
find /home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup/impp_i3_block_form/ -mtime +$mysqlBackupDays -name 'impp_i3_block_form-*.zip' -exec rm -rf {} \;
find /home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup/impp_i3_block_softswith/ -mtime +$mysqlBackupDays -name 'impp_i3_block_softswith-*.zip' -exec rm -rf {} \;
find /home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup/impp_i3_core/ -mtime +$mysqlBackupDays -name 'impp_i3_core-*.zip' -exec rm -rf {} \;
find /home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup/impp_i3_mes/ -mtime +$mysqlBackupDays -name 'impp_i3_mes-*.zip' -exec rm -rf {} \;
find /home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup/impp_i3_mes_pcn/ -mtime +$mysqlBackupDays -name 'impp_i3_mes_pcn-*.zip' -exec rm -rf {} \;
find /home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup/impp_i3_schedule/ -mtime +$mysqlBackupDays -name 'impp_i3_schedule-*.zip' -exec rm -rf {} \;
find /home/qmestest/estsh/estsh-i3plus-server/app/mysql-backup/impp_i3_wms/ -mtime +$mysqlBackupDays -name 'impp_i3_wms-*.zip' -exec rm -rf {} \;
echo "MySQL备份历史文件删除完成"

echo `date +%Y-%m-%d_%H:%M:%S`" 执行删除历史文件任务完成,任务结束..."

Java发布脚本

andon自动发布脚本:andon.sh

#!/bin/bash
today=`date +%Y-%m-%d_%H%M%S`
jarPath=/home/qmestest/estsh/estsh-i3plus-server/app/i3plus-andon/jar
fileName=i3plus-andon-1.0-TEST-SNAPSHOT
todayFile=$fileName-$today.jar

echo "开始检测jar包是否正常"
if [ ! -e /home/qmestest/Downloads/i3plus-andon/i3plus-andon-1.0-TEST-SNAPSHOT.jar ]; then
        echo "i3plus-andon-1.0-TEST-SNAPSHOT.jar 文件不存在,退出程序"
        exit
else
        echo "已找到文件,继续执行"
fi
sleep 2


fileCount=`ls /home/qmestest/Downloads/i3plus-andon | wc -l`
if [ $fileCount = 1 ]; then
        echo "当前i3plus-andon文件夹下只有1个文件,继续操作..."
else
        echo "i3plus-andon文件夹内的文件个数错误,目前有"$fileCount"个文件。执行程序只能有1个文件"
        echo "程序退出"
        exit
fi
sleep 2


echo "开始移动jar包"
mv /home/qmestest/Downloads/i3plus-andon/$fileName*.jar $jarPath/backup/$todayFile
cd $jarPath
sleep 2

echo "开始建立软连接"
ln --symbolic --force backup/$todayFile i3plus-andon-1.0-SNAPSHOT.jar
sleep 2

echo "开始重启服务"
systemctl restart i3plus-andon
sleep 2

echo "重启完成,查看启动日志命令如下"
sleep 2
echo "tail -f /home/qmestest/estsh/estsh-i3plus-server/app/i3plus-andon/logs/andon.log"
impp-pc自动发布脚本:impp-pc.sh
#!/bin/bash
today=`date +%Y-%m-%d_%H%M%S`
path=/home/qmestest/estsh/estsh-i3plus-server/app/webfront/impp-pc

echo "开始检测zip文件是否正常"
if [ ! -e /home/qmestest/Downloads/impp-pc/pc-*.zip ]; then
        echo "pc-*.zip 文件不存在,退出程序"
        exit
else
        echo "已找到文件,继续执行"
fi
sleep 2


fileCount=`ls /home/qmestest/Downloads/impp-pc | wc -l`
if [ $fileCount = 1 ]; then
        echo "当前impp-pc文件夹下只有1个文件,继续操作..."
else
        echo "impp-pc文件夹内的文件个数错误,目前有"$fileCount"个文件。执行程序只能有1个文件"
        echo "程序退出"
        exit
fi
sleep 2


fileSize=`du -s /home/qmestest/Downloads/impp-pc | awk '{print $1}'`
if [ $fileSize -lt 42000 ]; then
        echo "pc-*.zip文件大小错误,zip文件应大于44M,实际大小为"$fileSize"K"
        echo "程序退出"
        exit
else
        echo "pc-*.zip文件大小正常,继续操作..."
fi      
sleep 2


echo "开始创建备份文件夹【"$today"】"
cd $path/backup
mkdir $today
sleep 2

echo "开始将impp-pc目录下文件迁移到备份"
cd $path
mv favicon.ico index.html static backup/$today
sleep 2

echo "开始移动最新压缩包到指定路径"
mv /home/qmestest/Downloads/impp-pc/pc-*.zip $path
sleep 2

echo "开始解压"
sleep 2
unzip pc-*.zip
mv pc-*/* .
sleep 2

echo "开始删除压缩包文件"
rm -rf pc-*
sleep 2
echo "impp-pc更新完成..."
mes自动发布脚本:mes.sh
#!/bin/bash
today=`date +%Y-%m-%d_%H%M%S`
jarPath=/home/qmestest/estsh/estsh-i3plus-server/app/i3plus-mes/jar
fileName=i3plus-mes-apiservice-1.0-TEST-SNAPSHOT
todayFile=$fileName-$today.jar

echo "开始检测jar包是否正常"
if [ ! -e /home/qmestest/Downloads/i3plus-mes/i3plus-mes-apiservice-1.0-TEST-SNAPSHOT.jar ]; then
        echo "i3plus-mes-apiservice-1.0-TEST-SNAPSHOT.jar 文件不存在,退出程序"
        exit
else
        echo "已找到文件,继续执行"
fi
sleep 2


fileCount=`ls /home/qmestest/Downloads/i3plus-mes | wc -l`
if [ $fileCount = 1 ]; then
        echo "当前i3plus-mes文件夹下只有1个文件,继续操作..."
else
        echo "i3plus-mes文件夹内的文件个数错误,目前有"$fileCount"个文件。执行程序只能有1个文件"
        echo "程序退出"
        exit
fi
sleep 2


echo "开始移动jar包"
mv /home/qmestest/Downloads/i3plus-mes/$fileName*.jar $jarPath/backup/$todayFile
cd $jarPath
sleep 2

echo "开始建立软连接"
ln --symbolic --force backup/$todayFile i3plus-mes-apiservice-1.0-SNAPSHOT.jar
sleep 2

echo "开始重启服务"
systemctl restart i3plus-mes
sleep 2

echo "重启完成,查看启动日志命令如下"
sleep 2
echo "tail -f /home/qmestest/estsh/estsh-i3plus-server/app/i3plus-mes/logs/i3mes.log"

mes自动发布脚本:mes.sh

#! /bin/bash
today=`date +%Y-%m-%d_%H%M%S`
jarPath=/home/qmestest/estsh/estsh-i3plus-server/app/i3plus-mes/jar
fileName=i3plus-mes-apiservice-1.0-TEST-SNAPSHOT
todayFile=$fileName-$today.jar

echo "开始检测jar包是否正常"
if [ ! -e /home/qmestest/Downloads/i3plus-mes/i3plus-mes-apiservice-1.0-TEST-SNAPSHOT.jar ]; then
        echo "i3plus-mes-apiservice-1.0-TEST-SNAPSHOT.jar 文件不存在,退出程序"
        exit
else
        echo "已找到文件,继续执行"
fi
sleep 2


fileCount=`ls /home/qmestest/Downloads/i3plus-mes | wc -l`
if [ $fileCount = 1 ]; then
	echo "当前i3plus-mes文件夹下只有1个文件,继续操作..."
else
	echo "i3plus-mes文件夹内的文件个数错误,目前有"$fileCount"个文件。执行程序只能有1个文件"
	echo "程序退出"
	exit
fi
sleep 2


echo "开始移动jar包"
mv /home/qmestest/Downloads/i3plus-mes/$fileName*.jar $jarPath/backup/$todayFile
cd $jarPath
sleep 2

echo "开始建立软连接"
ln --symbolic --force backup/$todayFile i3plus-mes-apiservice-1.0-SNAPSHOT.jar
sleep 2

echo "开始重启服务"
systemctl restart i3plus-mes
sleep 2

echo "重启完成,查看启动日志命令如下"
sleep 2
echo "tail -f /home/qmestest/estsh/estsh-i3plus-server/app/i3plus-mes/logs/i3mes.log"

pcn自动发布脚本:pcn.sh

#!/bin/bash
today=`date +%Y-%m-%d_%H%M%S`
jarPath=/home/qmestest/estsh/estsh-i3plus-server/app/i3plus-mes-pcn/jar
fileName=i3plus-mes-pcn-apiservice-1.0-TEST-SNAPSHOT
todayFile=$fileName-$today.jar

echo "开始检测jar包是否正常"
if [ ! -e /home/qmestest/Downloads/i3plus-mes-pcn/i3plus-mes-pcn-apiservice-1.0-TEST-SNAPSHOT.jar ]; then
        echo "i3plus-mes-pcn-apiservice-1.0-TEST-SNAPSHOT.jar 文件不存在,退出程序"
        exit
else
        echo "已找到文件,继续执行"
fi
sleep 2


fileCount=`ls /home/qmestest/Downloads/i3plus-mes-pcn | wc -l`
if [ $fileCount = 1 ]; then
        echo "当前i3plus-mes-pcn文件夹下只有1个文件,继续操作..."
else
        echo "i3plus-mes-pcn文件夹内的文件个数错误,目前有"$fileCount"个文件。执行程序只能有1个文件"
        echo "程序退出"
        exit
fi
sleep 2


echo "开始移动jar包"
mv /home/qmestest/Downloads/i3plus-mes-pcn/$fileName*.jar $jarPath/backup/$todayFile
cd $jarPath
sleep 2

echo "开始建立软连接"
ln --symbolic --force backup/$todayFile i3plus-mes-pcn-apiservice-1.0-SNAPSHOT.jar
sleep 2

echo "开始重启服务"
systemctl restart i3plus-mes-pcn
sleep 2

echo "重启完成,查看启动日志命令如下"
sleep 2
echo "tail -f /home/qmestest/estsh/estsh-i3plus-server/app/i3plus-mes-pcn/logs/i3mes-pcn.log"
pcn-client自动发布脚本:pcn-client.sh
#! /bin/bash
today=`date +%Y-%m-%d_%H%M%S`
path=/home/qmestest/estsh/estsh-i3plus-server/app/webfront/impp-mes-pcn

echo "开始检测zip文件是否正常"
if [ ! -e /home/qmestest/Downloads/pcn-client/pcn-*.zip ]; then
        echo "pcn-*.zip 文件不存在,退出程序"
        exit
else
        echo "已找到文件,继续执行"
fi
sleep 2


fileCount=`ls /home/qmestest/Downloads/pcn-client | wc -l`
if [ $fileCount = 1 ]; then
        echo "当前pcn-client文件夹下只有1个文件,继续操作..."
else
        echo "pcn-client文件夹内的文件个数错误,目前有"$fileCount"个文件。执行程序只能有1个文件"
        echo "程序退出"
        exit
fi
sleep 2

fileSize=`du -s /home/qmestest/Downloads/pcn-client | awk '{print $1}'`
if [ $fileSize -lt 21000 ]; then
        echo "pcn-*.zip文件大小错误,zip文件应大于21M,实际大小为"$fileSize"K"
        echo "程序退出"
        exit
else
        echo "pcn-*.zip文件大小正常,继续操作..."
fi      
sleep 2


echo "开始创建备份文件夹【"$today"】"
cd $path/backup
mkdir $today
sleep 2

echo "开始将impp-mes-pcn目录下文件迁移到备份"
cd $path
mv index.html static backup/$today
sleep 2

echo "开始移动最新压缩包到指定路径"
mv /home/qmestest/Downloads/pcn-client/pcn-*.zip $path
sleep 2

echo "开始解压"
sleep 2
unzip pcn-*.zip
mv pcn-*/* .
sleep 2

echo "开始删除压缩包文件"
rm -rf pcn-*
sleep 2
echo "impp-pcn更新完成..."

前端发布脚本

impp-pc自动发布脚本:impp-pc.sh

#! /bin/bash
today=`date +%Y-%m-%d_%H%M%S`
path=/home/qmestest/estsh/estsh-i3plus-server/app/webfront/impp-pc

echo "开始检测zip文件是否正常"
if [ ! -e /home/qmestest/Downloads/impp-pc/pc-*.zip ]; then
        echo "pc-*.zip 文件不存在,退出程序"
        exit
else
        echo "已找到文件,继续执行"
fi
sleep 2


fileCount=`ls /home/qmestest/Downloads/impp-pc | wc -l`
if [ $fileCount = 1 ]; then
	echo "当前impp-pc文件夹下只有1个文件,继续操作..."
else
	echo "impp-pc文件夹内的文件个数错误,目前有"$fileCount"个文件。执行程序只能有1个文件"
	echo "程序退出"
	exit
fi
sleep 2


fileSize=`du -s /home/qmestest/Downloads/impp-pc | awk '{print $1}'`
if [ $fileSize -lt 42000 ]; then
	echo "pc-*.zip文件大小错误,zip文件应大于44M,实际大小为"$fileSize"K"
	echo "程序退出"
	exit
else
	echo "pc-*.zip文件大小正常,继续操作..."
fi	
sleep 2


echo "开始创建备份文件夹【"$today"】"
cd $path/backup
mkdir $today
sleep 2

echo "开始将impp-pc目录下文件迁移到备份"
cd $path
mv favicon.ico index.html static backup/$today
sleep 2

echo "开始移动最新压缩包到指定路径"
mv /home/qmestest/Downloads/impp-pc/pc-*.zip $path
sleep 2

echo "开始解压"
sleep 2
unzip pc-*.zip
mv pc-*/* .
sleep 2

echo "开始删除压缩包文件"
rm -rf pc-*
sleep 2
echo "impp-pc更新完成..."

pcn-client自动发布脚本:pcn-client.sh

#! /bin/bash
today=`date +%Y-%m-%d_%H%M%S`
path=/home/qmestest/estsh/estsh-i3plus-server/app/webfront/impp-mes-pcn

echo "开始检测zip文件是否正常"
if [ ! -e /home/qmestest/Downloads/pcn-client/pcn-*.zip ]; then
        echo "pcn-*.zip 文件不存在,退出程序"
        exit
else
        echo "已找到文件,继续执行"
fi
sleep 2


fileCount=`ls /home/qmestest/Downloads/pcn-client | wc -l`
if [ $fileCount = 1 ]; then
	echo "当前pcn-client文件夹下只有1个文件,继续操作..."
else
	echo "pcn-client文件夹内的文件个数错误,目前有"$fileCount"个文件。执行程序只能有1个文件"
	echo "程序退出"
	exit
fi
sleep 2

fileSize=`du -s /home/qmestest/Downloads/pcn-client | awk '{print $1}'`
if [ $fileSize -lt 21000 ]; then
	echo "pcn-*.zip文件大小错误,zip文件应大于21M,实际大小为"$fileSize"K"
	echo "程序退出"
	exit
else
	echo "pcn-*.zip文件大小正常,继续操作..."
fi	
sleep 2


echo "开始创建备份文件夹【"$today"】"
cd $path/backup
mkdir $today
sleep 2

echo "开始将impp-mes-pcn目录下文件迁移到备份"
cd $path
mv index.html static backup/$today
sleep 2

echo "开始移动最新压缩包到指定路径"
mv /home/qmestest/Downloads/pcn-client/pcn-*.zip $path
sleep 2

echo "开始解压"
sleep 2
unzip pcn-*.zip
mv pcn-*/* .
sleep 2

echo "开始删除压缩包文件"
rm -rf pcn-*
sleep 2
echo "impp-pcn更新完成..."

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值