为什么要做这个工具
之前做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