- 论坛徽章:
- 0
|
1.你是如何学习Python的?看书?看视频?还是?
开始学习是由于任务紧急,报了个线上视频班,速度快但是学的不深入,后来就抱着《简明python教程》,看一些模块代码。
2.你工作中有用到Python吗?主要用来做什么?目的是把现有日常运维繁杂的工作释放出来,做了一套线上自动发布系统,把发布任务外发到测试部门
3.你觉得Python跟其他脚本语言比优势在哪?
比起shell优势是socket编程,大量现成模块可以用,可以方便地实现shell完成不了的事情
4.谈谈Python在自动化运维中的应用。
我们主要用在自动化发布系统中C/S通信
5.给出一个你自己写的Python脚本(最好有详细注释)。
#! /usr/bin/env python
'''
先拷贝需要备份的文件,再压缩文件
'''
import os
import os.path
import sys
import shutil
import time
### 需要备份的文件start
source = '/www'
config = ['.vimrc',
'/etc/profile']
data = ['java',
'web_server',
'NFS']
all = config + data
### 需要备份的文件end
def copy_file(source, target_dir):
'对比文件,如果目标文件不存在或者还是源文件新,则拷贝或覆盖文件。'
# 如果路径有前缀 '/',则先将其去除。
target = os.path.join(target_dir,
source[:1] == '/' and source[1:] or source)
if not os.path.exists(target):
dir, name = os.path.split(target)
if not os.path.exists(dir):
os.makedirs(dir)
shutil.copy(source, target)
print 'Backup', source
elif os.path.getmtime(source) > os.path.getmtime(target):
shutil.copy(source, target)
print 'Backup', source
def copy_dir(top, target_dir):
'递归地利用 copy_file 函数进行文件夹的拷贝。'
for root, dirs, files in os.walk(top):
for file in files:
filepath = os.path.join(root, file)
copy_file(filepath, target_dir)
if __name__ == '__main__':
if (len(sys.argv) == 2 and sys.argv[1] != '-m') or len(sys.argv) > 2:
print '''
Usage: backup.py [-m]
加入 -m 参数则将 tar 文件移动到 /BACK_UP 分区中。'''
sys.exit(1)
# 临时存放需要备份的文件,便于打包。
tmp_dir = 'backup'
# 备份文件的保存路径。
bkpath = '/BACK_UP'
date = time.strftime('%y-%m-%d')
os.chdir(source)
for source in all:
if os.path.isfile(source):
copy_file(source, tmp_dir)
else:
copy_dir(source, tmp_dir)
os.system('tar -czf %s.tar.gz %s' % (date, tmp_dir))
if len(sys.argv) == 2 and sys.argv[1] == '-m':
os.system('mv %s.tar.gz %s' % (date, bkpath))
|
|