发布时间:2019-08-27 08:03:28编辑:auto阅读(1444)
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys,os,time
db_user = 'postgres'
db_pwd = 'postgres'
db_port = '5432'
db_name = 'CCIC2'
backup_path = '/home/sunjc/backupdb/'
cmd_path = '/usr/local/pgsql/bin/'
log_path = backup_path + 'log'
# 创建日志函数
def writeLogs(filename,contents):
f = file(filename,'aw')
f.write(contents)
f.close()
today = backup_path + time.strftime('%Y-%m-%d')
fname = today + os.sep + time.strftime('%Y-%m-%d') + '_' + db_name + '.backup'
# 创建备份目录
if not os.path.exists(today):
Msg = '-'*30 + time.strftime('%Y-%m-%d,%H:%M:%S') + '-'*30 + '\n'
if(os.mkdir(today)) == None:
Msg += '** 成功创建备份目录: ' + today + '\n\n'
writeLogs(log_path,Msg)
else:
Msg += '!! 创建备份目录: ' + today + '失败,请检查目录是否可写!\n\n'
writeLogs(log_path,Msg)
sys.exit()
# 备份 CCIC2数据库
cmd_dump = "%spg_dump -i -h localhost -p %s -U %s -F c -b -v -f %s %s" % \
(cmd_path,db_port,db_user,fname,db_name)
# 执行备份命令
if os.system(cmd_dump) == 0:
writeLogs(log_path,'数据备份为: ' + fname + '\n')
else:
writeLogs(log_path,'数据备份失败!\n')
上一篇: python 执行 shell命令 的几
下一篇: Python实现任意进制之间的转换
47882
46450
37336
34773
29348
26011
24961
19979
19582
18076
5821°
6449°
5962°
5988°
7096°
5937°
5981°
6472°
6437°
7818°