-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paths3.py
42 lines (36 loc) · 1.13 KB
/
s3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import boto
import boto.s3.connection
from boto.s3.key import Key
import os
import time
import datetime
from config import *
conn = boto.connect_s3(
aws_access_key_id = key_id,
aws_secret_access_key = sec_key,
calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
bucket = conn.get_bucket(bucket_name)
def backup():
"""
Backup Database
"""
username = db_user
password = db_pass
database = db_name
date = time.strftime('%Y-%m-%d')
filename = 'backup/%s.sql.gz' % (date)
os.popen("mysqldump -u%s -p%s -h %s -d %s | gzip -c > %s" % (username, password, hostname, database, filename))
upload = Key(bucket)
upload.key = "%s.sql.gz" % (date)
upload.set_contents_from_filename(filename)
print "uploading Backup File %s" % (upload.key)
os.remove(filename)
checkdate = datetime.date.today()-datetime.timedelta(30)
for key in bucket:
if str(key.last_modified.split('T')[0]) < str(checkdate):
print "Deleting 30 Days Old Backup File %s" % (key.name)
bucket.delete_key(key.name)
print "----------------------------------------------------------------------------"
if __name__ == '__main__':
backup()