-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·75 lines (64 loc) · 1.88 KB
/
backup.sh
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh
docker exec -i cookiewiki_web_1 php maintenance/generateSitemap.php --memory-limit=50M --fspath=/data/sitemap/ --urlpath=/sitemap/ --server=https://cookiewiki.org --compress=yes --skip-redirects
function unlock() {
docker exec -i $(docker ps -f label=org.cookie.container=mediawiki --format='{{.ID}}') \
sh -c "sed '/wgReadOnly/d' -i LocalSettings.php"
}
trap unlock EXIT
docker exec -i $(docker ps -f label=org.cookie.container=mediawiki --format='{{.ID}}') \
sh -c "echo '\$wgReadOnly = \"Dumping Database, Access will be restored shortly\";' >> LocalSettings.php"
now=$(date +%F-%H-%M-%S)
mkdir -p temp
docker exec -i $(docker ps -f label=org.cookie.container=mariadb --format='{{.ID}}') \
mysqldump -ppassword mediawiki | gzip -9 > temp/database.sql.gz
tar --transform "s/data\/web/$now\/web/;s/temp/$now\/sql/" -czvf backups/${now}.tar.gz temp/database.sql.gz data/web
ln -sf ${now}.tar.gz backups/latest.tar.gz
rm -r temp
# remove every backup but
# - each per day of last month
# - each per month of last year
# - each per year
find backups/ -type f -name '*.tar.gz' | sort -n | awk -F '[-./]' '
BEGIN{
year=month=day=0;
}
{
if (!year) {
year=$2;
month=$3;
day=$4;
keep[year][month][day]=lastday=$0;
next
}
if (year == $2 && month == $3 && day == $4) {
print lastday;
}
if (year != $2) {
for (m in keep[year]) {
for (d in keep[year][m]) {
if (m == month && d == day) {
continue
}
print keep[year][m][d]
}
}
delete keep[year];
keep[year][month][day]=lastday;
year=$2;
month=$3
}
if (month != $3) {
for (d in keep[year][month]) {
if (d == day) {
continue
}
print keep[year][month][d]
}
delete keep[year][month];
keep[year][month][day]=lastday;
month=$3;
}
day=$4;
keep[year][month][day]=$0;
lastday=$0;
}' | xargs rm -vf