SCP Automatic Backup

Source: Internet
Author: User

An FTP server to see if the file is taking up too much space. The SCP is then assigned to the server. Delete outdated files


has been used in advance

ssh-keygen-t RSA Ssh-copy-id-i [email protected]

Add related trusts


Originally wanted to use the Pexpect module to automatically enter the password. And then found this in copying large files seems to have a problem. Check it out. Change the following file

Vi/usr/lib/python2.7/dist-packages/pxssh.py# said in this place add Self.sendline () Time.sleep () try not to. 135 Self.read_nonblocki Ng (size=10000,timeout=1) # Gas:clear out the cache before getting the prompt136 Time.sleep (0.1) 137 Self.s EndLine ()
#!/usr/bin/pythonimport os, sys, re, datetime, time, subprocess # ,  SHUTILSDISK_SPACE = OS.STATVFS ("/local_home") #f_bsize =0, f_frsize=1, f_blocks=2,  f_bfree=3, f_bavail=4, f_files=5, f_ffree=6, f_favail=7, f_flag=8, f_ namemax=9# f_bavail : non root user available space# f_bfree :  ALL AVAILABLE SPACE# AVAILABLE = OS.STATVFS (Mount) .f_bfree *  OS.STATVFS (Mount). F_BSIZE# ALL_SPACE = OS.STATVFS (Mount). F_BLOCKS * OS.STATVFS (Mount) .f_bsize# used = all_space - availableall_space = disk_space.f_blocks  * disk_space.f_bsize / 1024ava_space = disk_space.f_bfree * disk_space.f_ bsize / 1024if  (Ava_space * 1.0 / all_space)  > 0.1:   sys.exit () Def c_time (time_tmp= '):  if time_tmp ==  ':    tmp =  Datetime.datetime.now (). Strftime ("%y-%m-%d %h:%m:%s")   else:    tmp =  time.strftime ("%y-%m-%d %h:%m:%s",  time.localtime (time_tmp))   now =  Datetime.datetime.strptime (tmp,  '%y-%m-%d %h:%m:%s ')   return nowdef chown_file () :   os.system ("ssh -t [email protected]  ' Chown -r server:root /bak /server ' ") Def scp_file (CMD1, CMD2):   child = subprocess. Popen ("scp %s [email protected]:/bak/server/%s/"  %  (CMD1, CMD2),  shell=true ,  stdout=subprocess. PIPE)   child.wait ()   if child.returncode == 0:     Return true  else:    return falsefor root,dirs,files in  os.walk ('/local_home/ftp '):  If len (Re.findall ("/",  root))  == 3:  #限制目录深度     for i in  dirs:      if  (C_time ()  - c_time (Os.stat (root + ) /" + i). St_mtime). DAYS > 7:        IF SCP _file ("-r "  + root +  "/"  + i,  "/". Join ((root +  "/"  +  i). Split ('/') [3:-1]):           os.system (' RM -RF  %s '  %  (root +  "/"  + i) "    for i in  files:      if  (C_time ()  - c_time (Os.stat (root +  "/"  + i). St_mtime). Days > 7:        if scp_ File (root +  "/"  + i,  "/". Join ((root +  "/"  + i). Split ('/') [3:-1]) :    &Nbsp;      os.system (' rm -rf %s '  %  (root +  "/"  + i)) Chown_file () #print   "Scp ok"

SCP Automatic backup

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.