saltstack 发布 service 服务
如果是注册的服务发布:
salt -L '172.16.3.39' state.sls service.deploy
目录结构:
/home/salt/servername
├── deploy.sls
└── files
└── servername
server1.sls 配置示例:
# cat server1.sls
# 备份 声明变量
{% set time = salt['cmd.run']('D:Scriptgetinfo.py gettime') %} {% set service_dirname = 'Service' %} {% set service_name = 'Server1' %}
# 或者自设置批处理备份方式 backup_service: cmd.run: - name: D:ackup ar.bat
# 窗口服务 除了 net stop service 还可以直接杀死进程 stop_service: cmd.run: - name: taskkill /im Server1.exe /f - require: - cmd: backup_service
# 服务间歇 stop_sleep: cmd.run: - name: D:\Script\sleep.vbs - require: - cmd: stop_service # 服务发布 deploy_service: file.recurse: - name: D:\{{service_dirname}} - source: - salt://Service/Server1 - require: - cmd: stop_sleep # 服务启动 start_service: cmd.run: - name: net start {{service_name}} - require: - file: deploy_service
同样的发布指令:
salt -L '172.16.3.39' state.sls service.server1
相关脚本参考:
getinfo.py
#coding=utf-8
import os
import time
import sys
def getTime(*vars):
date = time.strftime("%Y%m%d", time.localtime(time.time()))
return date
def getLastBackup(*vars):
if not os.path.exists(vars[0]):
print '%r: 备份路径不存在' % vars[0]
sys.exit(1)
try:
os.chdir(vars[0])
except OSError, e:
print e
sys.exit(1)
else:
lastbackup = sorted(os.listdir(os.curdir)).pop()
return lastbackup
def usage():
print 'Usage: python getinfo.py [ gettime | getbackup <service>]'
funcdict = {'gettime': getTime, 'getbackup': getLastBackup}
def main():
if len(sys.argv) < 2:
usage()
sys.exit(1)
option = sys.argv[1:]
if option[0] in funcdict.keys():
print funcdict[option[0]](*option[1:])
else:
usage()
if __name__ == '__main__':
main()
压缩备份 rar.bat
set "today=%Date:~3,4%%Date:~8,2%%Date:~11,2%"
md D:\backup\%today%
"C:Program Files (x86)WinRARWinRAR.exe" a D:ackup\%today%DcApi_%today%.rar -x D:Serviceserver1Logs D:Serviceserver1
sleep.vbs
WScript.sleep 5000