-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgDriveBackup.py
More file actions
executable file
·117 lines (103 loc) · 4.99 KB
/
gDriveBackup.py
File metadata and controls
executable file
·117 lines (103 loc) · 4.99 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
#gbackup.py v 0.1
#Author Greg Hetrick
#greghetrick@gmail.com
#Released under the GPL license.
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>
import httplib2
import pprint
import os
import sys, getopt
import mimetypes
import logging
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import FlowExchangeError
from apiclient import errors
from oauth2client.file import Storage
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive.file'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
os.environ['REQUESTS_CA_BUNDLE'] = '/home/ghetrick/cacert.pem'
USAGE = """
Usage: gbackup.py [options]
Options:
-h,--help Show this help menu and exit
-i, --init Initalize the access with Google for this script and the users account.
Only needs to be run once, unless access is revoked. Must run as root
First time this is run copy the printed URL from the command line to a browser and
allow access for the script. Copy the code presented back to the commandline
-f,--file filename(s) This can be a space delimited list: file1.txt file2.txt file3.txt - Or it can be *
"""
user = os.getlogin()
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "i f:h ", ["init", "file", "help"])
except getopt.GetoptError as error:
print (str(error))
print USAGE
sys.exit(2)
homedir = os.environ['HOME']
if homedir == "" :
homedir = "/root"
credfile = homedir + "/.gbackupcred"
if opts == []:
print USAGE
sys.exit(2)
for opt,toss in opts:
if opt in ("-h","--help"):
print USAGE
sys.exit()
elif opt in ("-f", "--file"):
#look for credential file
if not os.path.isfile(credfile):
print "Need to run qbackup with --init before uploading a file"
else:
storage = Storage(credfile)
creds = storage.get()
flow = flow_from_clientsecrets('client_secrets.json', OAUTH_SCOPE, REDIRECT_URI)
#Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = creds.authorize(http)
drive_service = build('drive', 'v2', http=http)
# Insert a file
for arg in sys.argv[2:]:
filename = os.path.basename(arg)
format,encoding = mimetypes.guess_type(arg)
if format is None:
format = 'application/zip'
media_body = MediaFileUpload(arg, mimetype=format, resumable=True)
body = {
'title': filename,
'description': 'Backupfile from gbackup.py',
'mimeType': format
}
#file = drive_service.files().insert(body=body, media_body=media_body).execute()
file = drive_service.files().insert(body=body, media_body=media_body)
response = None
while response is None:
status, response = file.next_chunk()
if status:
print "Uploaded %d%%." % int(status.progress() *100)
print filename + " - Upload Complete!"
elif opt in ("-i", "--init"):
httpfix = httplib2.Http(disable_ssl_certificate_validation=True)
flow = flow_from_clientsecrets('client_secrets.json', OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: \n' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code,httpfix)
storage = Storage(credfile)
storage.put(credentials)
print "All finished initializing, run again using -f or --file to upload a file."
sys.exit()