Skip to content

Commit edbf37c

Browse files
Create s3_python_backup.py
1 parent cc4f62a commit edbf37c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

s3_python_backup.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from secrets import access_key,secret_key
2+
import os
3+
import tarfile
4+
import boto3
5+
import datetime
6+
import posixpath
7+
8+
directory = "./docroot/"
9+
10+
for item in os.listdir(directory):
11+
12+
dt = datetime.datetime.now()
13+
14+
ts = dt.strftime('%d-%m-%Y-%H-%M')
15+
16+
absPath = posixpath.join(directory,item)
17+
18+
tarName = '/tmp/backup/{}-{}.tar.gz'.format(item,ts)
19+
20+
tar = tarfile.open(tarName,'w:gz')
21+
22+
tar.add(absPath)
23+
24+
tar.close()
25+
print('Backup for',absPath,'created')
26+
print()
27+
28+
client = boto3.client('s3',aws_access_key_id= access_key,aws_secret_access_key = secret_key)
29+
for file in os.listdir('/tmp/backup'):
30+
if '.tar' in file:
31+
upload_file_bucket = "private.s3.bucket.com"
32+
upload_file_key = "backup/{}".format(file)
33+
path = posixpath.join('/tmp/backup/',file)
34+
client.upload_file(path,upload_file_bucket,upload_file_key)
35+
print('Upload to S3 successfull for',file)

0 commit comments

Comments
 (0)