Python script implementation automatically backs up the database to Dropbox

Source: Internet
Author: User
Tags dbx
Recently, a big event happened, that is, GitLab's operations have accidentally deleted the production of data, although GitLab has been appalling prepared five backup mechanisms, but still caused them to lose nearly 6 hours of user data, especially the loss of their reputation is simply immeasurable. Reflect on, this blog becomin ' Charles, is also no perfect backup, is really cold sweat straight to take, mainly considering this is my personal blog, but think that has persisted for almost ten years, if really lost, or very sad.

Right, my wife is learning Python programming, I'm teaching her, actually, I'm a PHP programmer, and I don't like python at all, but to be honest, a layman learns to program, Python is really much friendlier than PHP and can only recommend her to learn Python. Well, by this opportunity, I decided to learn Python programming myself, so I decided to use Python to do a database automatic backup script. Backup location, just use Dropbox to do it, because my server is Linode provided, the United States Fremont Room, select the United States storage services, more appropriate. Here is my code, Python small white, please advise:

#!/usr/bin/python#coding:utf-8 import sysimport osfrom yamlimport loadfrom datetime import Datetimeimport Dropboxfrom Dr Opbox.filesimport writemodefrom dropbox.exceptions Import apierror, autherror if Len (SYS.ARGV) < 2:print >>sys  . stderr, "Usage:%s <config_file>"% sys.argv[0] Sys.exit (0) conf = load (file (sys.argv[1], ' R ')) # config file is A YAML looks like#---# server-name:127.0.0.1# local-backup-path:/tmp# remote-backup-path:/backup# dropbox-token:jdkg jdkjg# databases:#-host:localhost# port:3306# user:user# pass:password# name:database1# charset:ut f8#-host:localhost# port:3306# user:user2# pass:password2# name:database2# Charset:utf8 for Dbin conf[' databases ': filename = "%s_%s.sql"% (db[' name '), DateTime.Now (). Strftime ("%y%m%d-%h-%m-%s")) filepath = "%s/%s      "% (conf[' local-backup-path '), filename) cmd =" mysqldump-h%s-u%s-p%s-p%s--single-transaction%s >%s "% ( db[' Host '], db[' user'], db[' Pass ', db[' Port '], db[' name '], filepath) os.system (cmd) cmd = "gzip%s"% Filepat H Os.system (cmd) filepath = filepath + '. Gz ' dbx = Dropbox. Dropbox (conf[' dropbox-token ') backuppath = "%s/%s/%s/%s"% (conf[' Remote-backup-path '], # remote Path date Time.now (). Strftime ("%y%m%d"), # Date string conf[' Server-name '], # server name filename + '. Gz ') with op  En (filepath, ' RB ') as F:time = DateTime.Now (). Strftime ("%y-%m-%d%h:%m:%s") Print (Time + "uploading" + filepath + "To Dropbox as" + backuppath) Try:dbx.files_upload (F.read (), Backuppath, Mode=writemode (' overwrite ')) excep  T Apierroras err: # This checks for the specific error where a user doesn ' t has # enough Dropbox space quota to Upload this file if (Err.error.is_path () and Err.error.get_path (). Error.is_insufficient_space ()): Sy S.exit ("Error:cannot back up;      Insufficient space. ") Elif Err.user_message_text: Print (Err.user_message_text) sys.exit () Else:print (ERR) sys.exit () 

Briefly describe the idea of this code, this program should meet this several requirements:

Use mysqldump to back up a database to a local

Configuration files should be supported, allowing multiple databases to be configured

Upload to Dropbox.

In order to complete these requirements, the first encounter of the problem is how to support the configuration file, a search, the original Python has a default configparser, you can complete this task, but the normal thing is more disgusting, the configuration file must be in [section] as a unit organization. In fact, my configuration obviously some global configuration, there is a database of various information is repeated repeatedly, this configuration file, nesting ability is simply bad, must two layers of structure, it is disgusting. So I go online search configuration file format, a lot of articles compared the advantages and disadvantages of various configuration files, in fact, this article quite a lot of, I think, maybe I can also write an article about my own feelings. Anyway, there are many articles at the end of the recognition that YAML is the most perfect configuration file. So I also decided to use this, sure enough, there is a ready-made class library, is Pyyaml, special convenience, on both the function load and dump, directly to the file into the dict format.

The second problem is uploading Dropbox, later found that the official provides a very rich API, and directly have the SDK, (let me envy is, the official did not have PHP SDK, so not to be seen?) ), study the SDK usage, found that there is a code example directly, so directly copied into my code, the instant completion of the code 50%, cool!

After the whole code was done, I found that it didn't take much time to write the code, and, I learned the Python way, I used to complain about the Python document is difficult to use, I found that, in fact, the best way is actually in the interactive Shell, with help to query the API, and then assisted with official documents, Is the more correct way. It's a place where I've known each other before. It feels good to practice. Python's Package Manager Pip is also very useful.

Pip Install Pyyamlpip install Dropbox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.