-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_s3.py
56 lines (47 loc) · 1.37 KB
/
backup_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python
import os
import bz2
import time
import socket
import shutil
import tarfile
import subprocess
import ConfigParser
date = time.strftime("%Y-%m-%d")
config = ConfigParser.RawConfigParser()
config.read('/etc/backupS3.conf')
dst = config.get('global', 'destiny')
bkp_items = config.items('paths')
hostname = socket.gethostname()
s3_bucket = config.get('s3', 'bucket')
def check_dest(dest):
if not os.path.exists(dest):
os.makedirs(dest)
def compress(date, dest, files):
check_dest(dest)
if os.path.exists(files):
if os.path.isfile(files):
data = open(files, 'r').read()
compress = bz2.BZ2File('%s-%s.bz2' % (files, date), 'wb')
compress.write(data)
compress.close()
files = compress.name
shutil.move(files, dest)
return files
elif os.path.isdir(files):
compress = tarfile.open('%s-%s.tar.bz2' % (files, date), 'w:bz2')
compress.add(files)
compress.close()
shutil.move(compress.name, dest)
else:
print "file does'nt exist"
def send_s3(files, bucket):
cmd = subprocess.Popen(['/usr/bin/s3cmd', '--recursive', '--reduced-redundancy', 'put', files+'/', bucket+'/'+hostname+'/']).wait()
for x in os.listdir(files):
os.remove(files+'/'+x)
def main():
for items, objects in bkp_items:
compress(date, dst, objects)
send_s3(dst, s3_bucket)
if __name__ == '__main__':
main()