Last active
November 8, 2023 08:51
-
-
Save ZiTAL/fa0e2e45883dccaf3cbf89887aea8123 to your computer and use it in GitHub Desktop.
mediawiki: nginx + pretty urls + sql backup + crontab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import sys, os, subprocess, tempfile | |
from datetime import datetime, timedelta | |
def _exec(command): | |
subprocess.run(str(command), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | |
# mysqldump | |
md = "/usr/bin/mysqldump" | |
# how many days to backup | |
backup_days = 15 | |
# today | |
date = datetime.now().strftime("%Y-%m-%d") | |
# db config | |
config = { | |
"user": 'wiki', | |
"passwd": 'wiki', | |
"db": 'wiki' | |
} | |
# dir config | |
tmp = tempfile.mkdtemp() | |
tmp_dir = f"{tmp}/wiki" | |
project_dir = f"{sys.path[0]}/.." | |
backup_dir = f"{project_dir}/backup" | |
tmp_backup_dir = f"{tmp_dir}/backup" | |
# create files backup | |
command = f"cp -R {project_dir} {tmp_dir}" | |
_exec(command) | |
# remove backup dir | |
command = f"rm -rf {tmp_backup_dir}" | |
_exec(command) | |
# create backup dir | |
command = f"mkdir {tmp_backup_dir}" | |
_exec(command) | |
# create db backup | |
command = f"{md} -u {config['user']} -p{config['passwd']} {config['db']} > {tmp_backup_dir}/{config['db']}-{date}.sql" | |
_exec(command) | |
# zip folder | |
command = f"zip -rq {backup_dir}/wiki-{date}.zip {tmp_dir}" | |
_exec(command) | |
# get files from backup | |
files = [os.path.join(backup_dir, f) for f in os.listdir(backup_dir) if os.path.isfile(os.path.join(backup_dir, f))] | |
files.sort(key=lambda x: os.path.getmtime(x), reverse=True) | |
# only keep last backups | |
current_date = datetime.now() | |
delta = timedelta(days=backup_days) | |
for i in range(len(files)): | |
file_mtime = datetime.fromtimestamp(os.path.getmtime(files[i])) | |
age = current_date - file_mtime | |
if age >= delta: | |
command = f"rm -rf {files[i]}" | |
_exec(command) | |
# remove tmp folder | |
command = f"rm -rf {tmp_dir}" | |
_exec(command) | |
command = f"rm -rf {tmp}" | |
_exec(command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 0 * * * /usr/bin/python3 /home/projects/wiki/scripts/backup.py >> /dev/null 2>> /home/projects/wiki/scripts/backup.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... | |
$wgScriptPath = ""; | |
$wgArticlePath = "/wiki/$1"; | |
$wgUsePathInfo = true; | |
//... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://shorturls.redwerks.org | |
server { | |
server_name wiki.zital.eus wiki.opi5; | |
root /home/projects/wiki; | |
location ~ \.php$ { | |
fastcgi_pass unix:/run/php/php8.1-fpm.sock; | |
fastcgi_index index.php; | |
include fastcgi_params; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
} | |
location / { | |
index index.php; | |
try_files $uri $uri/ /index.php?$args; | |
} | |
location /images { | |
location ~ ^/images/thumb/(archive/)?[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ { | |
try_files $uri $uri/ @thumb; | |
} | |
} | |
location /wiki { | |
include fastcgi_params; | |
fastcgi_param SCRIPT_FILENAME $document_root/index.php; | |
fastcgi_pass unix:/run/php/php8.1-fpm.sock; | |
} | |
location @thumb { | |
rewrite ^/images/thumb/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /thumb.php?f=$1&width=$2; | |
rewrite ^/images/thumb/archive/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /thumb.php?f=$1&width=$2&archived=1; | |
include fastcgi_params; | |
fastcgi_param SCRIPT_FILENAME $document_root/thumb.php; | |
fastcgi_pass unix:/run/php/php8.1-fpm.sock; | |
} | |
location /images/deleted { | |
deny all; | |
} | |
location /backup { deny all; } | |
location /cache { deny all; } | |
location /includes { deny all; } | |
location /languages { deny all; } | |
location /maintenance { deny all; } | |
location /scripts { deny all; } | |
location /serialized { deny all; } | |
location /tests { deny all; } | |
location /vendor { deny all; } | |
location /extensions/MobileFrontend/dev-scripts/ { deny all; } | |
location /extensions/MobileFrontend/tests/ { deny all; } | |
location ~ /.(svn|git)(/|$) { deny all; } | |
location ~ /.ht { deny all; } | |
# location /mw-config { deny all; } | |
error_log /var/log/nginx/wiki-error.log error; | |
access_log /var/log/nginx/wiki-access.log; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
create folders and files:
create backup.py in scripts folder