Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
自动备份-Dropbox-python-linux

这个文件只是为了阻止access_token成为第一个文件。

这个文件第一行是access_token,在https://www.dropbox.com/developers/apps创建{create}一个应用{app}后(建议权限类型{Permission type}为"应用文件夹"{App folder}),在{OAuth2}栏目下可以生成访问令牌{Generate access token},用生成的{generated}访问令牌{access token}覆盖这个文件里的所有内容并保存。
#! /usr/bin/python
import dropbox
import time
import os
import sys
# 超级混乱 啊啊 就这样吧 懒得改了
# 以下三个文件须和此文件在同一目录 仅支持linux系统
# initialize
origind = os.getcwd()
# get time
sdate = time.strftime('%Y-%m-%d', time.localtime(time.time()))
# create workspace
tmpdir = '/tmp/pybackuper-%s'%sdate
os.system('mkdir %s'%tmpdir)
# zip files
fdir = '%s/files' % tmpdir
os.system('mkdir %s'%fdir)
f2ul = open('%s/folders2upload'%origind, 'r')
flist = f2ul.readlines()
for f in flist:
if ('\n' == f[0]) or ('#' == f[0]):
continue
fp = f.strip()
sys.stdout.write('copying %s .. '%fp)
sys.stdout.flush()
command = 'cp -r "%s" "%s/%s"'%(f.strip(),fdir,f.strip().replace('/','_'))
os.system(command)
print 'Done.'
f2ul.close()
sys.stdout.write('Pack files... ')
sys.stdout.flush()
os.chdir(fdir)
pfilebackup = '%s/filebackup-%s.tar.gz' % (tmpdir,sdate)
command = 'tar -czf ../filebackup-%s.tar.gz *'%(sdate)
os.system(command)
print 'Done.'
# export databases
ddir = '%s/dbs' % tmpdir
os.system('mkdir %s' % ddir)
f4db = open('%s/databases2backup'%origind, 'r')
password = f4db.readline().strip()
dlist = f4db.readlines()
alldb = ' '
for db in dlist:
if ('\n' == db[0]) or ('#' == db[0]):
continue
dbs = db.strip()
dbs = dbs.split()
for adb in dbs:
sys.stdout.write( 'export database %s .. ' % adb )
sys.stdout.flush()
comment = 'mysqldump -uroot -p%s --databases %s > %s/mysql-%s-%s.sql' % (password,adb,ddir,adb,sdate)
os.system( comment )
alldb += '%s ' % adb
print 'Done.'
sys.stdout.write('export all databases ... ')
sys.stdout.flush()
command = 'mysqldump -uroot -p%s --databases %s > %s/mysql-%s.sql'%(password,alldb,ddir,sdate)
os.system( command )
print 'Done.'
pdatabasesf = '%s/mysql-%s.tar.gz' % (tmpdir,sdate)
sys.stdout.write('Pack them... ')
sys.stdout.flush()
os.chdir(ddir)
command = 'tar -czf ../mysql-%s.tar.gz *.sql' % sdate
os.system( command)
print 'Done.'
# upload them
atf = open('%s/access_token'%origind, 'r')
access_token = atf.readline().strip()
client = dropbox.client.DropboxClient(access_token)
sys.stdout.write('Uploading files.. ')
sys.stdout.flush()
f = open(pfilebackup, 'rb')
client.put_file('/files/filebackup-%s.tar.gz'%(sdate), f)
print 'Done.'
sys.stdout.write('Uploading databases.. ')
sys.stdout.flush()
d = open(pdatabasesf, 'rb')
client.put_file('/databases/mysql-%s.tar.gz'%sdate, d)
print 'Done.'
# cleaning
os.system('rm -frd %s'%tmpdir)
root_password
# 包含需要备份的数据库的名字 第一行必须是root的密码 以后每行可以有若干个数据库名 用空格分隔
#包含需要备份的文件夹的**绝对路径**,一行一个
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.