-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_python_backup.py
More file actions
43 lines (33 loc) · 1.18 KB
/
Copy paths3_python_backup.py
File metadata and controls
43 lines (33 loc) · 1.18 KB
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
from secrets import access_key,secret_key
import os
import tarfile
import boto3
import datetime
import posixpath
directory = "./docroot/"
#Local backup location
temp_back = "/tmp/backup/"
for item in os.listdir(directory):
#retreving the current date
dt = datetime.datetime.now()
#Modifying according to our preffered date-time format .
ts = dt.strftime('%d-%m-%Y-%H-%M')
absPath = posixpath.join(directory,item)
tarName = '{}{}-{}.tar.gz'.format(temp_back,item,ts)
#Lets create the tar file with .gz
tar = tarfile.open(tarName,'w:gz')
tar.add(absPath)
#Creating the tar file.
tar.close()
print('Backup for',absPath,'created')
print()
client = boto3.client('s3',aws_access_key_id= access_key,aws_secret_access_key = secret_key)
for file in os.listdir('/tmp/backup'):
if '.tar' in file:
#Mention your s3 bucket name here:
upload_file_bucket = "s3-bucket-name"
upload_file_key = "backup/{}".format(file)
path = posixpath.join('/tmp/backup/',file)
#Lets upload the tar file to s3 bucket.
client.upload_file(path,upload_file_bucket,upload_file_key)
print('Upload to S3 successfull for',file)