Use Python scripts to back up MySQL, Redis, mongodb databases in Linux

Source: Internet
Author: User
Tags datetime mongodb redis

The installation can be performed directly with the PIP:

The code is as follows Copy Code

Pip Install YUNBK



GitHub address is as follows:

Https://github.com/dantezhu/yunbk

And the usage is very simple, such as to back up MySQL, the code is as follows:

  code is as follows copy code
from YUNBK Import YUNBK
from yunbk.backend.local import localbackend
 
Import sh
 
Backend = Localbacke nd ('/data/backup ')
 
with YUNBK (' MySQL ', [backend]) as YBK:
    sh.mysqldump (u= ' root ',
                 All_ Databases=true,
                  _out= "Dump.sql")
    ybk.backup ()


How, simple!
The code above will dump all of MySQL's databases and save them to the local/data/backup directory and name them by category and date. In addition to saving to the local, YUNBK also provides FTP, SFTP, Baidu's BCS, Ali's OSS and many other backup methods.

Briefly describe the design of YUNBK:

In the above code, use the WITH statement to enter a default temporary directory specified by YUNBK, at which time all file writes will be in this directory.
When you call backup, YUNBK calls the upload method of each backend in [backend] and backs up the data
when the with end, the temporary directory created by YUNBK is automatically deleted, leaving no traces.
Need to specifically note that YUNBK is inspired by the Bakthat plug-in, but he uses Amazon as a backup way, in the Celestial Kingdom you know, there are some ways to realize that is not particularly good, code some redundancy, so on their own to achieve a.

Nonsense is not much, list some common examples of how to be blocked:

FTP backup

The code is as follows Copy Code
From YUNBK import YUNBK
From yunbk.backend.ftp import Ftpbackend

Import sh

Backend = ftpbackend (' 127.0.0.1 ', ' user ', ' passwd ', '/data/backup ')

With YUNBK (' MySQL ', [backend]) as YBK:
Sh.mysqldump (u= ' root ',
All_databases=true,
_out= "Dump.sql")
Ybk.backup ()



Ali OSS Backup

The code is as follows Copy Code

From YUNBK import YUNBK
From Yunbk.backend.ali_oss import Ossbackend

Import sh

Backend = ossbackend (' 127.0.0.1 ', ' access_id ', ' secret_access_key ', ' backup_my_data ')

With YUNBK (' MySQL ', [backend]) as YBK:
Sh.mysqldump (u= ' root ',
All_databases=true,
_out= "Dump.sql")
Ybk.backup ()
SFTP and Baidu BCS will not write, we should have been able to write out their own

In addition, the recommended combination of timed execution of the library: Apscheduler together to use, very convenient:

#-*-Coding:utf-8-*-

Import datetime
From Apscheduler.scheduler Import Scheduler
From apscheduler.events import Event_job_error, event_job_missed
Import logging

From YUNBK.YUNBK import YUNBK
From yunbk.backend.local import Localbackend


Logger = Logging.getlogger (' default ')
Logger.addhandler (logging. Streamhandler ())
Logger.setlevel (logging. DEBUG)

Sched = Scheduler (daemonic=false)


def err_listener (EV):
If ev.exception:
Logger.fatal ('%s error. ', str (ev.job), exc_info=true)
Else
Logger.info ('%s Miss ', str (ev.job))


@sched. Cron_schedule (second= ' 1 ')
Def job ():
Logger.debug (Datetime.datetime.now ())
Backend = localbackend ('/data/release/backup/')
With YUNBK (' YBK ', [backend]) as YBK:
f = open (' T2.txt ', ' W ')
F.write (' OK ')
F.close ()
Ybk.backup ()


if __name__ = = ' __main__ ':
Sched.add_listener (Err_listener, Event_job_error | event_job_missed)
Sched.start ()


All right, here we go, I want to be useful to you:

Related Article

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.