用python写一个简单的脚本,就可以每天凌晨得到一个备份 每天都会延迟一点。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
import os
from datetime import datetime
from time import sleep

datestr = datetime.strftime(datetime.now(), "%Y%m%d%H%M%S")

h = int(datestr[8:10])
m = int(datestr[10:12])
s = int(datestr[12:14])

h = 05 - h
m = 01 - m
s = 01 - s

s = h * 3600 + m * 60 + s
print s

if s < 0:
    s = 3600 * 24 + s
print s

sleep(s)

while True:
    datestr = datetime.strftime(datetime.now(), "%Y%m%d%H%M%S")
    print datestr
    os.system("echo db_password|pg_dump -p 5432 -h localhost -U db_user db_name > db%s.sql" % datestr)
    os.system("tar -czvf db%s.sql.tar.gz db%s.sql" % (datestr, datestr))
    os.system("rm -rf db%s.sql" % datestr)
    print "ok ............\n"
    
    sleep(60 * 60 * 24)