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 :)