one-click-installation-script/repair_scripts/resize_journal.sh

137 lines
4.9 KiB
Bash
Raw Normal View History

2022-12-17 23:05:21 +08:00
#!/bin/bash
2022-12-18 11:56:08 +08:00
#by spiritlhl
#from https://github.com/spiritLHLS/one-click-installation-script
2023-11-24 10:55:36 +08:00
#version: 2023.11.24
2022-12-18 11:56:08 +08:00
2023-04-27 20:51:11 +08:00
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8")
if [[ -z "$utf8_locale" ]]; then
echo "No UTF-8 locale found"
else
export LC_ALL="$utf8_locale"
export LANG="$utf8_locale"
export LANGUAGE="$utf8_locale"
echo "Locale set to $utf8_locale"
fi
2023-09-26 13:52:13 +08:00
red() { echo -e "\033[31m\033[01m$1$2\033[0m"; }
green() { echo -e "\033[32m\033[01m$1$2\033[0m"; }
yellow() { echo -e "\033[33m\033[01m$1$2\033[0m"; }
reading() { read -rp "$(green "$1")" "$2"; }
2022-12-17 23:05:21 +08:00
2022-12-17 23:10:25 +08:00
head() {
# 支持系统Ubuntu 12+Debian 6+
2023-05-22 14:13:30 +08:00
ver="2023.05.22"
2022-12-17 23:10:25 +08:00
changeLog="一键修改journal日志记录大小释放系统盘空间"
clear
echo "#######################################################################"
2022-12-17 23:13:12 +08:00
echo "# ${YELLOW}一键修改journal大小脚本${PLAIN} #"
2022-12-17 23:10:25 +08:00
echo "# 版本:$ver #"
2022-12-17 23:13:12 +08:00
echo "# 更新日志:$changeLog #"
2022-12-17 23:10:25 +08:00
echo "# ${GREEN}作者${PLAIN}: spiritlhl #"
2023-04-01 18:33:11 +08:00
echo "# ${GREEN}仓库${PLAIN}: https://github.com/spiritLHLS/one-click-installation-script #"
2022-12-17 23:10:25 +08:00
echo "#######################################################################"
2022-12-18 11:43:51 +08:00
echo "支持系统Ubuntu 18+Debian 8+centos 7+FedoraAlmalinux 8.5+"
2022-12-18 11:39:23 +08:00
echo "1.自定义修改大小单位为MB一般500或者1000即可有的系统日志默认给了5000甚至更多不是做站啥的没必要"
2022-12-18 12:04:26 +08:00
echo "请注意修改journal目录大小会影响系统日志的记录因此在修改journal目录大小之前如果需要之前的日志建议先备份系统日志到本地"
2022-12-18 12:02:49 +08:00
echo "2.自定义修改设置系统日志保留日期时长,超过日期时长的日志将被清除"
echo "3.默认修改日志只记录warning等级(无法自定义)"
2022-12-18 11:39:23 +08:00
echo "4.以后日志的产生将受到日志文件大小,日志保留时间,日志保留等级的限制"
2022-12-17 23:10:25 +08:00
# Display prompt asking whether to proceed with changing
2022-12-18 11:33:08 +08:00
reading "Do you want to proceed with changing? [y/n] " confirm
2022-12-17 23:10:25 +08:00
echo ""
2022-12-17 23:05:21 +08:00
2022-12-17 23:10:25 +08:00
# Check user's input and exit if they do not want to proceed
if [ "$confirm" != "y" ]; then
exit 0
fi
2022-12-18 11:43:51 +08:00
reading "Enter the desired day of the journal retention days (eg: 7): " retention_days
2022-12-18 11:29:55 +08:00
reading "Enter the desired size of the journal directory in MB (eg: 500): " size
2022-12-17 23:10:25 +08:00
}
2022-12-17 23:05:21 +08:00
2022-12-17 23:10:25 +08:00
main() {
2022-12-18 11:04:03 +08:00
# Set default value for size
size="$size"M
2022-12-18 17:29:48 +08:00
temp_file=$(mktemp)
backup_file="/etc/systemd/journald.conf-$(date +%Y%m%d%H%M%S).bak"
sudo cp /etc/systemd/journald.conf "$backup_file"
yellow "Backed up /etc/systemd/journald.conf to $backup_file"
2023-09-26 13:52:13 +08:00
awk -v size="$size" '{ if ($1 == "SystemMaxUse=") { print "SystemMaxUse=" size } else { print $0 } }' /etc/systemd/journald.conf >"$temp_file"
2022-12-18 17:29:48 +08:00
sudo cp "$temp_file" /etc/systemd/journald.conf
2023-05-22 14:22:23 +08:00
rm -rf "$temp_file"
2023-05-22 14:13:30 +08:00
conf_file="/etc/systemd/journald.conf"
variable1="SystemMaxFileSize"
variable2="ForwardToSyslog"
new_value1="128M"
new_value2="no"
if [[ ! -f "$conf_file" ]]; then
2023-09-26 13:52:13 +08:00
exit 1
2023-05-22 14:13:30 +08:00
fi
if grep -q "^$variable1=" "$conf_file"; then
2023-09-26 13:52:13 +08:00
sed -i "s/^$variable1=.*/$variable1=$new_value1/" "$conf_file"
2023-05-22 14:13:30 +08:00
else
2023-09-26 13:52:13 +08:00
echo "$variable1=$new_value1" >>"$conf_file"
2023-05-22 14:13:30 +08:00
fi
if grep -q "^$variable2=" "$conf_file"; then
2023-09-26 13:52:13 +08:00
sed -i "s/^$variable2=.*/$variable2=$new_value2/" "$conf_file"
2023-05-22 14:13:30 +08:00
else
2023-09-26 13:52:13 +08:00
echo "$variable2=$new_value2" >>"$conf_file"
2023-05-22 14:13:30 +08:00
fi
2022-12-18 11:04:03 +08:00
systemctl restart systemd-journald
2022-12-18 17:29:48 +08:00
2022-12-18 13:01:21 +08:00
green "change /etc/systemd/journald.conf successfully"
2022-12-17 23:10:25 +08:00
}
2022-12-18 11:04:03 +08:00
level() {
# Set default values for variables
log_level=warning
2022-12-18 11:29:55 +08:00
journald_log_dir="/var/log/journal"
2022-12-18 11:09:27 +08:00
# Check if log directory exists
2022-12-18 11:29:55 +08:00
if [ ! -d "$journald_log_dir" ]; then
2022-12-18 13:01:21 +08:00
yellow "Log directory not found, so no delete" >&2
2022-12-18 11:47:02 +08:00
else
# Set log retention period
2023-11-24 10:55:36 +08:00
# find "$journald_log_dir" -mtime +$retention_days -exec rm {} \;
rm -rf "$journald_log_dir"/*
2022-12-18 13:01:21 +08:00
green "change $journald_log_dir successfully"
2022-12-18 11:09:27 +08:00
fi
2023-09-26 13:52:13 +08:00
2022-12-18 11:09:27 +08:00
# Check if config file exists
if [ ! -f /etc/rsyslog.conf ]; then
2022-12-18 11:43:51 +08:00
red "Config file (/etc/rsyslog.conf) not found, so not modify" >&2
2022-12-18 11:47:02 +08:00
else
# Set log level
2022-12-18 17:29:48 +08:00
temp_file=$(mktemp)
backup_file="/etc/rsyslog.conf-$(date +%Y%m%d%H%M%S).bak"
sudo cp /etc/rsyslog.conf "$backup_file"
yellow "Backed up /etc/rsyslog.conf to $backup_file"
if grep -q "loglevel" /etc/rsyslog.conf; then
2023-09-26 13:52:13 +08:00
awk -v log_level="$log_level" '{ if ($1 == "loglevel") { print "loglevel = " log_level } else { print $0 } }' /etc/rsyslog.conf >"$temp_file"
2022-12-18 17:29:48 +08:00
else
2023-09-26 13:52:13 +08:00
cat /etc/rsyslog.conf >"$temp_file"
echo "loglevel = $log_level" >>"$temp_file"
2022-12-18 11:47:02 +08:00
fi
2022-12-18 17:29:48 +08:00
sudo cp "$temp_file" /etc/rsyslog.conf
rm "$temp_file"
2022-12-18 13:01:21 +08:00
green "change /etc/rsyslog.conf successfully"
2022-12-18 11:29:55 +08:00
fi
2022-12-18 17:29:48 +08:00
2022-12-18 11:29:55 +08:00
}
2022-12-18 11:09:27 +08:00
2022-12-18 11:29:55 +08:00
check_again() {
# Loop for 5 seconds, printing journald disk usage every second
count=0
while [ $count -lt 5 ]; do
journalctl --disk-usage
2023-09-26 13:52:13 +08:00
count=$((count + 1))
2022-12-18 11:29:55 +08:00
sleep 1
done
2022-12-18 11:04:03 +08:00
}
2022-12-17 23:10:25 +08:00
head
main
2022-12-18 11:04:03 +08:00
level
2022-12-18 11:29:55 +08:00
check_again