- Scheduled File Upload Script client.py
#!/usr/bin/env python
import os
import time
from time import sleep
from socket import *
HOST = '10.239.44.242'
PORT = 21567
BUFSIZ = 4096
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
while 1:
sleep(1)
day_date = time.strftime("%Y%m%d",time.localtime())
hours_date = time.strftime("%H:%M:%S",time.localtime())
filename=day_date+'_4a_liucheng_db.sql'
#os.system('rm -rf /opt/databack/'+filename)
if hours_date == '16:27:00':
data = 'send '+ filename
try:
os.stat(data.split()[1])
tcpCliSock.send(data)
print 'send msg:', data
recv_data = tcpCliSock.recv(BUFSIZ)
if recv_data == 'ok2send':
file2send = data.split()[1]
f = open(file2send,'rb')
file_data = f.read()
f.close()
tcpCliSock.sendall(file_data)
print 'file sent finished!'
sleep(2)
os.system('rm -rf /opt/databack/'+filename)
tcpCliSock.send('file_send_done')
except OSError:
print '\033[31;1mNo file %s found on localhost\033[0m' % data.split()[1]
continue
View Code2. Shceduled File Reception Script ftpserver.py
#!/usr/bin/env python
import SocketServer
import os
from time import ctime,sleep
HOST = ''
PORT = 21567
ADDR = (HOST, PORT)
class MyRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
print '...connected from:', self.client_address
#print self.request.recv(1024)
#self.request.send('Username')
#----------Auth part----------
recv_data = ''
if recv_data == 'auth':
print 'auth'
while 1:
self.request.send('Username')
username = self.request.recv(1024)
if username == 'alex':
self.request.send('correct')
print 'Correct! Welcome!'
break
else:
self.request.send('incorrect')
continue
def SendFromClient(filename):
print 'start receiving data'
f = file('/opt/caoqinghui/databak/ldap3.4/'+filename,'wb')
while True:
data = self.request.recv(4096)
if data == 'file_send_done':break
f.write(data)
f.close()
print 'File %s receive done!' % filename
def SendToClient(filename):
print 'start sending file to client..'
f = file(filename,'rb')
#while True:
file_data = f.read()
# if not file_data:break
self.request.sendall(file_data)
f.close()
print 'file %s sent to client finished!' % filename
sleep(0.5)
self.request.send('file_send_to_client_done')
#ftp()
while True:
try:
re_msg = self.request.recv(1024)
print 'get',re_msg
if re_msg.split()[0] == 'send':
filename = re_msg.split()[1]
self.request.send('ok2send')
print 'ready to receive file from %s' % self.client_address[0]
SendFromClient(filename)
elif re_msg.split()[0] == 'get':
filename = re_msg.split()[1]
try:
os.stat(filename)
except OSError:
msg = '\033[31;1mNo file %s found on FTP server\033[0m' % filename
self.request.send(msg)
print msg
else:
self.request.send('ok2get')
sleep(0.5)
print 'ready to send file to client %s .' % self.client_address[0]
SendToClient(filename)
elif re_msg == 'help' or re_msg == '?':
help_msg = '''\033[32;1m\nhelp\nget filename\tget file from FTP server\nsend filename\tsend file to FTP server\nls\t\tshow file list on FTP server\033[0m'''
self.request.send(help_msg)
elif re_msg == 'ls':
print 'print dir list',re_msg
#file_list = os.listdir('.')
#convert2string = '\t'.join(file_list)
file_list = os.popen('ls -lth')
f_list = file_list.read()
self.request.sendall(f_list)
else:
print 'invalid instruction'
self.request.send('\033[31;1minvalid_instruction\033[0m')
print "get from %s: %s" %(self.client_address[0], re_msg )
#self.request.sendall('[%s] %s' % (ctime(),re_msg))
except IndexError:
print '%s client %s logout !' %(ctime(),self.client_address[0])
break
try:
tcpServ = SocketServer.ThreadingTCPServer(ADDR, MyRequestHandler)
print 'waiting for connection...'
tcpServ.serve_forever()
except socket.error,e:
print 'error happend!'
View Code3. Scheduled LDAP Backup Script
ldap-a:/opt/databack#more ldapback.sh
/opt/sun/dsee7/bin/dsconf export -p 30000 -c -w /opt/databack/ldap_pwd dc=jx,dc=mobile,dc=com /opt/databack/ldapbak_$(date +%Y%m%d).ldif
- Scheduled PostgreSQL Backup Script
node49:/opt/databack#more msdbback.sh
#!/bin/bash
export PATH=/opt/pgsql/bin:$PATH
date=date
pg_dump -U pguser 4a_db>/opt/databack/$(date +%Y%m%d)_4a_db.sql
- Scheduled Tasks Configuration
node49:/opt/databack#crontab -l
30 01 * * * /bin/sh /opt/databack/msdbback.sh>/opt/databack/log.txt 2>&1