1
0
Fork 0

Adding SD unit: mariadbontmpfs.service

This commit is contained in:
Chl 2022-12-04 20:15:16 +01:00
parent 00290cc1e1
commit 47fe0efef1

View file

@ -0,0 +1,72 @@
# This systemd unit puts the MariaDB/MySQL storage on tmpfs and (tries to) sync
# it back at shutdown.
# Of course, in case of power failure or any problem preventing this
# unit to stop properly and sync back the data to disk : DATA LOSS.
# To install :
# - apt install rsync
# - systemctl stop mariadb
# - copy this file to /etc/systemd/system/mariadbontmpfs.service
# - systemctl enable mariadbontmpfs.service
# - systemctl start mariadbontmpfs.service
# - systemctl start mariadb
# This file is free software licensed under the WTFPL (v2)
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
[Unit]
Description=Put /var/lib/mysql on tmpfs before MariaDB/MySQL starts
Before=mariadb.service mysql.service
ConditionPathIsDirectory=/var/lib/mysql
ConditionPathExists=/usr/bin/rsync
[Install]
WantedBy=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=yes
Restart=no
# We double-check that MariaDB is not running before doing anything
# Per https://www.freedesktop.org/software/systemd/man/systemd.service.html
# when an ExecCondition= command exits with exit code 1 through 254
# (inclusive), the remaining commands are skipped and the unit is not marked as
# failed. However, if an ExecCondition= command exits with 255 or abnormally
# (e.g. timeout, killed by a signal, etc.), the unit will be considered failed
# (and remaining commands will be skipped).
ExecCondition=/bin/sh -c "systemctl --quiet is-active mariadb mysql && echo 'MariaDB or MySQL still active' && exit 255 || exit 0"
# If the ON-DISK directory doesn't exist, simply rename /var/lib/mysql to
# /var/lib/mysql.ON-DISK and recreate /var/lib/mysql
ExecStart=/bin/sh -c "if [ ! -e /var/lib/mysql.ON-DISK ] ; then mv -v /var/lib/mysql /var/lib/mysql.ON-DISK && mkdir -v /var/lib/mysql && chown -v mysql:mysql /var/lib/mysql; fi"
# Mount the tmpfs
# (note: to forcefully limit memory consumption, we can add 'size=1024M' to the mount options)
ExecStart=/bin/mount -v -t tmpfs -o uid=mysql,gid=mysql,mode=0755 none /var/lib/mysql
# Copy the ON-DISK content to the tmpfs
ExecStart=/usr/bin/rsync -avH --delete /var/lib/mysql.ON-DISK/ /var/lib/mysql/
# At stop :
# - Little notice for the logs
ExecStop=/bin/sh -c "echo 'Syncing tmpfs:/var/lib/mysql to disk'"
# - fetch back the in-memory data to disk
ExecStop=/usr/bin/rsync -avH --delete /var/lib/mysql/ /var/lib/mysql.ON-DISK/
# - destroy the tmpfs
ExecStop=/bin/umount -v /var/lib/mysql
# - put back the on-disk storage in its original path
ExecStop=/bin/sh -c "rmdir -v /var/lib/mysql && mv -v /var/lib/mysql.ON-DISK /var/lib/mysql"