percona容器添加logrotate功能

添加logRotate

拉包

git clone ssh://gavinwei@git.example.com:29418/docker/base

修改脚本

1
2
cd ~/base/ubuntu-py/src/main/docker
cat logRotate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python

import os
import schedule
from apscheduler.schedulers.blocking import BlockingScheduler

def rotate():
os.system('/etc/cron.daily/logrotate')

def logrotate():
try:
scheduler = BlockingScheduler()
scheduler.add_job(rotate, 'cron', hour=0, minute=0)
scheduler.start()
except Exception as e:
print "{}: {}".format('logrotate', e)
print "logrotate: exited"

def main():
logrotate()

if __name__ == "__main__":
main()

修改Dockerfile

1
2
3
4
5
cd ~/base/ubuntu-py/src/main/docker

添加
RUN rm /etc/cron.daily/logrotate
COPY logrotate /etc/cron.daily/

mvn install

1
2
cd ~/base/ubuntu-data
mvn install

这条命令将会下载一些包到~/.m2/repository/com/example/docker/base/ubuntu-data 下面

镜像ubuntu-data依赖ubuntu-py,而镜像percona依赖ubuntu-data,所以,归初修改的logRotate.py脚本最后会出现在percona镜像的窗口中。

mvn clean install

1
2
cd ~/base
mvn clean install

此命令将会生成大量镜像,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
docker ps 
root@uidev:~/percona# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.example.com/percona 0.0.1-SNAPSHOT 4bf97cc1a755 2 minutes ago 951.8 MB
docker.example.com/ubuntu-data 0.0.1-SNAPSHOT e614ede872af 11 minutes ago 795.9 MB
docker.example.com/ubuntu-py 0.0.1-SNAPSHOT 7397b0030230 12 minutes ago 795.9 MB
docker.example.com/varnish 0.0.1-SNAPSHOT aa2377d44ecc 19 hours ago 954.2 MB
docker.example.com/ubuntu-data 0.0.22 55ee02d4cfa6 3 months ago 794.4 MB
docker.example.com/ubuntu-py 0.0.22 d98e886c4808 3 months ago 794.4 MB
docker.example.com/ubuntu-java 0.0.1-SNAPSHOT 313ede731d40 3 months ago 734.1 MB
docker.example.com/ubuntu-ldap 0.0.1-SNAPSHOT 80201502aab7 3 months ago 358.2 MB
docker.example.com/ubuntu-upstart 0.0.1-SNAPSHOT 927c581760a9 3 months ago 268.1 MB
docker.example.com/ubuntu 0.0.1-SNAPSHOT 918e86b5fd29 3 months ago 229.6 MB
docker.example.com/dind 0.0.3 98be25cb5c64 3 months ago 805.7 MB
docker.example.com/ubuntu 14.04.3 af88597ec24b 4 months ago 187.9 MB
docker.example.com/collectd 0.0.6 f46eb5b0649b 4 months ago 655.9 MB

重新build percona镜像

1
2
~/percona/src/main/docker
cat backup.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
#!/usr/bin/python
#coding=utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.executors.pool import ThreadPoolExecutor
import os
import yaml
import time

def fullbackup():
os.system('/usr/local/bin/surrogate -b full')


def incremental():
os.system('if -f /backup/backups/.digext && grep full /backup/backups/.digest ;then /usr/local/bin/surrogate -b inc ; else /usr/local/bin/surrogate -b full ; fi')

yaml_file = '/etc/mysql/conf.d/backup.yaml'
if os.path.exists( '/backup.yaml' ):
yaml_file = '/backup.yaml'

if os.path.exists( yaml_file ):
f = open(yaml_file,'r')
x = yaml.load(f)

executors = {
'default': ThreadPoolExecutor(1),
}
scheduler = BlockingScheduler(executors=executors)

# We can only make full backup now
#------------------Full Backup----------------------------------
if x['backup'].has_key('full') and x['backup']['full'].has_key('type') and x['backup']['full'].has_key('time'):
scheduler.add_job(fullbackup,x['backup']['full']['type'], max_instances=2, **x['backup']['full']['time'])

#------------------Incremental Backup---------------------------
if x['backup'].has_key('incremental') and x['backup']['incremental'].has_key('type') and x['backup']['incremental'].has_key('time'):
scheduler.add_job(incremental,x['backup']['incremental']['type'], max_instances=2, **x['backup']['incremental']['time'])

try:
scheduler.start()
except Exception:
# When there is error in backup.yaml file
while True :
time.sleep(86400)
else:
while True :
time.sleep(86400)

cat backup.yaml

1
2
3
4
5
6
7
8
9
10
backup:
full:
type: "cron"
time:
hour: 1
minute: 0
incremental:
type: "cron"
time:
hour: "*/2"

cat startMysql.sh

1
2
#!/bin/bash
exec dockerStart.py --cfgfile /clustercheck.yaml /startMysql.yaml /backupMysql.yaml

cat bootstrapMysql.sh

1
2
#!/bin/bash
exec dockerStart.py --cfgfile /clustercheck.yaml /bootstrapMysql.yaml /backupMysql.yaml

run ~/scripts/auto.sh