造了一个轮子,用于服务器更新和备份文件
2013-08-13 16:50
260 查看
用python 造了一个轮子:
在服务器上自动拷贝文件到指定目录,在覆盖前自动备份旧的文件;
这个脚本写得很简单只是检验文件是否一致,未做新旧文件的判断,如有其他需求请自行修改;
同时备份发布文件到指定目录;
发布完文件后自动删除发布目录;
如要过滤发布文件的类型,请自行添加一行代码;
这个脚本需要一个配置文件,配置的格式如下:
[publish]
source=d:\update
target=d:\website
bakup=d:\backup
publish=d:\publish
在服务器上自动拷贝文件到指定目录,在覆盖前自动备份旧的文件;
这个脚本写得很简单只是检验文件是否一致,未做新旧文件的判断,如有其他需求请自行修改;
同时备份发布文件到指定目录;
发布完文件后自动删除发布目录;
如要过滤发布文件的类型,请自行添加一行代码;
这个脚本需要一个配置文件,配置的格式如下:
[publish]
source=d:\update
target=d:\website
bakup=d:\backup
publish=d:\publish
import ConfigParser,io,sys,os ''' Date: 2013.08.09 Author: Sean Pu Ver: 0.2 Declare: Maybe some bugs can not find by local test,after u update new files ,u find it; At that time,u need recover the old files immediately. This script help u update the new files and backup the old files. This script can be use in many OS that support python. Before run this script,pls config the file :update.conf ''' import filecmp import datetime import shutil #fortmat print data def Pl (str): n=datetime.datetime.now() print n.strftime('%Y-%m-%d %H:%M:%S '),str #delete file and dir,the follow function copy from internet def delete_file_folder(src): if os.path.isfile(src): try: os.remove(src) except: pass elif os.path.isdir(src): for item in os.listdir(src): itemsrc=os.path.join(src,item) delete_file_folder(itemsrc) try: os.rmdir(src) except: pass def ChkF(spath,tpath,bpath,ppath): if os.path.exists(spath)==False: Pl('there is no dir:'+spath+',pls check it.') return sf=os.listdir(spath) for f in sf: s_child_path=spath+"\\"+f t_child_path=tpath+"\\"+f b_child_path=bpath+"\\"+f p_child_path=ppath+"\\"+f if os.path.isdir(s_child_path): if os.path.exists(t_child_path)==False: os.makedirs(t_child_path) if os.path.exists(p_child_path)==False: os.makedirs(p_child_path) ChkF(s_child_path,t_child_path,b_child_path,p_child_path) else: if os.path.exists(t_child_path): #if file not same,backup and copy file if filecmp.cmp(s_child_path,t_child_path)==False: Pl("bak:"+bpath) #Pl(b_child_path) if os.path.exists(bpath)==False:os.makedirs(bpath) #bakup file first ,then copy new file to target dir shutil.copy(t_child_path,b_child_path) shutil.copy(s_child_path,t_child_path) shutil.copy(s_child_path,p_child_path) else: Pl(b_child_path) shutil.copy(s_child_path,t_child_path) shutil.copy(s_child_path,p_child_path) #shutil.rmtree(s_child_path) delete_file_folder(s_child_path) #Get config file config=ConfigParser.RawConfigParser() config.read("update.conf") sourceDir=config.get('publish','source') targetDir=config.get('publish','target') bakupDir=config.get('publish','bakup') pubDir=config.get('publish','publish') n=datetime.datetime.now() n=n.strftime('%Y%m%d') bakupDir=bakupDir+"\\"+n pubDir=pubDir+"\\"+n if os.path.exists(bakupDir)==False:os.mkdir(bakupDir) if os.path.exists(pubDir)==False: os.mkdir(pubDir) print sourceDir,targetDir,bakupDir,pubDir Pl ('begin update file and dir in : '+sourceDir+'') if os.path.exists(sourceDir)==False: Pl('there is no dir:'+sourceDir+',pls check it.') exit() ChkF(sourceDir,targetDir,bakupDir,pubDir) exit();
相关文章推荐
- [mcpc]minecraft1.4.7服务器找不到一个好的简单插件用于禁用物品
- 自己写的一个批处理文件,自动更新svn文件 并自动用ant编译 调用jboss的run启动服务器
- C#一个简单下载程序实例(可用于更新)
- 一个脚本从git上pull 并更新到服务器
- 最近遇到一个类似淘宝购买商品加减数量然后还得实时更新数据到服务器的问题
- mysql5.6服务器突然掉电后一个mysql实例无法启动的修复(更新中可能最后没法解决)
- 易语言-循环读取配置文件,用于更新类的一个更新思路和想法
- 一个电脑双显示屏,用于画板子(有待更新)
- 执行更新版本获取服务器连接必须开启一个新的线程
- CNBlogsDottext部署到服务器上后遇到一个缓存不更新的问题
- 发布一个用于和GridView或DataGrid表格相关联的查找搜索服务器控件
- 操作必须使用一个可更新的查询;HTTP 500 - 内部服务器错误; 您未被授权查看该页;您不具备使用所提供的凭据查看该目录或页的权限;HTTP 错误 401.3 - 访问被资源 ACL 拒绝
- java简单实现一个日志工具类(用于服务器调试)
- 操作必须使用一个可更新的查询;HTTP 500 - 内部服务器错误; 您未被授权查看该页;您不具备使用所提供的凭据查看该目录或页的权限;HTTP 错误 401.3 - 访问被资源 ACL 拒绝
- 造轮子:搭建一个简单的nodejs服务器,从零开始搭建一个自用网站(0)
- 在JAVA中利用SOCKET创建一个用于收发报文的长连接服务器
- 在公司局域网建立一个 REDHAT LINUX更新服务器
- 用于Web主机的SQL服务器数据库发布工具包更新
- lua中配置luasocket,并且做成一个socket服务器,主要用于(D'Fusion Studio)中
- rvm实现一个系统同时运行多个ruby版本项目(用于解决一个服务器部署多个ruby版本项目的实现)