Kaynağa Gözat

[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
Koinu 2 yıl önce
ebeveyn
işleme
b6942b91c9
2 değiştirilmiş dosya ile 87 ekleme ve 0 silme
  1. 12 0
      README.md
  2. 75 0
      install-release.sh

+ 12 - 0
README.md

@@ -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
 ```
 
+**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/[email protected]
+installed: /etc/systemd/system/[email protected]
+
+installed: /etc/logrotate.d/xray
+```
+
 **Install & Upgrade Xray-core and geodata with `User=root`, which will overwrite `User` in existing service files**
 
 ```

+ 75 - 0
install-release.sh

@@ -76,6 +76,9 @@ NO_GEODATA='0'
 # --without-logfiles
 NO_LOGFILES='0'
 
+# --logrotate
+LOGROTATE='0'
+
 # --no-update-service
 N_UP_SERVICE='0'
 
@@ -297,6 +300,11 @@ judgment_parameters() {
       '--no-update-service')
         N_UP_SERVICE='1'
         ;;
+      '--logrotate')
+        LOGROTATE='1'
+        LOGROTATE_TIME="$2"
+        shift
+        ;;
       *)
         echo "$0: unknown option -- -"
         exit 1
@@ -616,6 +624,49 @@ stop_xray() {
   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/[email protected]
+[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/[email protected]
+[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() {
   download_geodata() {
     if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}" "${1}"; then
@@ -672,6 +723,7 @@ remove_xray() {
     fi
     local delete_files=('/usr/local/bin/xray' '/etc/systemd/system/xray.service' '/etc/systemd/system/[email protected]' '/etc/systemd/system/xray.service.d' '/etc/systemd/system/[email protected]')
     [[ -d "$DAT_PATH" ]] && delete_files+=("$DAT_PATH")
+    [[ -f '/etc/logrotate.d/xray' ]] && delete_files+=('/etc/logrotate.d/xray')
     if [[ "$PURGE" -eq '1' ]]; then
       if [[ -z "$JSONS_PATH" ]]; then
         delete_files+=("$JSON_PATH")
@@ -679,8 +731,11 @@ remove_xray() {
         delete_files+=("$JSONS_PATH")
       fi
       [[ -d '/var/log/xray' ]] && delete_files+=('/var/log/xray')
+      [[ -f '/etc/systemd/system/[email protected]' ]] && delete_files+=('/etc/systemd/system/[email protected]')
+      [[ -f '/etc/systemd/system/[email protected]' ]] && delete_files+=('/etc/systemd/system/[email protected]')
     fi
     systemctl disable xray
+    systemctl disable [email protected]
     if ! ("rm" -r "${delete_files[@]}"); then
       echo 'error: Failed to remove Xray.'
       exit 1
@@ -732,6 +787,7 @@ show_help() {
   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-logfiles        Don't install /var/log/xray"
+  echo "    --logrotate               Install with logrotate"
   echo '  install-geodata:'
   echo '    -p, --proxy               Download through a proxy server'
   echo '  remove:'
@@ -761,6 +817,9 @@ main() {
   # Check if the user is effective
   check_install_user
 
+  # Check Logrotate after Check User
+  [[ "$LOGROTATE" -eq '1' ]] && install_with_logrotate
+
   # Two very important variables
   TMP_DIRECTORY="$(mktemp -d)"
   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/error.log'
   fi
+  if [[ "$LOGROTATE_FIN" -eq '1' ]]; then
+    echo 'installed: /etc/systemd/system/[email protected]'
+    echo 'installed: /etc/systemd/system/[email protected]'
+    if [[ "$LOGROTATE_DIR" -eq '1' ]]; then
+    echo 'installed: /etc/logrotate.d/'
+    fi
+    echo 'installed: /etc/logrotate.d/xray'
+    systemctl start [email protected]
+    systemctl enable [email protected]
+    sleep 1s
+    if systemctl -q is-active [email protected]; then
+      echo "info: Enable and start the [email protected] service"
+    else
+      echo "warning: Failed to enable and start the [email protected] service"
+    fi
+  fi
   if [[ "$SYSTEMD" -eq '1' ]]; then
     echo 'installed: /etc/systemd/system/xray.service'
     echo 'installed: /etc/systemd/system/[email protected]'