python实战系列之按期备份配置文件(五)

  1. 背景需求python

    办公区使用了dhcp用于动态分配ip地址,解决了办公用户,移动用户(wifi)地址分配问题,因有配置的变动,为保障配置文件正确,防止出故障时实现快速恢复,特编写了一个备份文件的python脚本。备份的方式是:本地保留一个月的数据,并将备份文件上传至另一台机器,以达到容灾的目的,程序经过crontab天天按期执行一次便可。shell


  2. 程序内容
    数据库

cat /usr/local/sbin/backup_dhcp.py  
#!/usr/bin/python
#_*_ coding:utf8 _*_
#author:Happy
#目的:备份dhcp的配置文件/etc/dhcp/dhcpd.conf
#来自Happy实验室,快乐学习,一块儿进步

import os
import sys
import time
import os.path
import shutil

class Backup(object):
        '''
        用于备份dhcp的配置文件
        '''
        dhcp_conf = "/etc/dhcp/dhcpd.conf"
        backup_dir = "/etc/dhcp/backup"
        date = time.strftime('%Y-%m-%d',time.localtime())
        
        def check_path(self):
                '''
                检查dhcp配置文件和备份路径
                '''
                if not os.path.exists(self.dhcp_conf):
                        print "dhcpd configuration %s is not exsits" % (self.dhcp_conf)
                        return False 
                if not os.path.isdir(self.backup_dir):
                        os.makedirs(self.backup_dir)
                return True
                
        def get_backup_filename(self):
                '''
                获得dhcp的备份名字,相似于:/etc/dhcp/backup/dhcpd-2016-01-01的格式
                '''
                if self.check_path():
                        conf =  os.path.split(self.dhcp_conf)[-1]
                        backup_filename = os.path.join(self.backup_dir,"%s_%s" % (conf,self.date))
                        shutil.copyfile(self.dhcp_conf,backup_filename)
                return backup_filename
                
        def upload_backup_filename(self):
                '''
                将备份文件上传至远程的服务器,此处经过rsync的方式实现,提早配置好rsync服务
                '''
                cmd = "rsync -av %s rsync://root@10.16.2.8/upload/dhcp_bak"  % (self.get_backup_filename())
                return os.system(cmd)
        def delete_exipre_filename(self):
                '''
                删除过时的文件,保留周期为一个月,此处直接调用shell中的find实现
                '''
                cmd = 'find %s -type f -a -ctime +31 -a -name "dhcpd.conf*" | xargs rm -f {}'  % (self.backup_dir)
                return os.system(cmd)
                
if __name__ == "__main__":
        bak = Backup()
        bak.upload_backup_filename() 
        bak.delete_exipre_filename()

3. 按期执行
服务器

    经过周期性计划任务crontab实现周期性自动执行,备份按照性质不一样能够设置不一样的备份策略,如每小时备份,天天备份,每周备份,或者每月备份,按照不一样的状况,采起不一样的备份周期,编写crontab实现便可,以下天天备份为例:
app

chmod u+x /usr/local/sbin/backup_dhcp.py 
[root@KUGOU_DHCP_GZYC backup]# crontab -e
01      01      *       *       *       ( python /usr/local/sbin/backup_dhcp.py )


4. 总结运维

   备份是平常运维中工做的重要组成部分,如数据库的备份,配置文件的备份,归档数据的备份等等,经过备份策略,能够在数据有故障时,采起快速的回滚,还原,必定程度减小数据的丢失,保障应用程序正常运行。上述的例子经过备份单个文件实现数据的备份,能够改造为一个通用的备份,如备份的文件路径,备份存储路径,远程保存的地址等。ide


5. time模块的学习学习

python中经过time模块,实现时间相关的处理,以下总结了time经常使用的方法.
crontab

  1. 获取当前时间的时间戳ip

>>> import time
>>> time.time()
1452581489.4855039

2. 获取当前日期

>>> time.ctime()
'Tue Jan 12 14:52:29 2016'

3. 获取时间的八元组

>>> time.localtime()
time.struct_time(tm_year=2016, tm_mon=1, tm_mday=12, tm_hour=14, tm_min=53, tm_sec=11, tm_wday=1, tm_yday=12, tm_isdst=0)

4. 将时间戳转换为日期格式

>>> time.strftime("%Y-%m-%d",time.localtime(time.time()))
'2016-01-12'
>>> time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
'2016-01-12 14:55:13'

5. 将时间日期转换为八元组

>>> time.strptime("2011-09-28 10:00:00","%Y-%m-%d %H:%M:%S") 
time.struct_time(tm_year=2011, tm_mon=9, tm_mday=28, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=271, tm_isdst=-1)

6. 将时间日期转换为时间戳

>>> time.mktime(time.strptime("2011-09-28 10:00:00","%Y-%m-%d %H:%M:%S"))
1317175200.0
>>> time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(1317175200.0))
'2011-09-28 10:00:00'
相关文章
相关标签/搜索