[Feature Addition] Introducing the --logrotate Option for Xray's Enhanced Log Rotation (#62)

* [Feat] Add Option To Install with Logrotate

* Update: Using built-in user detection

* Changes:
1.Default retention time
2.Check Logrotate after Check User

https://github.com/XTLS/Xray-core/discussions/2369#discussioncomment-6554217
This commit is contained in:
Koinu 2023-07-31 11:41:30 +08:00 committed by GitHub
parent 9af684cf00
commit b6942b91c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 0 deletions

View file

@ -48,6 +48,18 @@ Notice: Xray will NOT log to `/var/log/xray/*.log` by default. Configure `"log"`
# bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install --beta # bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install --beta
``` ```
**Install & Upgrade Xray-core and geodata with `logrotate`**
```
# bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install --logrotate
```
```
installed: /etc/systemd/system/logrotate@.service
installed: /etc/systemd/system/logrotate@.timer
installed: /etc/logrotate.d/xray
```
**Install & Upgrade Xray-core and geodata with `User=root`, which will overwrite `User` in existing service files** **Install & Upgrade Xray-core and geodata with `User=root`, which will overwrite `User` in existing service files**
``` ```

75
install-release.sh Normal file → Executable file
View file

@ -76,6 +76,9 @@ NO_GEODATA='0'
# --without-logfiles # --without-logfiles
NO_LOGFILES='0' NO_LOGFILES='0'
# --logrotate
LOGROTATE='0'
# --no-update-service # --no-update-service
N_UP_SERVICE='0' N_UP_SERVICE='0'
@ -297,6 +300,11 @@ judgment_parameters() {
'--no-update-service') '--no-update-service')
N_UP_SERVICE='1' N_UP_SERVICE='1'
;; ;;
'--logrotate')
LOGROTATE='1'
LOGROTATE_TIME="$2"
shift
;;
*) *)
echo "$0: unknown option -- -" echo "$0: unknown option -- -"
exit 1 exit 1
@ -616,6 +624,49 @@ stop_xray() {
echo 'info: Stop the Xray service.' echo 'info: Stop the Xray service.'
} }
install_with_logrotate() {
install_software 'logrotate' 'logrotate'
if [[ -z "$LOGROTATE_TIME" ]]; then
LOGROTATE_TIME="00:00:00"
fi
cat <<EOF > /etc/systemd/system/logrotate@.service
[Unit]
Description=Rotate log files
Documentation=man:logrotate(8)
[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /etc/logrotate.d/%i
EOF
cat <<EOF > /etc/systemd/system/logrotate@.timer
[Unit]
Description=Run logrotate for %i logs
[Timer]
OnCalendar=*-*-* $LOGROTATE_TIME
Persistent=true
[Install]
WantedBy=timers.target
EOF
if [[ ! -d '/etc/logrotate.d/' ]]; then
install -d -m 700 -o "$INSTALL_USER_UID" -g "$INSTALL_USER_GID" /etc/logrotate.d/
LOGROTATE_DIR='1'
fi
cat << EOF > /etc/logrotate.d/xray
/var/log/xray/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 0600 $INSTALL_USER_UID $INSTALL_USER_GID
}
EOF
LOGROTATE_FIN='1'
}
install_geodata() { install_geodata() {
download_geodata() { download_geodata() {
if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}" "${1}"; then if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}" "${1}"; then
@ -672,6 +723,7 @@ remove_xray() {
fi fi
local delete_files=('/usr/local/bin/xray' '/etc/systemd/system/xray.service' '/etc/systemd/system/xray@.service' '/etc/systemd/system/xray.service.d' '/etc/systemd/system/xray@.service.d') local delete_files=('/usr/local/bin/xray' '/etc/systemd/system/xray.service' '/etc/systemd/system/xray@.service' '/etc/systemd/system/xray.service.d' '/etc/systemd/system/xray@.service.d')
[[ -d "$DAT_PATH" ]] && delete_files+=("$DAT_PATH") [[ -d "$DAT_PATH" ]] && delete_files+=("$DAT_PATH")
[[ -f '/etc/logrotate.d/xray' ]] && delete_files+=('/etc/logrotate.d/xray')
if [[ "$PURGE" -eq '1' ]]; then if [[ "$PURGE" -eq '1' ]]; then
if [[ -z "$JSONS_PATH" ]]; then if [[ -z "$JSONS_PATH" ]]; then
delete_files+=("$JSON_PATH") delete_files+=("$JSON_PATH")
@ -679,8 +731,11 @@ remove_xray() {
delete_files+=("$JSONS_PATH") delete_files+=("$JSONS_PATH")
fi fi
[[ -d '/var/log/xray' ]] && delete_files+=('/var/log/xray') [[ -d '/var/log/xray' ]] && delete_files+=('/var/log/xray')
[[ -f '/etc/systemd/system/logrotate@.service' ]] && delete_files+=('/etc/systemd/system/logrotate@.service')
[[ -f '/etc/systemd/system/logrotate@.timer' ]] && delete_files+=('/etc/systemd/system/logrotate@.timer')
fi fi
systemctl disable xray systemctl disable xray
systemctl disable logrotate@xray.timer
if ! ("rm" -r "${delete_files[@]}"); then if ! ("rm" -r "${delete_files[@]}"); then
echo 'error: Failed to remove Xray.' echo 'error: Failed to remove Xray.'
exit 1 exit 1
@ -732,6 +787,7 @@ show_help() {
echo " --no-update-service Don't change service files if they are exist" echo " --no-update-service Don't change service files if they are exist"
echo " --without-geodata Don't install/update geoip.dat and geosite.dat" echo " --without-geodata Don't install/update geoip.dat and geosite.dat"
echo " --without-logfiles Don't install /var/log/xray" echo " --without-logfiles Don't install /var/log/xray"
echo " --logrotate Install with logrotate"
echo ' install-geodata:' echo ' install-geodata:'
echo ' -p, --proxy Download through a proxy server' echo ' -p, --proxy Download through a proxy server'
echo ' remove:' echo ' remove:'
@ -761,6 +817,9 @@ main() {
# Check if the user is effective # Check if the user is effective
check_install_user check_install_user
# Check Logrotate after Check User
[[ "$LOGROTATE" -eq '1' ]] && install_with_logrotate
# Two very important variables # Two very important variables
TMP_DIRECTORY="$(mktemp -d)" TMP_DIRECTORY="$(mktemp -d)"
ZIP_FILE="${TMP_DIRECTORY}/Xray-linux-$MACHINE.zip" ZIP_FILE="${TMP_DIRECTORY}/Xray-linux-$MACHINE.zip"
@ -848,6 +907,22 @@ main() {
echo 'installed: /var/log/xray/access.log' echo 'installed: /var/log/xray/access.log'
echo 'installed: /var/log/xray/error.log' echo 'installed: /var/log/xray/error.log'
fi fi
if [[ "$LOGROTATE_FIN" -eq '1' ]]; then
echo 'installed: /etc/systemd/system/logrotate@.service'
echo 'installed: /etc/systemd/system/logrotate@.timer'
if [[ "$LOGROTATE_DIR" -eq '1' ]]; then
echo 'installed: /etc/logrotate.d/'
fi
echo 'installed: /etc/logrotate.d/xray'
systemctl start logrotate@xray.timer
systemctl enable logrotate@xray.timer
sleep 1s
if systemctl -q is-active logrotate@xray.timer; then
echo "info: Enable and start the logrotate@xray.timer service"
else
echo "warning: Failed to enable and start the logrotate@xray.timer service"
fi
fi
if [[ "$SYSTEMD" -eq '1' ]]; then if [[ "$SYSTEMD" -eq '1' ]]; then
echo 'installed: /etc/systemd/system/xray.service' echo 'installed: /etc/systemd/system/xray.service'
echo 'installed: /etc/systemd/system/xray@.service' echo 'installed: /etc/systemd/system/xray@.service'