Skip to content

Commit 0385cfc

Browse files
committed
first commit
1 parent f2f2d4e commit 0385cfc

File tree

16 files changed

+625
-0
lines changed

16 files changed

+625
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.env
2+
/logs/*
3+
/test
4+
/rootfs/config
5+
/.idea
6+
/rootfs/cn_s3_api/__pycache__
7+
.DS_Store

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM debian:12
2+
3+
ENV TIMEZONE=Europe/Paris \
4+
LANGUAGE=fr_FR.UTF-8 \
5+
LANG=fr_FR.UTF-8 \
6+
TERM=xterm \
7+
DEBFULLNAME="Libertech suncS3" \
8+
DEBEMAIL="product@libertech.Fr"
9+
10+
RUN apt-get clean -yq
11+
RUN apt-get update -yq
12+
RUN apt-get upgrade -yq
13+
RUN apt-get install --no-install-recommends -yq ca-certificates python3 python3-pyinotify python3-boto3 gettext
14+
15+
COPY ./rootfs /
16+
17+
ENTRYPOINT /docker-entrypoint.sh

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
app:
3+
env_file: .env
4+
build: .
5+
container_name: "sync"
6+
volumes:
7+
- /home/docker/bareos-sd/data/storage:/data
8+
- ./logs:/var/log/s3sync

env.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
S3_ENDPOINT=https://s3.rbx.io.cloud.ovh.net/
2+
S3_KEY_ID=xx
3+
S3_KEY_SECRET=xx
4+
S3_REGION=RBX
5+
S3_BUCKET=My-Bucket
6+
DIR_SOURCE=/data
7+
S3_DIR_DEST=/data-sd1
8+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3>=1.15.0

rootfs/checkdir.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import os.path
2+
from cn_s3_api.api import CNS3Api
3+
import argparse
4+
import configparser
5+
import logging
6+
7+
8+
__CONFIG__=configparser.RawConfigParser()
9+
__SECTION__=''
10+
def read_config(file):
11+
with open(file) as f:
12+
file_content = f.read()
13+
__CONFIG__.read_string(file_content)
14+
return __CONFIG__
15+
16+
def config(section,key,default=''):
17+
c=__CONFIG__[section]
18+
return c.get(key,default)
19+
20+
def get_config():
21+
items=__CONFIG__.items('config')
22+
data = {}
23+
for k, v in items:
24+
data[k] = v
25+
return data
26+
27+
def connect_s3():
28+
s3_api = CNS3Api(dict(
29+
aws_access_key_id=config(__SECTION__, 'access_key'),
30+
aws_secret_access_key=config(__SECTION__, 'secret_key'),
31+
endpoint_url=config(__SECTION__, 'end_point'),
32+
region_name=config(__SECTION__, 'region'),
33+
))
34+
return s3_api
35+
def copy_to_s3(file):
36+
s3_api = connect_s3()
37+
filename=os.path.basename(file)
38+
s3path=config(__SECTION__,'dir_dest') + "/" + filename
39+
try:
40+
s3_api.upload_file(config(__SECTION__,'bucket'), file, s3path)
41+
except Error:
42+
print("Erreur")
43+
44+
def delete_to_s3(file):
45+
s3_api = connect_s3()
46+
filename = os.path.basename(file)
47+
s3path = config(__SECTION__, 'dir_dest') + "/" + filename
48+
try:
49+
s3_api.remove(config(__SECTION__,'bucket'),s3path)
50+
except Error:
51+
print("Erreur")
52+
53+
def list_s3(dir):
54+
s3_api = connect_s3()
55+
return s3_api.list(config(__SECTION__,'bucket'),dir)
56+
def search_list(key,s3dir_list):
57+
dirname=config(__SECTION__, 'dir_dest')
58+
for i in s3dir_list:
59+
if (i['Key'] == dirname + "/" + key):
60+
return i
61+
return False
62+
def main():
63+
global __SECTION__
64+
parser = argparse.ArgumentParser()
65+
parser.add_argument('--config', help='configFile',default='config')
66+
parser.add_argument('--section', help='a section in the configfile', default='s3')
67+
args = parser.parse_args()
68+
__SECTION__=args.section
69+
read_config(args.config)
70+
format= '%(asctime)s %(message)s'
71+
logging.basicConfig(filename=config(args.section,'logfile'), level=logging.INFO, format=format)
72+
path = config(args.section, 'dir_source')
73+
#Load s3 list
74+
listdest=list_s3(config(__SECTION__, 'dir_dest') + "/")
75+
#list source directory
76+
for f in os.listdir(path):
77+
s=search_list(f,listdest)
78+
if (s != False):
79+
#/compare size
80+
size=os.stat(path +"/" + f).st_size
81+
if (size == s['Size']):
82+
print(f + " OK")
83+
else:
84+
print(f + " KO BAD SIZE" + "s3-size=" + str(s['Size']) + " /size=" + str(size))
85+
copy_to_s3(path +"/" + f)
86+
print(f + " COPIED")
87+
else:
88+
print(f + " KO NOT FOUND")
89+
#copy
90+
copy_to_s3(path +"/" + f)
91+
print(f + " COPIED")
92+
93+
94+
if __name__ == "__main__":
95+
main()

rootfs/cn_s3_api/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)