一台FTP服务器, 查看文件是否占用过多空间. 然后就scp到指定服务器. 删除过期文件
已提前使用
ssh-keygen -t rsa ssh-copy-id -i root@server
添加相关信任
本来想用 pexpect 模块自动输入密码. 然后发现这个在复制大文件似乎有问题. 查了一下说更改下面的文件
vi /usr/lib/python2.7/dist-packages/pxssh.py # 说在这个地方再添加 self.sendline() time.sleep() 试了不行. 135 self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt 136 time.sleep(0.1) 137 self.sendline()
#!/usr/bin/python import os, sys, re, datetime, time, subprocess # , shutils disk_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 - available all_space = disk_space.f_blocks * disk_space.f_bsize / 1024 ava_space = disk_space.f_bfree * disk_space.f_bsize / 1024 if (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 now def chown_file(): os.system("ssh -t root@bacula 'chown -R server:root /bak/server'") def scp_file(cmd1, cmd2): child = subprocess.Popen("scp %s root@server:/bak/server/%s/" % (cmd1, cmd2), shell=True, stdout=subprocess.PIPE) child.wait() if child.returncode == 0: return True else: return False for 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])): os.system('rm -rf %s' % (root + "/" + i)) chown_file() #print "SCP OK"