- 论坛徽章:
- 0
|
以前都是用shell做的服务器相关文件备份。
进来想实验一下应用python 搞定,顺便也开始新的学习历程。
由于是自我野蛮生长,遇到诸多困难和疑问在此提出与大家一起讨论。
如下场景:
1.定义备份的目录:
#!/usr/bin/python
import os,shutil,time,subprocess,tarfile
today = time.strftime("%Y%m%d")
tgzfile = today+".tar.gz"
dst='/tmp/'+today
if os.path.isdir(dst):
shutil.rmtree(dst)
os.makedirs(dst)
sys_Base=['/var/spool/cron/','/etc/sysconfig/iptables','/var/log/messages','/var/log/wtmp','/root/.bash_history']
2.开始打压缩包:
方法一:
for file in sys_Base:
os.chdir(dst)
tar=tarfile.open(file+today,"w:gz")
tar.add(file)
tar.close()
无论如何是把参数传递不到里面,出来的都是file+today.tar.gz ....
方法二:
被人耻笑的os.system().
请各位前辈给个建议吧
|
|