Add --all and --none options

This commit is contained in:
Jérémy Lecour 2022-03-27 10:39:07 +02:00 committed by Jérémy Lecour
parent fbff1ae797
commit 1179c2d9d3
2 changed files with 218 additions and 151 deletions

View file

@ -12,8 +12,12 @@ The **patch** part changes is incremented if multiple releases happen the same m
### Added ### Added
* `--all` and `--none` options to execute all of no task.
### Changed ### Changed
* rename internal variables to use "task" as concept of unit of work
### Fixed ### Fixed
### Removed ### Removed

View file

@ -38,11 +38,13 @@ Main options
-d, --dump-dir path to the directory where data will be stored -d, --dump-dir path to the directory where data will be stored
--backup-dir legacy option for dump directory --backup-dir legacy option for dump directory
-f, --force keep existing dump directory and its content -f, --force keep existing dump directory and its content
-v, --verbose print details about each step -v, --verbose print details about each task
-V, --version print version and exit -V, --version print version and exit
-h, --help print this message and exit -h, --help print this message and exit
Dump options Tasks options
--all reset options to execute all tasks
--none reset options to execute no task
--[no-]etc copy of /etc (default: no) --[no-]etc copy of /etc (default: no)
--[no-]dpkg-full copy of /var/lib/dpkg (default: no) --[no-]dpkg-full copy of /var/lib/dpkg (default: no)
--[no-]dpkg-status copy of /var/lib/dpkg/status (default: yes) --[no-]dpkg-status copy of /var/lib/dpkg/status (default: yes)
@ -64,6 +66,12 @@ Dump options
--[no-]dmesg copy of dmesg (default: yes) --[no-]dmesg copy of dmesg (default: yes)
--[no-]mysql copy of mysql processes (default: yes) --[no-]mysql copy of mysql processes (default: yes)
--[no-]systemctl copy of systemd services states (default: yes) --[no-]systemctl copy of systemd services states (default: yes)
Tasks options order matters. They are evaluated from left to right.
Examples :
* "[…] --none --uname" will do only the uname task
* "[…] --all --no-etc" will do everything but the etc task
* "[…] --etc --none --mysql" will do only the mysql task
END END
} }
debug() { debug() {
@ -73,7 +81,7 @@ debug() {
} }
create_dump_dir() { create_dump_dir() {
debug "## Create ${dump_dir}" debug "Task: Create ${dump_dir}"
last_result=$(mkdir -p "${dump_dir}" && chmod -R 755 "${dump_dir}") last_result=$(mkdir -p "${dump_dir}" && chmod -R 755 "${dump_dir}")
last_rc=$? last_rc=$?
@ -87,8 +95,8 @@ create_dump_dir() {
fi fi
} }
do_etc() { task_etc() {
debug "## /etc" debug "Task: /etc"
rsync_bin=$(command -v rsync) rsync_bin=$(command -v rsync)
@ -118,7 +126,7 @@ do_etc() {
fi fi
} }
do_apt_states() { task_apt_states() {
apt_dir="/" apt_dir="/"
apt_dir_state="var/lib/apt" apt_dir_state="var/lib/apt"
apt_dir_state_extended_states="extended_states" apt_dir_state_extended_states="extended_states"
@ -133,7 +141,7 @@ do_apt_states() {
extended_states="${apt_dir}/${apt_dir_state}/${apt_dir_state_extended_states}" extended_states="${apt_dir}/${apt_dir_state}/${apt_dir_state_extended_states}"
if [ -f "${extended_states}" ]; then if [ -f "${extended_states}" ]; then
debug "## APT states" debug "Task: APT states"
last_result=$(cp -r "${extended_states}" "${dump_dir}/apt-extended-states.txt") last_result=$(cp -r "${extended_states}" "${dump_dir}/apt-extended-states.txt")
last_rc=$? last_rc=$?
@ -148,8 +156,8 @@ do_apt_states() {
fi fi
} }
do_apt_config() { task_apt_config() {
debug "## APT config" debug "Task: APT config"
apt_config_bin=$(command -v apt-config) apt_config_bin=$(command -v apt-config)
@ -169,8 +177,8 @@ do_apt_config() {
fi fi
} }
do_dpkg_full() { task_dpkg_full() {
debug "## DPkg full state" debug "Task: DPkg full state"
dir_state_status="/var/lib/dpkg/status" dir_state_status="/var/lib/dpkg/status"
@ -222,8 +230,8 @@ do_dpkg_full() {
fi fi
} }
do_dpkg_status() { task_dpkg_status() {
debug "## DPkg status" debug "Task: DPkg status"
dir_state_status="/var/lib/dpkg/status" dir_state_status="/var/lib/dpkg/status"
@ -245,8 +253,8 @@ do_dpkg_status() {
fi fi
} }
do_packages() { task_packages() {
debug "## List of installed package" debug "Task: List of installed package"
dpkg_bin=$(command -v dpkg) dpkg_bin=$(command -v dpkg)
@ -266,8 +274,8 @@ do_packages() {
fi fi
} }
do_uname() { task_uname() {
debug "## uname" debug "Task: uname"
last_result=$(uname -a > "${dump_dir}/uname.txt") last_result=$(uname -a > "${dump_dir}/uname.txt")
last_rc=$? last_rc=$?
@ -281,8 +289,8 @@ do_uname() {
fi fi
} }
do_uptime() { task_uptime() {
debug "## uptime" debug "Task: uptime"
last_result=$(uptime > "${dump_dir}/uptime.txt") last_result=$(uptime > "${dump_dir}/uptime.txt")
last_rc=$? last_rc=$?
@ -296,8 +304,8 @@ do_uptime() {
fi fi
} }
do_processes() { task_processes() {
debug "## Process list" debug "Task: Process list"
last_result=$(ps fauxw > "${dump_dir}/ps.txt") last_result=$(ps fauxw > "${dump_dir}/ps.txt")
last_rc=$? last_rc=$?
@ -326,8 +334,8 @@ do_processes() {
fi fi
} }
do_netstat() { task_netstat() {
debug "## Network status" debug "Task: Network status"
ss_bin=$(command -v ss) ss_bin=$(command -v ss)
@ -364,8 +372,8 @@ do_netstat() {
fi fi
} }
do_netcfg() { task_netcfg() {
debug "## Network configuration" debug "Task: Network configuration"
ip_bin=$(command -v ip) ip_bin=$(command -v ip)
@ -413,8 +421,8 @@ do_netcfg() {
fi fi
} }
do_iptables() { task_iptables() {
debug "## iptables" debug "Task: iptables"
iptables_bin=$(command -v iptables) iptables_bin=$(command -v iptables)
nft_bin=$(command -v nft) nft_bin=$(command -v nft)
@ -467,8 +475,8 @@ do_iptables() {
fi fi
} }
do_sysctl() { task_sysctl() {
debug "## sysctl values" debug "Task: sysctl values"
sysctl_bin=$(command -v sysctl) sysctl_bin=$(command -v sysctl)
@ -488,8 +496,8 @@ do_sysctl() {
fi fi
} }
do_virsh() { task_virsh() {
debug "## virsh list" debug "Task: virsh list"
virsh_bin=$(command -v virsh) virsh_bin=$(command -v virsh)
@ -509,8 +517,8 @@ do_virsh() {
fi fi
} }
do_lxc() { task_lxc() {
debug "## lxc list" debug "Task: lxc list"
lxc_ls_bin=$(command -v lxc-ls) lxc_ls_bin=$(command -v lxc-ls)
@ -530,8 +538,8 @@ do_lxc() {
fi fi
} }
do_disks() { task_disks() {
debug "## Disks" debug "Task: Disks"
lsblk_bin=$(command -v lsblk) lsblk_bin=$(command -v lsblk)
awk_bin=$(command -v awk) awk_bin=$(command -v awk)
@ -581,8 +589,8 @@ do_disks() {
fi fi
} }
do_mount() { task_mount() {
debug "## Mount points" debug "Task: Mount points"
findmnt_bin=$(command -v findmnt) findmnt_bin=$(command -v findmnt)
@ -619,8 +627,8 @@ do_mount() {
fi fi
} }
do_df() { task_df() {
debug "## df" debug "Task: df"
df_bin=$(command -v df) df_bin=$(command -v df)
@ -640,8 +648,8 @@ do_df() {
fi fi
} }
do_dmesg() { task_dmesg() {
debug "## dmesg" debug "Task: dmesg"
dmesg_bin=$(command -v dmesg) dmesg_bin=$(command -v dmesg)
@ -661,8 +669,8 @@ do_dmesg() {
fi fi
} }
do_mysql_processes() { task_mysql_processes() {
debug "## MySQL processes" debug "Task: MySQL processes"
mysqladmin_bin=$(command -v mysqladmin) mysqladmin_bin=$(command -v mysqladmin)
@ -687,8 +695,8 @@ do_mysql_processes() {
fi fi
} }
do_systemctl() { task_systemctl() {
debug "## Systemd services" debug "Task: Systemd services"
systemctl_bin=$(command -v systemctl) systemctl_bin=$(command -v systemctl)
@ -708,7 +716,6 @@ do_systemctl() {
fi fi
} }
main() { main() {
if [ -z "${dump_dir}" ]; then if [ -z "${dump_dir}" ]; then
echo "ERROR: You must provide the --dump-dir argument" >&2 echo "ERROR: You must provide the --dump-dir argument" >&2
@ -724,68 +731,68 @@ main() {
create_dump_dir create_dump_dir
fi fi
if [ "${DO_ETC}" -eq 1 ]; then if [ "${TASK_ETC}" -eq 1 ]; then
do_etc task_etc
fi fi
if [ "${DO_DPKG_FULL}" -eq 1 ]; then if [ "${TASK_DPKG_FULL}" -eq 1 ]; then
do_dpkg_full task_dpkg_full
fi fi
if [ "${DO_DPKG_STATUS}" -eq 1 ]; then if [ "${TASK_DPKG_STATUS}" -eq 1 ]; then
do_dpkg_status task_dpkg_status
fi fi
if [ "${DO_APT_STATES}" -eq 1 ]; then if [ "${TASK_APT_STATES}" -eq 1 ]; then
do_apt_states task_apt_states
fi fi
if [ "${DO_APT_CONFIG}" -eq 1 ]; then if [ "${TASK_APT_CONFIG}" -eq 1 ]; then
do_apt_config task_apt_config
fi fi
if [ "${DO_PACKAGES}" -eq 1 ]; then if [ "${TASK_PACKAGES}" -eq 1 ]; then
do_packages task_packages
fi fi
if [ "${DO_PROCESSES}" -eq 1 ]; then if [ "${TASK_PROCESSES}" -eq 1 ]; then
do_processes task_processes
fi fi
if [ "${DO_UPTIME}" -eq 1 ]; then if [ "${TASK_UPTIME}" -eq 1 ]; then
do_uptime task_uptime
fi fi
if [ "${DO_UNAME}" -eq 1 ]; then if [ "${TASK_UNAME}" -eq 1 ]; then
do_uname task_uname
fi fi
if [ "${DO_NETSTAT}" -eq 1 ]; then if [ "${TASK_NETSTAT}" -eq 1 ]; then
do_netstat task_netstat
fi fi
if [ "${DO_NETCFG}" -eq 1 ]; then if [ "${TASK_NETCFG}" -eq 1 ]; then
do_netcfg task_netcfg
fi fi
if [ "${DO_IPTABLES}" -eq 1 ]; then if [ "${TASK_IPTABLES}" -eq 1 ]; then
do_iptables task_iptables
fi fi
if [ "${DO_SYSCTL}" -eq 1 ]; then if [ "${TASK_SYSCTL}" -eq 1 ]; then
do_sysctl task_sysctl
fi fi
if [ "${DO_VIRSH}" -eq 1 ]; then if [ "${TASK_VIRSH}" -eq 1 ]; then
do_virsh task_virsh
fi fi
if [ "${DO_LXC}" -eq 1 ]; then if [ "${TASK_LXC}" -eq 1 ]; then
do_lxc task_lxc
fi fi
if [ "${DO_DISKS}" -eq 1 ]; then if [ "${TASK_DISKS}" -eq 1 ]; then
do_disks task_disks
fi fi
if [ "${DO_MOUNT}" -eq 1 ]; then if [ "${TASK_MOUNT}" -eq 1 ]; then
do_mount task_mount
fi fi
if [ "${DO_DF}" -eq 1 ]; then if [ "${TASK_DF}" -eq 1 ]; then
do_df task_df
fi fi
if [ "${DO_DMESG}" -eq 1 ]; then if [ "${TASK_DMESG}" -eq 1 ]; then
do_dmesg task_dmesg
fi fi
if [ "${DO_MYSQL_PROCESSES}" -eq 1 ]; then if [ "${TASK_MYSQL_PROCESSES}" -eq 1 ]; then
do_mysql_processes task_mysql_processes
fi fi
if [ "${DO_SYSTEMCTL}" -eq 1 ]; then if [ "${TASK_SYSTEMCTL}" -eq 1 ]; then
do_systemctl task_systemctl
fi fi
@ -870,151 +877,207 @@ while :; do
fi fi
;; ;;
--all)
for option in \
TASK_ETC \
TASK_DPKG_FULL \
TASK_DPKG_STATUS \
TASK_APT_STATES \
TASK_APT_CONFIG \
TASK_PACKAGES \
TASK_PROCESSES \
TASK_UNAME \
TASK_UPTIME \
TASK_NETSTAT \
TASK_NETCFG \
TASK_IPTABLES \
TASK_SYSCTL \
TASK_VIRSH \
TASK_LXC \
TASK_DISKS \
TASK_MOUNT \
TASK_DF \
TASK_DMESG \
TASK_MYSQL_PROCESSES \
TASK_SYSTEMCTL
do
eval "${option}=1"
done
;;
--none)
for option in \
TASK_ETC \
TASK_DPKG_FULL \
TASK_DPKG_STATUS \
TASK_APT_STATES \
TASK_APT_CONFIG \
TASK_PACKAGES \
TASK_PROCESSES \
TASK_UNAME \
TASK_UPTIME \
TASK_NETSTAT \
TASK_NETCFG \
TASK_IPTABLES \
TASK_SYSCTL \
TASK_VIRSH \
TASK_LXC \
TASK_DISKS \
TASK_MOUNT \
TASK_DF \
TASK_DMESG \
TASK_MYSQL_PROCESSES \
TASK_SYSTEMCTL
do
eval "${option}=0"
done
;;
--etc) --etc)
DO_ETC=1 TASK_ETC=1
;; ;;
--no-etc) --no-etc)
DO_ETC=0 TASK_ETC=0
;; ;;
--dpkg-full) --dpkg-full)
DO_DPKG_FULL=1 TASK_DPKG_FULL=1
;; ;;
--no-dpkg-full) --no-dpkg-full)
DO_DPKG_FULL=0 TASK_DPKG_FULL=0
;; ;;
--dpkg-status) --dpkg-status)
DO_DPKG_STATUS=1 TASK_DPKG_STATUS=1
;; ;;
--no-dpkg-status) --no-dpkg-status)
DO_DPKG_STATUS=0 TASK_DPKG_STATUS=0
;; ;;
--apt-states) --apt-states)
DO_APT_STATES=1 TASK_APT_STATES=1
;; ;;
--no-apt-states) --no-apt-states)
DO_APT_STATES=0 TASK_APT_STATES=0
;; ;;
--apt-config) --apt-config)
DO_APT_CONFIG=1 TASK_APT_CONFIG=1
;; ;;
--no-apt-config) --no-apt-config)
DO_APT_CONFIG=0 TASK_APT_CONFIG=0
;; ;;
--packages) --packages)
DO_PACKAGES=1 TASK_PACKAGES=1
;; ;;
--no-packages) --no-packages)
DO_PACKAGES=0 TASK_PACKAGES=0
;; ;;
--processes) --processes)
DO_PROCESSES=1 TASK_PROCESSES=1
;; ;;
--no-processes) --no-processes)
DO_PROCESSES=0 TASK_PROCESSES=0
;; ;;
--uptime) --uptime)
DO_UPTIME=1 TASK_UPTIME=1
;; ;;
--no-uptime) --no-uptime)
DO_UPTIME=0 TASK_UPTIME=0
;; ;;
--uname) --uname)
DO_UNAME=1 TASK_UNAME=1
;; ;;
--no-uname) --no-uname)
DO_UNAME=0 TASK_UNAME=0
;; ;;
--netstat) --netstat)
DO_NETSTAT=1 TASK_NETSTAT=1
;; ;;
--no-netstat) --no-netstat)
DO_NETSTAT=0 TASK_NETSTAT=0
;; ;;
--netcfg) --netcfg)
DO_NETCFG=1 TASK_NETCFG=1
;; ;;
--no-netcfg) --no-netcfg)
DO_NETCFG=0 TASK_NETCFG=0
;; ;;
--iptables) --iptables)
DO_IPTABLES=1 TASK_IPTABLES=1
;; ;;
--no-iptables) --no-iptables)
DO_IPTABLES=0 TASK_IPTABLES=0
;; ;;
--sysctl) --sysctl)
DO_SYSCTL=1 TASK_SYSCTL=1
;; ;;
--no-sysctl) --no-sysctl)
DO_SYSCTL=0 TASK_SYSCTL=0
;; ;;
--virsh) --virsh)
DO_VIRSH=1 TASK_VIRSH=1
;; ;;
--no-virsh) --no-virsh)
DO_VIRSH=0 TASK_VIRSH=0
;; ;;
--lxc) --lxc)
DO_LXC=1 TASK_LXC=1
;; ;;
--no-lxc) --no-lxc)
DO_LXC=0 TASK_LXC=0
;; ;;
--disks) --disks)
DO_DISKS=1 TASK_DISKS=1
;; ;;
--no-disks) --no-disks)
DO_DISKS=0 TASK_DISKS=0
;; ;;
--mount) --mount)
DO_MOUNT=1 TASK_MOUNT=1
;; ;;
--no-mount) --no-mount)
DO_MOUNT=0 TASK_MOUNT=0
;; ;;
--df) --df)
DO_DF=1 TASK_DF=1
;; ;;
--no-df) --no-df)
DO_DF=0 TASK_DF=0
;; ;;
--dmesg) --dmesg)
DO_DMESG=1 TASK_DMESG=1
;; ;;
--no-dmesg) --no-dmesg)
DO_DMESG=0 TASK_DMESG=0
;; ;;
--mysql-processes) --mysql-processes)
DO_MYSQL_PROCESSES=1 TASK_MYSQL_PROCESSES=1
;; ;;
--no-mysql-processes) --no-mysql-processes)
DO_MYSQL_PROCESSES=0 TASK_MYSQL_PROCESSES=0
;; ;;
--systemctl) --systemctl)
DO_SYSTEMCTL=1 TASK_SYSTEMCTL=1
;; ;;
--no-systemctl) --no-systemctl)
DO_SYSTEMCTL=0 TASK_SYSTEMCTL=0
;; ;;
--) --)
@ -1039,27 +1102,27 @@ done
# Default values # Default values
: "${VERBOSE:=0}" : "${VERBOSE:=0}"
: "${FORCE:=0}" : "${FORCE:=0}"
: "${DO_ETC:=0}" : "${TASK_ETC:=0}"
: "${DO_DPKG_FULL:=0}" : "${TASK_DPKG_FULL:=0}"
: "${DO_DPKG_STATUS:=1}" : "${TASK_DPKG_STATUS:=1}"
: "${DO_APT_STATES:=1}" : "${TASK_APT_STATES:=1}"
: "${DO_APT_CONFIG:=1}" : "${TASK_APT_CONFIG:=1}"
: "${DO_PACKAGES:=1}" : "${TASK_PACKAGES:=1}"
: "${DO_PROCESSES:=1}" : "${TASK_PROCESSES:=1}"
: "${DO_UNAME:=1}" : "${TASK_UNAME:=1}"
: "${DO_UPTIME:=1}" : "${TASK_UPTIME:=1}"
: "${DO_NETSTAT:=1}" : "${TASK_NETSTAT:=1}"
: "${DO_NETCFG:=1}" : "${TASK_NETCFG:=1}"
: "${DO_IPTABLES:=1}" : "${TASK_IPTABLES:=1}"
: "${DO_SYSCTL:=1}" : "${TASK_SYSCTL:=1}"
: "${DO_VIRSH:=1}" : "${TASK_VIRSH:=1}"
: "${DO_LXC:=1}" : "${TASK_LXC:=1}"
: "${DO_DISKS:=1}" : "${TASK_DISKS:=1}"
: "${DO_MOUNT:=1}" : "${TASK_MOUNT:=1}"
: "${DO_DF:=1}" : "${TASK_DF:=1}"
: "${DO_DMESG:=1}" : "${TASK_DMESG:=1}"
: "${DO_MYSQL_PROCESSES:=1}" : "${TASK_MYSQL_PROCESSES:=1}"
: "${DO_SYSTEMCTL:=1}" : "${TASK_SYSTEMCTL:=1}"
export LC_ALL=C export LC_ALL=C