美文网首页
Linux服务器冷备同步脚本

Linux服务器冷备同步脚本

作者: 平凡的运维之路 | 来源:发表于2023-02-15 16:29 被阅读0次

    Linux服务器冷备同步脚本

    脚本脚本详情

    • 详细代码 vim RsyncBackFile.py
    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    #Python3
    #import time, os, logging.config,configparser,subprocess
    #Python2.7
    import time, os, logging.config,ConfigParser,subprocess
    
    #探测主机是否存活
    def ProbeHost(ip):
        fuck = subprocess.Popen('ping -c 1 %s' % ip,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        result = fuck.stdout.read()
        if '100% packet loss' in result:
            logger.error("This is Ip: " +  ip  +  "  Network unreachable")
            return False
        else:
            return True
    
    #把配置文件配置项转换为列表形式
    def ToList(String):
        StringList = []
        try:
            for Line in  String.split(","):
                StringList.append(Line)
            return StringList
        except:
            logger.error(String + " Conversion list exception")
    
    #判断不存在则创建目录
    def IfDircRetedir(dirpath):
        if os.path.exists(dirpath):
            logger.info("directory already exists: "+ dirpath)
            #pass
        else:
            os.makedirs(dirpath)
            logger.info("directory created successfully: " + dirpath)
    
    #清理空目录当前一级
    def RmTmpdir(DirPath):
        try:
            dirlist = os.listdir(DirPath)
            for dir in dirlist:
                if not  os.listdir(DirPath +dir):
                    Rmdir = DirPath +dir
                    os.system("rm -r "  +Rmdir)
        except:
            pass
    
    #递归清理所有空目录
    def RmTmpdirToBak2(DirPath):
        try:
            for rootPath,dirs,files in os.walk(DirPath):
                for files in dirs:
                    if not os.listdir(os.path.join(rootPath,files)):
                        # os.removedirs(os.path.join(root,f))
                        os.removedirs(rootPath +"/"+files)
        except:
            logger.error("It is not a directory: " +DirPath )
    
    
    #远程判断目录是否存在
    def RemotejugeDir(IP,DestDir):
        Shell= "sshpass -p " + BackHostPassword +"  ssh " +  BackHostUser +"@" + IP  + " \"ls " + DestDir + "  && echo OK|| echo Fail\""
        ReturnMsg = os.popen(Shell)
        RunshellFileStatus = ReturnMsg.read()
        logger.info("Now Run Shell: " + Shell + "  return: " + str(RunshellFileStatus))
        if "OK" in  RunshellFileStatus:
            logger.info(IP  + " This is dir  or file  " + DestDir )
            return True
        else:
            logger.info(IP  + " This Not is dir  or file  " + DestDir )
            return False
    
    def CCODRsync(IP,DestDir):
        # logger.info("Now Back Host: " + IP  + " And catalogue " + DestDir)
        RsyncDestdir = DestDir + "/"
        SrcBackPath = RsyncSrcDir +IP + DestDir + "/" 
        RunRsyncHomeShell = "sshpass -p " + BackHostPassword +" rsync -qaP  --exclude-from=" +  Rsyncexcludeccodfilelist  + " -e ssh "  + BackHostUser +"@" + IP +":"   +  RsyncDestdir  + " " +  SrcBackPath   + " ; echo  $?"
        
        if RemotejugeDir(IP,RsyncDestdir):
            logger.info("Run Shell command: " + RunRsyncHomeShell)
            IfDircRetedir(SrcBackPath)
            try:
                ReturnMsg = os.popen(RunRsyncHomeShell)
                GetShellRunCode = ReturnMsg.read()
                logger.info("Execute the above command to return: " + str(GetShellRunCode))
                if 0   == int(GetShellRunCode):
                    logger.info(IP + " Rsync file: " + RsyncDestdir +"----> "+  SrcBackPath + " successfully" )
                else:
                    logger.error("Check that the Rsync command not find  or This "  +  RsyncDestdir + " related directory does not exist")
            except:
                logger.error("Run shell Error Please check whether it is normal!")
    
    #同步系统相关文件
    def SystemRsync(IP,DestDir):
        SrcBackPath = RsyncSrcDir +IP
        IfDircRetedir(SrcBackPath+DestDir )
        # RunRsyncHomeShell = "sshpass -p " + BackHostPassword +" rsync -qaP  --exclude-from=" +  Rsyncexcludeccodfilelist  + " -e ssh "  + BackHostUser +"@" + IP +":"   +  DestDir   + " ; echo  $?"
        RunRsyncHomeShell = "sshpass -p " + BackHostPassword +" rsync -qaP  --include-from=" +  Rsyncinclude  + " --exclude=/* -e ssh "  + BackHostUser +"@" + IP +":"   +  DestDir  +" " + SrcBackPath+DestDir + " ; echo  $?"
        logger.info("Run Shell command: " + RunRsyncHomeShell)
        os.popen(RunRsyncHomeShell)
    
    
    #同步底层语音文件
    def RsyncHostWav(IP,SrcWavPath,BaseWavPath):
        SrcBackPath = RsyncSrcDir +IP + SrcWavPath
        #判断目录是否存在
        if RemotejugeDir(IP,SrcWavPath):
            logger.info("RsyncHostWav Function "+ SrcWavPath + " " +  SrcBackPath)
            IfDircRetedir(SrcBackPath)
            RunRsyncHomeShell = "sshpass -p " + BackHostPassword +" rsync -qaP  --exclude=*.tar.gz  -e ssh "  + BackHostUser +"@" + IP +":"   +  SrcWavPath  + " " +  SrcBackPath   + " ; echo  $?"
            os.popen(RunRsyncHomeShell)
    
    def main():
        #循环每个ip
        for HostIP in RsyncIPlist:
            logger.info("Now Run Host: " + HostIP)
            #探测主机ip是否可达
            HostStatus  = ProbeHost(HostIP)
            if HostStatus:
                logger.info("Go Rsync Starting !!!!")
                #循环基础原始路径
                for BasePath in BaseSrcPathList: 
                    #循环用户名进行同步相关操作
                    for  username in HomeNameList:
                        CCODRsync(HostIP,BasePath + username )
                        #pass
    
                #同步系统相关文件
                for system in SystemBackPathList:
                    #pass
                    SystemRsync(HostIP, system )
    
                #同步软交换底层语音文件
                for OriginalWavPath in RsyncWavoriginalPathList:
                    for HostwavPath in RsyncHostWavPathList:
                        #pass
                        #源服务器的语音文件路径
                        SrcHostWavFilePath =  OriginalWavPath + HostwavPath
                        RsyncHostWav(HostIP,SrcHostWavFilePath,HostwavPath)
                    #放在循环基础路径,删除不要空目录,最后在进行清理操作。
                    # RmTmpdir(RsyncSrcDir + HostIP+BasePath )
                # RmTmpdir(RsyncSrcDir + HostIP+"/")
    
    if __name__ == "__main__":
        curpath = os.path.dirname(os.path.realpath(__file__))
        cfgpath = os.path.join(curpath, "cfg/config.ini")
        #conf = configparser.ConfigParser()
        conf = ConfigParser.ConfigParser()
        conf.read(cfgpath)
        # 基础配置加载
        LogRunSentence = " [ ! -d  './log'  ] && mkdir -p  log"
        os.system(LogRunSentence)
        logging.config.fileConfig("./cfg/logger.conf")
        logger = logging.getLogger("rotatfile")
        logger.setLevel(logging.INFO)
        logger.info("Rsync Back file function start")
        
        RsyncIP = conf.get("Base", "RsyncIP")
        Rsyncexcludeccodfilelist = conf.get("Base", "Rsyncexcludeccodfilelist")
        LocalBackDirPath = conf.get("Base", "LocalBackDirPath")
        BackHostUser = conf.get("Base", "BackHostUser")
        BackHostPassword = conf.get("Base", "BackHostPassword")
        HomeName = conf.get("Base", "HomeName")
        BaseSrcPath = conf.get("Base", "BaseSrcPath")
        SystemBackPath = conf.get("Base", "SystemBackPath")
        Rsyncinclude = conf.get("Base", "Rsyncinclude")
        RsyncHostWavPath = conf.get("Base", "RsyncHostWavPath")
        RsyncWavoriginalPath = conf.get("Base", "RsyncWavoriginalPath")
    
        #获取今天的日期
        Today = time.strftime("%Y-%m-%d", time.localtime())
        logger.info("Get the current date as: " + Today)
        RsyncIPlist = ToList(RsyncIP)
        HomeNameList = ToList(HomeName)
        BaseSrcPathList = ToList(BaseSrcPath)
        SystemBackPathList = ToList(SystemBackPath)
        RsyncHostWavPathList = ToList(RsyncHostWavPath)
        RsyncWavoriginalPathList = ToList(RsyncWavoriginalPath)
        #RsyncSrcDir = LocalBackDirPath +  Today +"/"
        RsyncSrcDir = LocalBackDirPath 
    
        #运行主函数
        main()
    

    配置文件说明

    • 主配置文件 vim config.ini
    [Base]
    
    ###############################
    #注意点
    #1、需要每台服务器上面进行安装rsync服务
    #2、在中间服务器执行以下操作在 .ssh目录下添加config文件,输入,然后设置600权限即可
    #                        StrictHostKeyChecking no
    #                        UserKnownHostsFile /dev/null
    ###############################
    
    #ip列表,需要备份的主机ip地址。
    RsyncIP = 10.100.0.2,10.100.0.3,10.100.0.25,10.100.0.51,10.100.0.53,10.100.0.54,10.100.0.55,10.100.0.56,10.100.0.60,10.100.0.45,10.100.0.49,10.100.0.50,10.100.0.32,10.100.0.34,10.100.0.35,10.100.0.92,10.100.0.102,10.100.0.103,10.100.0.105,10.100.0.106,10.100.0.107,10.100.0.109,10.100.0.111,10.100.0.104,10.100.0.113,10.100.0.31,10.100.0.33,10.100.0.57,10.100.0.74,10.100.0.84,10.100.0.85,10.100.0.72,10.100.0.61,10.100.0.62,10.100.0.63,10.100.0.42,10.100.0.64,10.100.0.65,10.100.0.66,10.100.0.68,10.100.0.78,10.100.0.73,10.100.0.86,10.100.0.87,10.100.0.88,10.100.0.93,10.100.0.95,10.100.0.7,10.100.0.17,10.100.0.6,10.100.0.12,10.100.0.15,10.100.0.16,10.100.0.79,10.100.0.233,10.100.0.80,10.100.0.67,10.100.0.29,10.100.0.59,10.100.0.28,10.100.0.79,10.100.0.81,10.100.0.82,10.100.0.23,10.100.0.24,10.100.0.85,10.100.0.32,10.100.0.5,10.100.0.240,10.100.0.75,10.100.0.18,10.100.0.220
    #CCOD_exclude过滤不需要的文件
    Rsyncexcludeccodfilelist =./cfg/ccodexclude.list
    #只传输哪些文件
    Rsyncinclude=./cfg/include.list
    #本地备份路径
    LocalBackDirPath=/BAK/
    #执行需要执行同步服务器的用户名
    BackHostUser=root
    #指定备份服务器密码,最好是root用户密码,不然对其他用户目录无权限访问。
    BackHostPassword=***********
    #指定需要备份的家目录,或者程序的运行目录
    HomeName=redis,zookeeper,etc,keepalived,nginx-2,nginx-ssl
    #备份的原始基础路径,后面备份就在此
    BaseSrcPath=/home/,/usr/local/,/etc/
    #以下配置基本上就是固定
    #备份路由目录文件和host文件和keepalived文件
    SystemBackPath=/etc/
    #备份Host底层语音文件
    RsyncHostWavPath=record/recording/CCOD1.5/Document/,record/recording/CCOD1.5/Document/voice/
    RsyncWavoriginalPath=/home/,/
    
    • 过滤配置文件 vim ccodexclude.list
    *_201[1-9]*
    *_202[1-9]*
    *_203[1-9]*
    *_bak*
    */fps-uploaded/*
    *wav.mix
    *.tar.gz
    log.*
    cchannel_track_ucx.*
    core.*
    *.log
    *.log.*
    *.dat
    *.log_*
    *.log-*
    *.Log
    *.tar
    *.tar.*
    *.tar_*
    *.tar-*
    log.*
    *.tgz
    *.tgz.*
    *.tgz_*
    *.tgz-*
    *.tar.gz
    *.tar.gz.*
    *.tar.gz_*
    *.tar.gz-*
    *.sql
    *.sql.*
    *.sql-*
    *.sql_*
    *.war.bak
    *.war.*
    *.war_*
    *.war-*
    *.wav
    *.wav.*
    *.wav_*
    *.wav-*
    *.bin
    *.mp3
    dae_errorsql/
    ssbak/
    nohup.out
    core.*
    *Server-*
    *Server_*
    Platform/dat/
    log/
    *.amr
    *.txt
    ems/log/
    yanss/
    yinxt/
    liuxy/
    wt/
    guanyw/
    gaolei/
    oracle-bak/
    *.stat
    *.rar
    *.zip
    *.xls
    ChannelSoft/ATS4/runlog/
    ccodrunner/recordback/
    ccodrunner/recordfailed/
    CCOD1.5/record/
    0000050598/
    *.txt-*
    RunLog/
    SoftSwitch/generated/servicecode/
    ChannelSoft/CsCCP/CCODLog/
    ChannelSoft/CsCCP/CCODLog_bak/
    ccodqnsoft/mnt/media/
    webapps/ivrtime/userdata/ivrtimeconfig/
    ccodqnsoft/resin-3.0.22/test/
    tomcat/logs/
    ATS4_setup-BAK/
    slee.Platform/
    ChannelSoft/ATS4/cdr/DRFiles/
    ChannelSoft/ATS4/cdr/DRFilesBak/
    prd/Record/
    m3gc/Record/
    IsxSNMP-Proxy/Log/
    hugecap/log*/
    Log202[0-9]/
    *Log202[0-9]*
    *.pcap
    apache-tomcat-6.0.33/logs/
    CCOD1.5/recordbak/fps-uploaded/
    logs/catalina.*
    logs/host-manager.*
    logs/localhost.*
    recordback/fps-uploaded/
    Platform4.5/log
    m3gc/CCOD4.5
    m3gc/CCOD4.5/4.5bak
    prd/recordback
    backup
    BACKUP
    *.core.*
    Bak
    BAK
    bak
    update
    *.log.202[0-9]*
    

    特殊注意点

    • 注意点
      • 1、需要每台服务器上面进行安装rsync服务
      • 2、在中间服务器执行以下操作在 .ssh目录下添加config文件,输入,然后设置600权限即可
    vim ~/.ssh/conf
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null
    chmod 600  ~/.ssh/conf
    
    • 需要安装程序依赖
    • 需要每台安装rsync命令
    • 在冷备服务器安装sshpass命令

    相关文章

      网友评论

          本文标题:Linux服务器冷备同步脚本

          本文链接:https://www.haomeiwen.com/subject/vhhukdtx.html