Python+rsync precise synchronization of specified format files

Source: Internet
Author: User
Tags diff stdin

# coding: utf-8#!/usr/bin/env python ' updatedb update local server specified directory/home/upload/de  The LOCATE.DBZ database then locate the command to find the eligible file and output the file name to LOCATE.SRC through the Paramiko module to get the remote server eligible files and export the file name to LOCATE.DST compare these two files are Locate.diff, preferably RS The Ync command synchronizes the files in the Locate.diff list to the remote server ' import paramikoimport commandsimport osimport  Logginglogging.basicconfig (level=logging. debug,                     format= '% (asctime) s % (filename) s[line:% (Lineno) d] % (levelname) s % (message) s ',                      datefmt= '%y-%m-%d %h:%m:%s ',                     filename= '/var/log/upload_to_chengdu.log ',                   &nbsP; filemode= ' a ') logging.info ("###################################################################") hostname =  ' 8.8.8.8 ' username =  ' username ' password =  ' password ' port =  22locate_lists = ['/tmp/locate.src ',  '/tmp/locate.dst ',  '/tmp/locate.diff ']dict_path  = {' locate_lists ': locate_lists,  ' dst_db ':  '/tmp/locate.db ',               ' src_db ':  '/tmp/locate.db ',  ' dst_path ':  '/logs/ rsync_log/',              ' src_path ':  '/ home/upload/',  ' passwd_file ':  '/etc/rsync.pas '}def check_file ():     for  l in dict_path[' locate_lists ']:        if  Os.path.exists (L):             Commands.getstatusoutput (' sudo rm %s ' &Nbsp;% l) def get_dst_list ():     s = paramiko. Sshclient ()     s.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())     s.connect (Hostname=hostname, port=port, username=username,  password=password)     cmd_create =  "sudo updatedb -u %s  -o %s && sudo locate -d %s --regex  '. *[0-9]{4} ([-]{0,1}[ 0-9]{2}) {2}\. *gz$ '  >>%s '  %  (        dict_path[' Dst_path '),  dict_path[' dst_db '], dict_path[' dst_db '], dict_path[' locate_lists '][1])      cmd_delete =  ' sudo rm  '  + dict_path[' locate_lists '][1]     stdin, stdout, stderr = s.exec_command (cmd_create)     #  Print stdout.read ()     TRY:    &NBsp;   t = paramiko. Transport ((Hostname, port))         t.connect (Username=username,  password=password)         sftp = paramiko. Sftpclient.from_transport (t)         sftp.get (dict_path[' locate_lists ') [1], dict_path[' locate_lists '][1])         t.close ()      except Exception, e:        print e     stdin, stdout, stderr = s.exec_command (Cmd_delete)      s.close () def get_src_list ():    cmd =  "Sudo updatedb -U  %s -o %s && sudo locate -d %s --regex  '. *[0-9]{4} ([ -]{0,1}[0-9]{2}) {2}\. *gz$ '  >>%s '  %  (        dict_path[' Src_path '], dict_path[' src_db '], dict_path[' src_db '], dict_path[' locate_lists '] [0])     commands.getstatusoutput (CMD) def cmp_diff ():     f1 =  open (dict_path[' locate_lists '][0],  ' R ')     f2 = open (dict_path[' Locate_lists '][1],  ' R ')     f3 = open (dict_path[' locate_lists '][2],  ' a ' )     x = f1.readlines ()     y = f2.readlines ()     f1.close ()     f2.close ()     for i in  x:        j = dict_path[' Dst_path '] + i[13:]         if j not in y:             f3.writelines (i)     f3.close () def  Rsync_file ():   &NBsp; f = open (dict_path[' locate_lists '][2],  ' r ')     for i in  f.readlines ():        cmd =  ' cd %s & & sudo rsync -rvz %s [email protected]%s::aliyun_log --password-file=%s '  %  (            dict_path[' Src_path '),  i.strip (' \ n ') [13:], hostname, dict_path[' Passwd_file '])          logging.info (cmd)         commands.getstatusoutput (cmd)     f.close () def del_locate_file ():    cmd =  ' sudo  rm {%s,%s,%s} '  %  (dict_path[' locate_lists '][1], dict_path[' locate_lists '][0],  dict_path[' locate_lists '][2])     commands.getstatusoutput (cmd) if __name__ ==   ' __main__ ':   &NBSp; check_file ()     get_dst_list ()     get_src_list ()      cmp_diff ()     rsync_file ()     del_locate_file ()

Python+rsync exact synchronization of specified format files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.