步骤逻辑
要备份的文件夹:source = ["/opt/containerd"]
保存备份信息的文件夹 targz_dir = "/home/backup"
文件夹名(以当天日期为) day_dir = targz_dir +time.strftime('%Y%m%d')
文件名(当时时间为) filename = time.strftime('%H%M%S')
检查文件夹是否存在 os.path.exist(day_dir)
文件夹路径 需要压缩 zip_dir = day_dir + os.sep +filename +'.zip' # os.sep 兼容Windows平台斜杠 os.sep = '\'
文件压缩命令 command_shell = "zip -qr" +"zip_dir" + ' '.jion(source)
代码实现
source = ["/home/yjc/linux"] targz_dir = "hoem/backup" day_dir = targz_dir +time.strftime('%Y%m%d') filename = time.strftime('%H%M%S') zip_dir = day_dir +os.sep + filename +'.zip' command_shell= "zip -qr" + zip_dir + ''+' '.join(source) if not os.path.exists(day_dir): # 判断备份目录是否存在,如果不存在则创建 os.mkdir(day_dir) if os.system(command_shell) == 0: print("备份成功") else: print("备份失败")
简易图形界面
def bakcup(): global entry_source global entry_tagz_dir source= entry_source.get() targz_dir=entry_tagz_dir.get() day_dir = targz_dir +time.strftime('%Y%m%d') filename = time.strftime('%H%M%S') zip_dir = day_dir +os.sep + filename +'.zip' command_shell= "zip -qr" + zip_dir + ''+' '.join(source) if not os.path.exists(day_dir): # 判断备份目录是否存在,如果不存在则创建 os.mkdir(day_dir) if os.system(command_shell) == 0: print("备份成功") else: print("备份失败") #编写界面布局 root = tkinter.Tk() root.title('Backup') root.geometry("2000x2000") #第一行的两个控件 dir_source = tkinter.Label(root,text='Source') dir_source.grid(row=0,column=0) entry_source = tkinter.Entry(root) entry_source.grid(row=0,column=1) # 第二行控件 target_dir = tkinter.Label(root,text='Target') target_dir.grid(row=1,column=0) entry_tagz_dir=tkinter.Entry(root) entry_tagz_dir.grid(row=1,column=1) #第三行控件 bak=tkinter.Button(root,text='Backup') bak.grid(row=3,column=0) bak["command"] = bakcup #界面开始 root.mainloop()