Python backs up important EC2 files and MySQL databases to S3

Source: Internet
Author: User

Today, I tried to use the boto tool to back up files to S3 in python. If you don't need to talk about it, go to the Code:

1. Back up important files to S3:


[Python]
Import OS
Connected = 0
Def connect ():
Access_key = 'yourkey
Secret_key = 'yourkey'
From boto. s3.connection import S3Connection
Global conn
Conn = S3Connection (access_key, secret_key)
Global connected
Connected = 1
 
Def put (fileName, bucketName ):
If connected = 0:
Print 'not ccted! '
Elif connected = 1:
Local_file = fileName. strip ()
Bucket = bucketName. strip ()
From boto. s3.key import Key
B = conn. get_bucket (bucket)
K = Key (B)
K. key = local_file
K. set_contents_from_filename (local_file)

If _ name _ = '_ main __':
Connect ()
SourceFolder = '/var/www/www.ttgrow.com/ttgrow/photos/storyPhotos'
Print 'Story Photo sync in progress'
For root, dirs, files in OS. walk (sourceFolder ):
For file in files: www.2cto.com
Print ''+ str (OS. path. join (root, file ))
Put (OS. path. join (root, file), 'ttgrow-photo ')
SourceFolder = '/var/www/www.ttgrow.com/ttgrow/photos/thumbnails'
Print 'thumbnail sync in progress'
For root, dirs, files in OS. walk (sourceFolder ):
For file in files:
Print ''+ str (OS. path. join (root, file ))
Put (OS. path. join (root, file), 'ttgrow-photo ')
Print 'finished'

2. Back up the mysql database to S3:

[Python]
Import OS
Connected = 0
Def connect ():
Access_key = 'yourkey'
Secret_key = 'yourkey'
From boto. s3.connection import S3Connection
Global conn
Conn = S3Connection (access_key, secret_key)
Global connected
Connected = 1
 
Def put (fileName, bucketName ):
If connected = 0:
Print 'not ccted! '
Elif connected = 1:
Local_file = fileName. strip ()
Bucket = bucketName. strip ()
From boto. s3.key import Key
B = conn. get_bucket (bucket)
K = Key (B)
K. key = local_file
K. set_contents_from_filename (local_file)

If _ name _ = '_ main __':
From datetime import datetime
Import OS
Temp = datetime. today ()
FileName = '/tmp/dbbak-' + str (temp. year) + '-' + str (temp. month) + '-' + str (temp. day) + '-' + str (temp. hour) + '-' + str (temp. minute) + '. SQL'
OS. system ("mysqldump-h YOUR_RDS_LOCATION-u USRNAME-pPASSWORD DBNAME>" + fileName)
Print 'backup DB finished'
Connect ()
Put (fileName, 'ttgrow-db ')
 
Print 'upload to S3 finished'
Add the execution script to the timer and you can execute it regularly every day :)

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.