Python script to automatically back up the database to Dropbox

Source: Internet
Author: User
Tags dbx
This article will share with you the code used by the author to automatically back up the dropbox network disk from the mysql database using the python script. it is very simple and practical. if you need it, you can refer to the following code, A major event happened, that is, GitLab's O & M personnel accidentally deleted the production data. although GitLab has already prepared five backup mechanisms, they still lost nearly six hours of user data, especially the loss of their reputation. Think about this blog becomin' Charles, which has not been backed up completely. it's really cool. it's mainly because this is my personal blog, but it's been stuck for nearly ten years, if it is lost, it is still very sad.

My wife is learning Python Programming recently. I am teaching her. In fact, I am a PHP programmer and do not like Python at all. but to be honest, if a layman learns programming, python is indeed much more friendly than PHP, and she can only be recommended to learn Python. With this opportunity, I decided to learn Python programming myself, so I decided to use Python to make an automatic backup script for the database. For the backup location, use Dropbox. because my server is provided by Linode, the US fremont Data Center is suitable for US storage services. I have written the following code: Python:

#!/usr/bin/python#coding:utf-8  import sysimport osfrom yamlimport loadfrom datetime import datetimeimport dropboxfrom dropbox.filesimport WriteModefrom dropbox.exceptions import ApiError, AuthError  if len(sys.argv) < 2:  print >>sys.stderr, "Usage: %s 
 
  " % 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: jdkgjdkjg# databases:#  - host:  localhost#   port:  3306#   user:  user#   pass:  password#   name:  database1#   charset: utf8#  - 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" % filepath  os.system(cmd)  filepath = filepath + '.gz'  dbx = dropbox.Dropbox(conf['dropbox-token'])  backuppath = "%s/%s/%s/%s" % (      conf['remote-backup-path'],    # remote path      datetime.now().strftime("%Y%m%d"), # date string      conf['server-name'],       # server name      filename + '.gz')  with open(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'))    except ApiErroras err:      # This checks for the specific error where a user doesn't have      # enough Dropbox space quota to upload this file      if (err.error.is_path() and          err.error.get_path().error.is_insufficient_space()):        sys.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 these requirements:

Use mysqldump to back up the database locally

Configuration files should be supported and multiple databases can be configured

Can be uploaded to Dropbox

To meet these requirements, the first problem encountered was how to support the configuration file, a search, the original Python has a default ConfigParser, which can complete this task, but the normal thing is quite disgusting, the configuration file must be in the unit of [Section. In fact, my configuration obviously has some global configurations, and the various information of the database is repeated multiple times. this configuration file has poor nesting capabilities and requires two layers of structure, which is disgusting. So I went to the Internet to search for the configuration file format. many articles compared the advantages and disadvantages of various configuration files. In fact, this article is quite a lot and I thought about it, in the future, I may write articles to talk about my own feelings. In the end, many articles agree that YAML is the most perfect configuration file. So I also decided to use this, and there was also a ready-made class library, namely PyYAML, which was very convenient. the two functions load and dump directly changed the file to the dict format.

The second challenge was to upload Dropbox. later, I found that the official website provided a wide range of APIs and there was an SDK directly. (to my eye, the official website did not have a PHP SDK, are you not going to see this ?), After studying the SDK usage, I found that there was a code example directly, so I copied it directly to my code and completed 50% of the code instantly!

After the code is complete, I found that writing code does not take much time. besides, I learned the Python method and complained about the difficulty of using Python Documentation. I found that, in fact, the best way is to use help to query the API in the interactive Shell, and then assist with official documentation. This is a refresh of what I used to know. In practice, I feel pretty good. Python package manager pip is also very useful.

pip install PyYAMLpip install dropbox

For more articles about how to automatically back up a database to Dropbox using Python scripts, see PHP!

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.