返回

php服务器版本更新工具up2server

发布时间:2022-10-13 11:19:53 275
# python# git# svn# 服务器# 工具

为什么要做这个工具

  之前做php 开发的时候,每次版本更新,要把修改的文件同步到服务器,都要小心翼翼,如果漏掉了文件,那就完蛋了,KPI,奖金什么的都没了。

所以写了这个工具。代码在github  ​​ https://github.com/foxswang/up2server​​

1. 复制 获取要上传的文件 运行

python get_upload_file.py

  get_upload_file.py的代码如下:

#!/usr/bin/python
import os
import os.path
import shutil
import time, datetime
import string

fromdir = "source_folder"
todir = "backup"
filename = 'filelist.txt'

#
def cpFile(srcPath, destPath):
shutil.copy(srcPath,destPath)
#shutil.copytree(srcPath,destPath)
#
def copyFiles(sourceDir, targetDir):
if sourceDir.find(".svn") > 0:
return
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
targetFile = os.path.join(targetDir, file)
if os.path.isfile(sourceFile):
if not os.path.exists(targetDir):
os.makedirs(targetDir)
if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
open(targetFile, "wb").write(open(sourceFile, "rb").read())
if os.path.isdir(sourceFile):
First_Directory = False
copyFiles(sourceFile, targetFile)
#
if __name__ == "__main__":
text_file = open("./"+filename ,"r")
#current_path = os.getcwd()
current_path = os.getcwd()
all_line = 0
folder_count = 0;
print("current_path "+ current_path )
if not os.path.exists(os.path.join(current_path, todir) ):
os.makedirs(os.path.join(current_path, todir) )
for line in text_file:
line=line.strip('\n')
srcFile = current_path + line
dstFile = current_path + '/' + todir + line
targetDir = os.path.dirname(dstFile)
if not os.path.isfile(srcFile):
folder_count = folder_count+1
continue;
print (srcFile)
print (dstFile)
if not os.path.exists(targetDir):
os.makedirs(targetDir)
print("cp "+ srcFile +" to " + dstFile)
cpFile(srcFile,dstFile)
all_line = all_line + 1

print ("copy success file : %d folder: %d" % (all_line,folder_count))

 

2. 备份服务器的文件,方便升级失败后进行恢复:

python backup_server_files.py

3. 将第一步复制的文件,上传到服务器

  服务器版本升级so easy

 

文件和一些参数说明

1. 确保系统安装了python(windows 和linux 都支持)

2 .get_upload_files.py

  获取需要上传到服务器上的文件列表,并保存到指定的文件夹

     源文件的目录

fromdir = "source_folder"

     根据filelist.txt 复制的文件存储目录

todir = "backup"

     需要上传服务器的文件列表

filename = 'filelist.txt'

3.backup_server_files.py

      备份目录文件

todir = "backup"

     需要备份的文件列表

filename = 'filelist.txt'
     服务器的代码路径,记住最后的 / 需要保留
SRC_FOLDER = ‘/path_to_server_src_folder/‘

4.filelist.txt 这个文件存储需要上传的文件

    git如何获取需要上传的文件
git diff --name-status HEAD~2 HEAD~3
git diff
git diff b45ba47d1b297217e3ec6a3ab0f61716a8d6ecbc c244d0bf06d56ec86aaedeefa5dcd84dd9febc60
一般来说,通过 hash 串的前 4~6 位就可以区分,所示可以简写为:
git diff b45b 355e

 

git diff这个命令能比较两个提交之间的差异,使用–name-only参数可以只显示文件名。例如:

$ git diff 608e120 4abe32e --name-only
 
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线