Send LogicMachine backup file to Dropbox #
Create a Dropbox app #
-
Go to https://www.dropbox.com/developers/apps and click Create app.
-
Fill in the form as click Create app:
- Choose an API = Scoped access
- Choose the type of access you need = App folder
- Choose a unique name for your app, a folder with the same name will be created in your Dropbox account inside of the Apps folder.
- Switch to Permissions tab, enable files.content.write permission and click Submit at the bottom of the window.
- Switch to Settings, find App key and App secret values. These values will be needed later.
Get refresh token #
- Copy the following URL, replace APPKEYHERE with your App key and open it in your browser.
https://www.dropbox.com/oauth2/authorize?client_id=APPKEYHERE&response_type=code&token_access_type=offline
-
Connect your account to your application and copy the resulting Authorization code.
-
Run the following script once, replace ... with the respective App key, App secret and Authorization code values.
appkey = '...'
appsecret = '...'
authcode = '...'
json = require('json')
ltn12 = require('ltn12')
http = require('socket.http')
escape = require('socket.url').escape
data = table.concat({
'code=' .. escape(authcode),
'grant_type=authorization_code'
}, '&')
res, code, hdrs, status = http.request({
url = 'https://api.dropbox.com/oauth2/token',
method = 'POST',
user = appkey,
password = appsecret,
headers = {
['Content-Length'] = #data,
},
source = ltn12.source.string(data),
})
if code == 200 then
resp = json.pdecode(res)
log('refresh token', resp.refresh_token)
else
log('request failed', res, code)
end
- Refresh token will visible in LM Logs tab.
If you receive code has expired error, run steps 2 and 3 again
Scheduled script #
Create a scheduled script that runs the backup procedure, replace ... with the respective App key, App secret and Refresh token values. Run the script and check the results in the LM Logs tab.
filepath can contain folders (separated by /). This can be useful if you want to use a single app for backups from multiple LMs.
appkey = '...'
appsecret = '...'
refreshtoken = '...'
filepath = '/LM-' .. os.date('%Y-%m-%d') .. '.zip'
json = require('json')
ltn12 = require('ltn12')
http = require('socket.http')
escape = require('socket.url').escape
data = table.concat({
'refresh_token=' .. escape(refreshtoken),
'grant_type=refresh_token',
'client_id=' .. escape(appkey),
'client_secret=' .. escape(appsecret)
}, '&')
res, code, hdrs, status = http.request({
url = 'https://api.dropbox.com/oauth2/token',
method = 'POST',
headers = {
['Content-Length'] = #data,
},
source = ltn12.source.string(data),
})
if code ~= 200 then
log('access token request failed', res, code)
return
end
token = json.pdecode(res).access_token
webrequest = require('webrequest')
filedata, err = webrequest('general', 'backup')
res, code, hdrs, status = http.request({
url = 'https://content.dropboxapi.com/2/files/upload',
method = 'POST',
headers = {
['Authorization'] = 'Bearer ' .. token,
['Content-Type'] = 'application/octet-stream',
['Content-Length'] = #filedata,
['Dropbox-API-Arg'] = json.encode({
path = filepath,
mode = 'add',
autorename = true,
mute = false
}),
},
source = ltn12.source.string(filedata),
})
if code == 200 then
log('dropbox backup ok')
else
log('dropbox backup failed', res, code, hdrs, status)
end