· 8 years ago · Feb 21, 2017, 05:56 PM
1#-*-coding: utf-8-*-
2
3import os
4import sys
5import time
6import string
7from os.path import getsize
8'''
9curDay = time.strftime("%d", time.gmtime())
10backupDelay = time.time()
11if curDay == "01" :
12 backupDelay = 0
13
14# Include the Dropbox SDK libraries
15from dropbox import client, rest, session
16
17# Get your app key and secret from the Dropbox developer website
18import appK
19APP_KEY = appK.KEY
20APP_SECRET = appK.SECRET
21
22# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
23ACCESS_TYPE = 'app_folder'
24sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
25
26oauth_token = ''
27oauth_token_secret = ''
28
29f = open("dropbox_token.txt",'r')
30if f:
31 oauth_token = string.strip(f.readline())
32 oauth_token_secret = string.strip(f.readline())
33 f.close()
34
35print "oauth token found:", oauth_token, oauth_token_secret
36
37if oauth_token == '' or oauth_token_secret == '':
38 request_token = sess.obtain_request_token()
39
40 # Authorize the application on dropbox site
41 url = sess.build_authorize_url(request_token)
42 print "url:", url
43 print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."
44 raw_input()
45 # This will fail if the user didn't visit the above URL and hit 'Allow'
46 access_token = sess.obtain_access_token(request_token)
47 f = open("dropbox_token.txt","wb")
48 f.write(access_token.key + '\n')
49 f.write(access_token.secret)
50 f.close()
51else:
52 sess.set_token(oauth_token, oauth_token_secret)
53
54client = client.DropboxClient(sess)
55print "linked account:", client.account_info()
56'''
57from time import gmtime, strftime
58import subprocess
59import os
60import glob
61import time
62
63USER = "testuser"
64PASS = "test"
65HOST = "127.0.0.1"
66
67BACKUP_DIR = "/home/brutal/backup"
68dumper = """ "/home/brutal/backup" -U %s -Z 9 -f %s -F c %s """
69
70def log(string):
71 print time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) + ": " + str(string)
72
73
74os.putenv('PGPASSWORD', PASS)
75
76database_list = ['testdb']
77
78for database_name in database_list :
79 log("dump started for %s" % database_name)
80 thetime = str(strftime("%Y-%m-%d-%H-%M"))
81 file_name = database_name + '_' + thetime + ".sql.pgdump"
82 #Run the pg_dump command to the right directory
83 command = dumper % (USER, BACKUP_DIR + file_name, database_name)
84 log(command)
85 subprocess.call(command,shell = True)
86 log("%s dump finished" % database_name)
87
88log("Backup job complete.")