Google App Engine 文件代码备份
六月 16th, 2008
Google App Engine 不支持直接下载代码文件,所以我们无法直接将服务器上的代码进行备份。如果本地的代码一旦丢失,就很难找回,这点对GAE的用户来说,是相当麻烦的。
好在有人编写了一个工具,用于将GAE站点的代码打包成zip格式并下载。
步骤如下:
1.在根目录下根据manatlan的代码建立zipme.py
2.在app.yaml中加入
- url: /zipme
script: zipme.py
3.访问youapp.appspot.com/zipme
zipme.py脚本
#!/usr/bin/env python # -*- coding: UTF-8 -*- """ ########################################################################## ZipMe : GAE Content Downloader ########################################################################## Just add this lines in your app.yaml : - url: /zipme script: zipme.py ########################################################################## """ # manatlan from google.appengine.ext import webapp from google.appengine.api import users import wsgiref.handlers import zipfile import os,re,sys,stat from cStringIO import StringIO def createZip(path): def walktree (top = ".", depthfirst = True): names = os.listdir(top) if not depthfirst: yield top, names for name in names: try: st = os.lstat(os.path.join(top, name)) except os.error: continue if stat.S_ISDIR(st.st_mode): for (newtop, children) in walktree (os.path.join(top, name), depthfirst): yield newtop, children if depthfirst: yield top, names list=[] for (basepath, children) in walktree(path,False): for child in children: f=os.path.join(basepath,child) if os.path.isfile(f): f = f.encode(sys.getfilesystemencoding()) list.append( f ) f=StringIO() file = zipfile.ZipFile(f, "w") for fname in list: nfname=os.path.join(os.path.basename(path),fname[len(path)+1:]) file.write(fname, nfname , zipfile.ZIP_DEFLATED) file.close() f.seek(0) return f class ZipMaker(webapp.RequestHandler): def get(self): if users.is_current_user_admin(): folder = os.path.dirname(__file__) self.response.headers['Content-Type'] = 'application/zip' self.response.headers['Content-Disposition'] = \ 'attachment; filename="%s.zip"' % os.path.basename(folder) fid=createZip(folder) while True: buf=fid.read(2048) if buf=="": break self.response.out.write(buf) fid.close() else: self.response.headers['Content-Type'] = 'text/html' self.response.out.write("<a href=\"%s\">You must be admin</a>." % users.create_login_url("/zipme")) def main(): application = webapp.WSGIApplication( [('/zipme', ZipMaker)], debug=False) wsgiref.handlers.CGIHandler().run(application) if __name__ == "__main__": main()
这段代码会通过google的身份认证来判断访问者是否可以下载代码,所以不用担心代码泄漏。不过还是希望Google可以提供代码备份和数据库备份的功能。
如果照你所修改的部份...有可能會被任意下載原始碼
www.google.coma/a/mydomain 的用戶 可以另外開一個app帳戶, 請mydomain的管理者邀請
Administration
Developers <== Invite a Developer to Collaborate on this Application:
Enter a complete email address
app.ymal
改为
- url: /zipme
script: zipme.py
login: admin
## if users.is_current_user_admin():
##else:
## self.response.headers['Content-Type'] = 'text/html'
## self.response.out.write("<a href=\"%s\">You must be admin</a>." %
## users.create_login_url("/zipme"))
这些部分注释掉就可以正常使用了。
为什么不支持下载呢?
为什么不支持下载呢?
为什么不支持下载呢?
查了很多资料也没看到解释
为什么不支持下载呢?
为什么不支持下载呢?
为什么不支持下载呢?
为什么不支持下载呢?
为什么不支持下载呢?
查了很多资料也没看到解释
C:\Documents and Settings\a>appcfg.py ?
C:\Program Files\Google\google_appengine\appcfg.py:40: DeprecationWarning: the
ha module is deprecated; use the hashlib module instead
DIR_PATH,
Usage: appcfg.py [options]
appcfg.py: error: Unknown action '?'
Action must be one of:
cron_info: Display information about cron jobs.
download_data: Download entities from datastore.
help: Print help for a specific action.
request_logs: Write request logs in Apache common log format.
rollback: Rollback an in-progress update.
update: Create or update an app version.
update_cron: Update application cron definitions.
update_indexes: Update application indexes.
upload_data: Upload data records to datastore.
vacuum_indexes: Delete unused indexes from application.
Use 'help ' for a detailed description.
C:\Documents and Settings\a>
可以下数据的
我照着做.怎么404了
不能完成身份认证?什么意思?
Google Apps用户是不能完成身份认证的,早前我在我的博客上就说过这事,这是GOOGLE 设计帐户机制时的一个缺陷。
在本地测试能把所有文件都打包,但实际在Google服务器上就不行了。我想是Google限制了程序访问某些文件(.yaml)和文件夹(静态文件夹)。 Google Apps的问题,就是,如果你申请Google App Engine的时候,是用一个Google Apps 帐户(而非普通Google帐户)申请的,就会出现问题。这个问题我大致打探清楚了。以后会详细说一说。
“<”确实需要稍微修改一下,你说的Google Apps用户,我不清楚是什么问题,不过我这里备份代码是成功的,就是缺少静态文件,估计是上传的时候把程序代码和静态文件放不同的服务器上了。
晕,你的留言系统自动把"<"改成"<"了。
几个问题: 1. 倒数11行,那两个“<”需要改成“<”。 2. 只能备份程序文件,不能备份静态文件。 3. 如果是 Google Apps 用户,无法正常登录。即使先通过 www.google.coma/a/mydomain 登录,也不能访问 zipme,依然提示未登录。
的确,这个功能很有实用,很有必要!