Created
March 22, 2013 13:36
-
-
Save anonymous/5221322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf8 -*- | |
# by c2249f8c70c4870ea670dc691ca4a64e | |
import os | |
import sys | |
import tempfile | |
import pickle | |
import yaml | |
from dropbox.client import DropboxClient | |
from dropbox.session import DropboxSession | |
from dropbox.rest import ErrorResponse | |
config = yaml.load(open(os.path.join(os.path.abspath(sys.path[0]), 'config.yaml'))) | |
def upload2dropbox(): | |
session = DropboxSession( | |
config['dropbox']['key'], | |
config['dropbox']['secret'], | |
'app_folder') | |
tokenfile = os.path.join(os.path.abspath(sys.path[0]), '.access_token') | |
try: | |
session.set_token(*pickle.load(open(tokenfile))) | |
except IOError: | |
authorize_url = session.build_authorize_url(session.obtain_request_token()) | |
raw_input('Visit: %s' % authorize_url) | |
session.obtain_access_token() | |
pickle.dump( | |
(session.token.key, session.token.secret), | |
open(tokenfile, 'wb')) | |
cli = DropboxClient(session) | |
size = os.path.getsize(config['package']['name']) | |
uploader = cli.get_chunked_uploader(open(config['package']['name'], 'rb'), size) | |
while uploader.offset < size: | |
try: | |
uploader.upload_chunked() | |
except ErrorResponse, e: | |
print e | |
result = uploader.finish(config['package']['name']) | |
print 'upload {path}({size}) successful!'.format(**result) | |
def main(): | |
os.chdir(tempfile.mkdtemp()) | |
for command in config['commands']: | |
os.system(command) | |
# package | |
os.system('7z a %s %s -p%s' % ( | |
config['package']['name'], | |
' '.join(config['backup_list']), | |
config['package']['password'])) | |
# upload | |
upload2dropbox() | |
# clean | |
os.system('rm -rf %s' % os.getcwd()) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commands: | |
- /usr/bin/crontab -l > crontab.txt | |
- /usr/local/bin/redis-cli save | |
- /opt/lampp/bin/mysqldump --all-databases > mysql.txt | |
backup_list: | |
- crontab.txt | |
- mysql.txt | |
- /etc/rc.local | |
- /etc/supervisord.conf | |
- /root | |
- /opt/lampp/etc | |
- /opt/lampp/htdocs | |
dropbox: | |
key: *** | |
secret: *** | |
package: | |
name: ***.7z | |
password: *** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment