Okay thanks that is much neater.
Peter
On Monday, 29 October 2012 11:47:57 UTC, Niphlod wrote:
>
> ps: don't use os.getcwd() + '/applications/' + request.application . Use
> request.folder.
>
> On Monday, October 29, 2012 12:08:27 PM UTC+1, peter wrote:
>>
>> I thought I would share some routines I find useful for backing up sqlite
>> to dropbox I believe safely.
>>
>> The nice thing about backing up to dropbox, is that the backup ends up on
>> your local machine and so can be backed up more safely from there.
>>
>> First execute 'setup_dropbox'. This should only need to be done once per
>> application.
>> Then run copy_to_backup
>> and finally backup_to_dropbox
>>
>>
>> This copies storage.sqlite in what I believe is a safe way, it is easy to
>> get a corrupted fileif you do not do it right like below. The file is
>> copied to a backup directory
>>
>> def copy_database():
>> import os, sqlite3, shutil, time
>>
>> source=os.getcwd()+'/applications/'+request.application+'/databases/storage.sqlite'
>> target_dir=os.getcwd()+'/applications/'+request.application+'/backup'
>> db = sqlite3.connect (source)
>> backupfile = os.path.join (target_dir, os.path.basename(source) +
>> time.strftime(".%Y%m%d-%H%M") )
>> cur = db.cursor ()
>> cur.execute ('begin immediate')
>> shutil.copyfile (source, backupfile)
>> db.rollback()
>> return dict()
>>
>>
>> This routines zips files except any ending in '.sqlite', and copies them
>> to the target directory
>> def do_zip_except_sqlite(target_dir, file_name):
>> import os, zipfile
>> zip = zipfile.ZipFile(file_name, 'w', zipfile.ZIP_DEFLATED)
>> rootlen = len(target_dir) + 1
>> print rootlen
>> for base, dirs, files in os.walk(target_dir):
>> print dir
>> for file in files:
>> if file.find('.sqlite',len(file)-7) ==-1:
>> fn = os.path.join(base, file)
>> zip.write(fn, fn[rootlen:])
>> zip.close()
>> return dict()
>>
>> This uses both routines to copy the database files to the backup
>> directory. It time stamps their names.
>> def copy_to_backup():
>> import os, time
>> base='applications/'+request.application
>> backupfile = os.path.join (base, 'backup/databases.zip' +
>> time.strftime(".%Y%m%d-%H%M") )
>> do_zip_except_sqlite(os.path.join(base,'databases'),backupfile)
>> copy_database()
>> return dict()
>>
>> This copies files from backup folder to dropbox..
>>
>> It assumes the dropbox access token has been written to the file
>>
>> applications/my_app/dropbox_token.txt
>>
>> Writing it to this file means that permission only has to be given once
>> per application. This seems appropriate for backing up.
>>
>> def backup_to_dropbox():
>> import os, zipfile
>> from gluon import current
>> from dropbox import client, rest, session
>> token=current.session.token
>>
>>
>> # Get your app key and secret from the Dropbox developer website
>> APP_KEY = 'put_your_key_here'
>> APP_SECRET = 'put_your_secret_here'
>>
>> ACCESS_TYPE = 'app_folder'
>> token_file = 'applications/'+request.application+'/dropbox_token.txt'
>> token_file = open(token_file)
>> token_key,token_secret = token_file.read().split('|')
>> token_file.close()
>>
>> sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )
>> sess.set_token(token_key,token_secret)
>> client = client.DropboxClient(sess)
>>
>> target_dir = 'applications/'+request.application+'/backup'
>> rootlen = len(target_dir) + 1
>> for base, dirs, files in os.walk(target_dir):
>> for file in files:
>> f = open(target_dir+'/'+file)
>> client.put_file('f1/'+file,f)
>> f.close()
>>
>> return dict()
>>
>>
>> To setup dropbox you need two routines. One to do the initial setting up,
>> and one to 'redirect' to after the permission has been granted.
>>
>> def setup_dropbox():
>> from gluon import current
>> # Include the Dropbox SDK libraries
>> from dropbox import client, rest, session
>> # Get your app key and secret from the Dropbox developer website
>> APP_KEY = 'put_your_key_here'
>> APP_SECRET ='put_your_secret_here'
>>
>> ACCESS_TYPE = 'app_folder'
>>
>> sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
>>
>> request_token = sess.obtain_request_token()
>>
>> url = sess.build_authorize_url(request_token,
>> URL('finish_setup_dropbox', scheme=True, host=True))
>> current.session.token=(request_token.key,request_token.secret)
>> TOKENS = 'applications/'+request.application+'/dropbox_tokenr.txt'
>> token_file = open(TOKENS,'w')
>> token_file.write("%s|%s" % (request_token.key,request_token.secret) )
>> token_file.close()
>> redirect(url)
>> return
>>
>>
>> This routine catches the redirection after the permission has been given
>> def finish_setup_dropbox():
>> from dropbox import client, rest, session
>> APP_KEY = 'put_your_key_here'
>> APP_SECRET = 'put_your_secret_here'
>>
>> ACCESS_TYPE = 'app_folder'
>> sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
>> request_token = sess.obtain_request_token()
>>
>> TOKENS = 'applications/'+request.application+'/dropbox_tokenr.txt'
>> token_file = open(TOKENS)
>> request_token.key,request_token.secret = token_file.read().split('|')
>> token_file.close()
>> access_token = sess.obtain_access_token(request_token)
>> TOKENS = 'applications/'+request.application+'/dropbox_token.txt'
>> token_file = open(TOKENS,'w')
>> token_file.write("%s|%s" % (access_token.key,access_token.secret) )
>> token_file.close()
>> client = client.DropboxClient(sess)
>> # print "linked account:", client.account_info()
>> return
>>
>>
>>
--