From: Hamatoma Date: Sat, 26 Apr 2025 09:36:40 +0000 (+0200) Subject: V0.3.0: improvements X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=6ff029b2078cd4466add0dc6b1ef1e1431d97651;p=nest_ansible.git V0.3.0: improvements --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ec6b54..5c7bf0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ # Change log of + +## V0.3.0: improvements + +## V0.2.0: first working version as nest1 + ## V0.1.0: first working version as ansknife diff --git a/playbooks/nest1_cron.yaml b/playbooks/nest1_cron.yaml new file mode 100644 index 0000000..65f0ee3 --- /dev/null +++ b/playbooks/nest1_cron.yaml @@ -0,0 +1,45 @@ +--- +# Maintains the cron jobs for the nest1 server +- hosts: all + vars: + hostname: "{{ inventory_hostname }}" + path_script: /usr/local/bin + path_script_local: "{{ path_script }}/{{ hostname }}" + hostname_short: Nest1 + vars_files: + - ../var/common.yaml + tasks: + - name: Creates a cron file for clearing the swap files + ansible.builtin.cron: + name: clear the swap files + minute: "42" + hour: "23" + user: root + job: "{{ path_script_local }}/ClearSwap -q" + cron_file: ClearSwap + - name: Save the ownership of a file tree in the file .owner.list + ansible.builtin.cron: + name: store the ownership of a file tree + minute: "39" + hour: "23" + user: root + job: "{{ path_script_local }}/SaveOwner.sh -q" + cron_file: SaveOwner + when: neverEver is defined + - name: Build a a server specific backup + ansible.builtin.cron: + name: builds the backup + minute: "19" + hour: "0" + user: root + job: "{{ path_script_local }}/{{ hostname_short }Backup -q" + cron_file: "{{ hostname_short }}Backup" + when: neverEver is defined + - name: Save the databases + ansible.builtin.cron: + name: Save the web applications + minute: "29" + hour: "0" + user: root + job: "{{ path_script_local }}/SaveDatabases -q" + cron_file: SaveDatabases diff --git a/playbooks/nest1_cron_install.yaml b/playbooks/nest1_cron_install.yaml new file mode 100644 index 0000000..aaac9c0 --- /dev/null +++ b/playbooks/nest1_cron_install.yaml @@ -0,0 +1,30 @@ +--- +# Installs the scripts usesed for the cron jobs +# +# needesd facts (variables): +# hostname: the name of the remote server +# optional facts +# hostname_short: the short name of the remote server. Default: prefix of hostname +- hosts: all + vars: + hostname: "{{ inventory_hostname }}" + path_script: /usr/local/bin + path_script_local: "{{ path_script }}/{{hostname}}" + vars_files: + - ../var/common.yaml + tasks: + - name: create facts + ansible.builtin.set_fact: + hostname_short: "{{ hostname.split('.')[0] }}" + when: hostname_short is not defined + - name: install the scripts in {{ path_script_local }} + ansible.builtin.copy: + src: ../templates.local/local_bin/{{ item }} + dest: "{{ path_script_local }}/{{ item }}" + mode: "0740" + with_items: + - CreateSysInfo + - SaveOwner + - ClearSwap + - SaveDatabases + - "{{ hostname_short }}Backup" \ No newline at end of file diff --git a/tasks/t_sysinfo_create.yaml b/tasks/t_sysinfo_create.yaml new file mode 120000 index 0000000..a6e3c5f --- /dev/null +++ b/tasks/t_sysinfo_create.yaml @@ -0,0 +1 @@ +../../ansknife/tasks.templates/t_sysinfo_create.yaml \ No newline at end of file diff --git a/templates.local/local_bin/ClearSwap b/templates.local/local_bin/ClearSwap new file mode 100644 index 0000000..faa711d --- /dev/null +++ b/templates.local/local_bin/ClearSwap @@ -0,0 +1,16 @@ +#! /bin/bash +LOG=/var/log/local/clearswap.log +VERBOSE=-v +test "$1" = -q && VERBOSE= +PATH=/bin:/usr/bin:/usr/sbin +function Log(){ + local msg=$(date "+%Y.%m.%d-%H:%M:%S: $1") + test -n "$VERBOSE" && echo $msg + echo >>$LOG "$msg" +} + +Log "swapoff:" +info=$(swapoff -a) +Log "logoff result: $info" +info=$(swapon -a) +Log "logon result: $info" diff --git a/templates.local/local_bin/CreateSysInfo b/templates.local/local_bin/CreateSysInfo new file mode 100644 index 0000000..e181eae --- /dev/null +++ b/templates.local/local_bin/CreateSysInfo @@ -0,0 +1,98 @@ +#! /bin/bash +# Ansible controlles. Do not change on remote server. +# +# Save structure data and configuration of the remove server nest1 +DIR_INFO=/srv/www/sys.info/nest1.gemeinwohl-gesellschaft.de +BTRFS_FS= +MK_HOME_TAR=yes +Home_TAR_EXCLUDE="--exclude=www" +USE_ZFS=no +EXCLUDE_HOME="--exclude=jails" +TAG_FILE=.do.not.save +VERBOSE=-v +export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin +test "$1" = -q && VERBOSE= +mkdir -p $DIR_INFO +function Packages(){ + test "$VERBOSE" = -v && echo "= packages" + dpkg -l | sort > $DIR_INFO/packets.installed.txt + apt-mark showmanual | sort >$DIR_INFO/packets.manually.txt + cp /var/log/apt/history.log $DIR_INFO/apt.history.txt +} +function Mount(){ + test "$VERBOSE" = -v && echo "= mount" + mount | grep /dev/ | egrep -v "devpts|hugetlbfs|tmpfs|mqueue" > $DIR_INFO/mount.txt +} +function Devices(){ + test "$VERBOSE" = -v && echo "= device info" + blkid > $DIR_INFO/blkid.txt + lsblk > $DIR_INFO/lsblk.txt + free > $DIR_INFO/free.txt + df -h | grep -v docker > $DIR_INFO/df.txt + test "$VERBOSE" = -v && echo "= btrfs" + rm -f $DIR_INFO/btrfs.subvols.txt + if [ -n "$BTRFS_FS" ]; then + for fs in $BTRFS_FS; do + if [ -d $media/$fs ]; then + echo "= $fs: >>$DIR_INFO/btrfs.subvols.txt" + btrfs subvol list /media/$fs | grep -v snapshot >> $DIR_INFO/btrfs.subvols.txt + fi + done + fi + test "$VERBOSE" = -v && echo "= fdisk" + fdisk -l > $DIR_INFO/fdisk.txt +} +function SystemD(){ + test "$VERBOSE" = -v && echo "= systemctl" + systemctl list-unit-files > $DIR_INFO/systemd.files.txt + systemctl list-units > $DIR_INFO/systemd.units.txt +} +function Trees(){ + test "$VERBOSE" = -v && echo "= directories" + for dir in etc usr/local/bin var/spool/cron/crontabs var/log; do + name=${dir//\//_} + cd /$dir + test -n "$VERBOSE" && echo "= $(pwd)" + opt= + trg=$DIR_INFO/$dir + mkdir -p $trg + #test $dir = var/log && opt=--newer-mtime=$(date --date='yesterday' +%Y%m%d) + #tar czf $DIR_INFO/$name.tgz . $opt + test $dir = var/log && opt="--exclude=journal --exclude=*.log.*gz" + test $dir = etc && opt="--exclude=.git --exclude=sysinit.target.wants --exclude=multi-user.target.wants" + rsync -au $VERBOSE --delete-excluded --delete $opt ./ $trg + test "$VERBOSE" = -v && echo "= chown $dir" + find $trg -uid 0 -exec chown bupsupply "{}" \; >/dev/null 2>&1 + done + if [ $MK_HOME_TAR = yes ]; then + test "$VERBOSE" = -v && echo "= home" + pushd /home >/dev/null 2>&1 + tar czf $DIR_INFO/home.tgz $EXCLUDE_HOME --exclude-tag-all=$TAG_FILE . + popd >/dev/null 2>&1 + test "$VERBOSE" = -v && ls -ld $DIR_INFO/home.tgz + fi +} +function Zfs(){ + if [ $USE_ZFS = yes ]; then + test "$VERBOSE" = -v && echo "= ZFS" + local fn=$DIR_INFO/zfs.info.txt + echo "== zfs list" >$fn + zfs list >>$fn + echo "== zpool list" >>$fn + zpool list >>$fn + echo "== zpool status" >>$fn + zpool status >>$fn + echo "== zfs list -t snapshot" >>$fn + zfs list -t snapshot >>$fn + fi +} +function DoIt(){ + local debug= + $debug Packages + $debug Mount + $debug Devices + $debug SystemD + $debug Trees + $debug Zfs +} +DoIt diff --git a/templates.local/local_bin/SaveDatabases b/templates.local/local_bin/SaveDatabases new file mode 100644 index 0000000..7c559cd --- /dev/null +++ b/templates.local/local_bin/SaveDatabases @@ -0,0 +1,26 @@ +#! /bin/bash +BASE_DIR=/srv/www +BASE_WEBAPPS=/etc/ansknife/webapps.d +VERBOSE=-v +test "$1" = -q && VERBOSE= +PATH=/bin:/usr/bin +function SaveOne(){ + local domain=$1 + if [ -e $BASE_WEBAPPS/$domain.conf ]; then + . $BASE_WEBAPPS/$domain.conf + if [ -n "$db_name" ]; then + local sql=$BASE_DIR/$domain/db/$domain.sql.gz + mysqldump -u $db_user "-p$db_password" $db_name | gzip > $sql + test -n "$VERBOSE" && ls -ld $sql + fi + fi +} +function SaveAll(){ + cd $BASE_DIR + for domain in *; do + if [ -d $BASE_DIR/$domain/db ]; then + SaveOne $domain + fi + done +} +SaveAll \ No newline at end of file diff --git a/templates.local/local_bin/SaveOwner b/templates.local/local_bin/SaveOwner new file mode 100644 index 0000000..24eeb04 --- /dev/null +++ b/templates.local/local_bin/SaveOwner @@ -0,0 +1,33 @@ +#! /bin/bash +DIRS="/srv/www" +# DIRS="/home /srv/wwww" +VERBOSE=5 +PATH=/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin +test "$1" = -q && VERBOSE=0 + +function SaveOneDir(){ + local dir=$1 + test $VERBOSE != 0 && echo "= $dir" + pushd $dir >/dev/null 2>&1 + fileknife -l$VERBOSE owner . .owner.list + gzip -q -f .owner.list + test $VERBOSE != 0 && ls -l $(pwd)/.owner.list* + popd >/dev/null 2>&1 +} +function SaveSubdirs(){ + local base=$1 + cd $base + for dir in *; do + if [ -d $dir ]; then + SaveOneDir $base/$dir + fi + done +} + +function SaveAll(){ + for dir in $DIRS; do + SaveSubdirs $dir + done +} +#SaveAll + diff --git a/templates.local/local_bin/nest1Backup b/templates.local/local_bin/nest1Backup new file mode 100644 index 0000000..14b76ff --- /dev/null +++ b/templates.local/local_bin/nest1Backup @@ -0,0 +1,3 @@ +#! /bin/bash + +# currently we do nothing \ No newline at end of file