From bbd2de679292028a1bf4298eeb6002d831dabe17 Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Mon, 14 Oct 2024 12:15:11 -0700 Subject: [PATCH] Update for log rotation and backups --- Dockerfile | 6 ++-- README.md | 10 +++--- local-dev.sh | 6 ++-- pipeline/automated-build.yml | 70 ------------------------------------ scripts/entrypoint.sh | 26 +++++++------- scripts/install-php74.sh | 2 +- scripts/install-php80.sh | 2 +- scripts/install-php81.sh | 2 +- scripts/install-php82.sh | 2 +- scripts/install-php83.sh | 2 +- scripts/log-rotate.sh | 26 ++++++++++++++ scripts/mysql-backup.sh | 13 +++++++ 12 files changed, 68 insertions(+), 99 deletions(-) delete mode 100644 pipeline/automated-build.yml create mode 100644 scripts/log-rotate.sh create mode 100644 scripts/mysql-backup.sh diff --git a/Dockerfile b/Dockerfile index 5523fb5..4238190 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,9 @@ FROM almalinux/9-base ARG PHPVER=81 -#RUN dnf update -y && dnf upgrade -y RUN dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y RUN dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm -#RUN dnf update -y && dnf upgrade -y -RUN dnf install -y httpd mod_ssl wget procps +RUN dnf update -y && dnf upgrade -y +RUN dnf install -y httpd mod_ssl wget procps cronie RUN openssl req -newkey rsa:2048 -nodes -keyout /etc/pki/tls/private/localhost.key -x509 -days 3650 -subj "/CN=localhost" -out /etc/pki/tls/certs/localhost.crt RUN mkdir /run/php-fpm/ RUN mkdir /scripts @@ -19,5 +18,6 @@ COPY ./configs/prod-php.ini /etc/php.ini COPY ./configs/phpinfo.php /var/www/html/ COPY ./configs/mariadb.repo /etc/yum.repos.d/ COPY ./configs/index.php /var/www/html/ +RUN echo "15 */12 * * * root /scripts/log-rotate.sh" >> /etc/crontab RUN yum clean all ENTRYPOINT [ "/scripts/entrypoint.sh" ] diff --git a/README.md b/README.md index 1eddbac..40fe129 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ __You can then run a development version of the server by running the following ```console mkdir -p local-development/domain.tld cd local-development/domain.tld -mkdir {user,db,logs} -docker run -d -it -p 80:80 -p 443:443 -e PHPVER=81 -e environment=DEV --mount type=bind,source="$(pwd)"/user,target=/home/myuser --mount type=bind,source="$(pwd)"/db,target=/var/lib/mysql -e uid=30001 -e user=myuser -e domain=domain.tld -e serveralias=www.domain.tld --name local-dev repo.anhonesthost.net/cloud-hosting-platform/cac:latest +mkdir {user,logs} +docker run -d -it -p 80:80 -p 443:443 -e PHPVER=81 -e environment=DEV --mount type=bind,source="$(pwd)"/user,target=/home/myuser -v"$name-mysql":/var/lib/mysql -e uid=30001 -e user=myuser -e domain=domain.tld -e serveralias=www.domain.tld --name local-dev repo.anhonesthost.net/cloud-hosting-platform/cac:latest ``` *This will start the processes needed to run sites locally.* @@ -40,7 +40,7 @@ wp core download You should be able to then go into your browser and go to https://localhost (accept the SSL warning if it appears) and follow the prompts to setup the site. -The database credentials are shown in the /var/lib/mysql/creds file, which we had *cat* in the commands above. +The database credentials are shown in the /home/```$user```/mysql_creds file, which we had *cat* in the commands above. They will also be stored in your user directory. ### PHPVER ### *74* - PHP 7.4 @@ -62,11 +62,11 @@ __Optional Tags__ ### Helpful Notes ### -* On your first creation of a dev instance, you will be dumped to the logs output. Hit ```ctrl + c``` to exit the running process. +* A cron is set up in the container to backup the database every 15 minutes to your user's directory. * If you want to restart the instance again, run ```docker start {name-of-your-container}``` in the example, *name-of-your-cintainer* is *local-dev* * To stop a restarted instance, run ```docker stop {name-of-your-container}``` * To view log stream from container, run ```docker logs -f {name-of-your-container}``` -* To delete a container, run ```docker rm {name-of-your-container}``` *__Note:__ this does not delete the files in public_html or database, as those are store in your system* +* To delete a container, run ```docker rm {name-of-your-container}``` *__Note:__ this does not delete the files in user directory or database, as those are store in your system* * To view running containers, run ```docker ps``` * To view all created containers, run ```docker ps --all`` * To view all container images downloaded on your system, run ```docker images``` \ No newline at end of file diff --git a/local-dev.sh b/local-dev.sh index a07d48f..2677e87 100755 --- a/local-dev.sh +++ b/local-dev.sh @@ -44,11 +44,11 @@ fi echo "Building Docker Image..." user=$(whoami) uid=$(id -u) -if [ ! -d "$root_path/web" ]; then - mkdir -p "$root_path/web"; +if [ ! -d "$root_path/user" ]; then + mkdir -p "$root_path/user"; fi $check_docker volume create "$name-mysql" -$check_docker run --pull=always -d -p "$http_port":80 -p "$https_port":443 -e PHPVER=$phpver -e environment=DEV --mount type=bind,source="$root_path"/web,target=/home/"$user"/public_html -v"$name-mysql":/var/lib/mysql -e uid="$uid" -e user="$user" -e domain="$name-local.dev" --name "$name" repo.anhonesthost.net/cloud-hosting-platform/cac:latest +$check_docker run --pull=always -d -p "$http_port":80 -p "$https_port":443 -e PHPVER=$phpver -e environment=DEV --mount type=bind,source="$root_path"/user,target=/home/"$user" -v"$name-mysql":/var/lib/mysql -e uid="$uid" -e user="$user" -e domain="$name-local.dev" --name "$name" repo.anhonesthost.net/cloud-hosting-platform/cac:latest echo "Creating management scripts in root directory..." echo "#!/usr/bin/env bash" > "$root_path/instance_start" echo "docker start $name" >> "$root_path/instance_start" diff --git a/pipeline/automated-build.yml b/pipeline/automated-build.yml deleted file mode 100644 index 9bece51..0000000 --- a/pipeline/automated-build.yml +++ /dev/null @@ -1,70 +0,0 @@ ---- -resources: -- name: cac - type: git - source: - uri: https://repo.anhonesthost.net/cloud-hosting-platform/cloud-apache-container.git - branch: trunk - -- name: build-cac-74 - type: docker-image - source: - repository: registry.dnspegasus.net/cac - tag: 74 - -- name: build-cac-80 - type: docker-image - source: - repository: registry.dnspegasus.net/cac - tag: 80 - -- name: build-cac-81 - type: docker-image - source: - repository: registry.dnspegasus.net/cac - tag: 81 - -- name: build-cac-82 - type: docker-image - source: - repository: registry.dnspegasus.net/cac - tag: 82 - -jobs: - - name: publish-cac-74 - plan: - - get: cac - trigger: true - - put: build-cac-74 - params: - build: cac - build_args: - PHPVER: 74 - - name: publish-cac-80 - plan: - - get: cac - trigger: true - - put: build-cac-80 - params: - build: cac - build_args: - PHPVER: 80 - - name: publish-cac-81 - plan: - - get: cac - trigger: true - - put: build-cac-81 - params: - build: cac - build_args: - PHPVER: 81 - - name: publish-cac-82 - plan: - - get: cac - trigger: true - - put: build-cac-82 - params: - build: cac - build_args: - PHPVER: 82 - \ No newline at end of file diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 3185754..b9e2a41 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash if [ -z "$PHPVER" ]; then PHPVER="81"; @@ -7,6 +7,8 @@ fi adduser -u $uid $user mkdir -p /home/$user/public_html +mkdir -p /home/$user/logs +mkdir -p /home/$user/logs/{apache,system} chown -R $user:$user /home/$user chmod -R 755 /home/$user @@ -16,17 +18,18 @@ chmod -R 755 /home/$user /scripts/create-vhost.sh /scripts/create-php-config.sh +ln -s /etc/httpd/logs /home/$user/logs/apache +ln -s /var/log /home/$user/logs/system + /usr/sbin/httpd -k start /usr/sbin/php-fpm -y /etc/php-fpm.conf if [[ $environment == 'DEV' ]]; then echo "Starting Dev Deployment" - dnf install -y cronie - /usr/sbin/crond mkdir -p /home/$user/public_html/_db_backups dnf install -y MariaDB-server MariaDB-client memcached nohup mysqld -umysql & - if [ ! -f /var/lib/mysql/creds ]; then + if [ ! -f /home/$user/mysql_creds ]; then echo "Give MySQL a chance to finish starting..." sleep 10 mysql_user=$(tr -dc A-Za-z0-9 /home/$user/public_html/_db_backups/$mysql_db.$dt.sql" >> /etc/crontab - echo "*/30 * * * * root /usr/bin/find /home/$user/public_html/_db_backups/ -type f -mmin +360 -delete" >> /etc/crontab - -type f -mmin +360 - echo "MySQL User: "$mysql_user > /var/lib/mysql/creds - echo "MySQL Password: "$mysql_password >> /var/lib/mysql/creds - echo "MySQL Database: devdb_"$mysql_db >> /var/lib/mysql/creds - cat /var/lib/mysql/creds + echo "*/15 * * * * root /scripts/mysql-backup.sh $user $mysql_db" >> /etc/crontab + echo "MySQL User: "$mysql_user > /home/$user/mysql_creds + echo "MySQL Password: "$mysql_password >> /home/$user/mysql_creds + echo "MySQL Database: devdb_"$mysql_db >> /home/$user/mysql_creds + cat /home/$user/mysql_creds fi /usr/bin/memcached -d -u $user @@ -53,7 +53,7 @@ fi if [[ $environment == 'PROD' ]]; then sed -r -i 's/;session.save_path="localhost:11211/session.save_path="memcache:11211/' /etc/php.d/50-memcached.ini fi - +/usr/sbin/crond tail -f /etc/httpd/logs/* exit 0 diff --git a/scripts/install-php74.sh b/scripts/install-php74.sh index e306ff8..f3a2da9 100644 --- a/scripts/install-php74.sh +++ b/scripts/install-php74.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash dnf module enable php:remi-7.4 -y dnf install -y php php-fpm php-mysqlnd php-xml php-pecl-zip php-sodium php-soap php-xmlrpc \ php-pecl-redis5 php-pecl-memcached php-pecl-memcache php-pecl-ip2location php-pecl-imagick php-pecl-geoip \ diff --git a/scripts/install-php80.sh b/scripts/install-php80.sh index 039ca21..b57f3aa 100644 --- a/scripts/install-php80.sh +++ b/scripts/install-php80.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash dnf module enable php:remi-8.0 -y dnf install -y php php-fpm php-mysqlnd php-xml php-pecl-zip php-sodium php-soap php-pecl-xmlrpc \ php-pecl-redis5 php-pecl-memcached php-pecl-memcache php-pecl-ip2location php-pecl-imagick php-pecl-geoip \ diff --git a/scripts/install-php81.sh b/scripts/install-php81.sh index 550dd68..69f3ef6 100644 --- a/scripts/install-php81.sh +++ b/scripts/install-php81.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash dnf module enable php:remi-8.1 -y dnf install -y php php-fpm php-mysqlnd php-xml php-pecl-zip php-sodium php-soap php-pecl-xmlrpc \ php-pecl-redis5 php-pecl-memcached php-pecl-memcache php-pecl-ip2location php-pecl-imagick php-pecl-geoip \ diff --git a/scripts/install-php82.sh b/scripts/install-php82.sh index c462b2d..9200760 100644 --- a/scripts/install-php82.sh +++ b/scripts/install-php82.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash dnf module enable php:remi-8.2 -y dnf install -y php php-fpm php-mysqlnd php-xml php-pecl-zip php-sodium php-soap php-pecl-xmlrpc \ php-pecl-redis5 php-pecl-memcached php-pecl-memcache php-pecl-ip2location php-pecl-imagick php-pecl-geoip \ diff --git a/scripts/install-php83.sh b/scripts/install-php83.sh index 9044670..f63a756 100644 --- a/scripts/install-php83.sh +++ b/scripts/install-php83.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash dnf module enable php:remi-8.3 -y dnf install -y php php-fpm php-mysqlnd php-xml php-pecl-zip php-sodium php-soap php-pecl-xmlrpc \ php-pecl-redis5 php-pecl-memcached php-pecl-memcache php-pecl-ip2location php-pecl-imagick php-pecl-geoip \ diff --git a/scripts/log-rotate.sh b/scripts/log-rotate.sh new file mode 100644 index 0000000..03aa61d --- /dev/null +++ b/scripts/log-rotate.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +# Set the log directory +LOG_DIR="/etc/httpd/logs" + +# Get current date +DATE=$(date +%Y%m%d) + +# Rotate access log +if [ -f "$LOG_DIR/access_log" ]; then + cp "$LOG_DIR/access_log" "$LOG_DIR/access_log.$DATE" + cat /dev/null > "$LOG_DIR/access_log" +fi + +# Rotate error log +if [ -f "$LOG_DIR/error_log" ]; then + cp "$LOG_DIR/error_log" "$LOG_DIR/error_log.$DATE" + cat /dev/null > "$LOG_DIR/error_log" +fi + +# Compress logs older than 3 days +find "$LOG_DIR" -name "*.log.*" -type f -mtime +3 -exec gzip {} \; + +# Delete logs older than 7 days +find "$LOG_DIR" -name "*.log.*" -type f -mtime +7 -delete + diff --git a/scripts/mysql-backup.sh b/scripts/mysql-backup.sh new file mode 100644 index 0000000..f7ef97b --- /dev/null +++ b/scripts/mysql-backup.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +user=$1 +mysql_db=$2 +dt=$(date +%y%m%d-%T) +if [ ! -d /home/$user/_db_backups ]; then + mkdir -p /home/$user/_db_backups +fi + +/usr/bin/mysqldump $mysql_db > /home/$user/_db_backups/$mysql_db.$dt.sql" +/usr/bin/find /home/$user/_db_backups/ -type f -mmin +360 -delete" + +exit 0 \ No newline at end of file