[群晖]此套件需要您启动[pgsql-adapter.service]
短信预约 -IT技能 免费直播动态提醒
1. 打开群晖SSH
默认使用22端口
2. 通过SSH工具进入群晖
我这里用的是Xshell
什么ssh工具都可以,不会的朋友请自行学习。
3. 切换root用户
xxx@xxx:/$ sudo -iPassword: root@xxx:~#
这里的密码通常是群晖第一个管理员用户密码
4.创建脚本
随意进入到一个目录下(我是在/usr/pgsql),创建“pgsql_analyzer.sh”文件,操作如下:
root@xxx:/# cd /usr/root@xxx:/usr# mkdir pgsqlroot@xxx:/usr# cd pgsql/root@xxx:/usr/pgsql# vim pgsql_analyzer.sh
此时已创建好了“pgsql_analyzer.sh”文件,并打开了它
接下来我们把以下的脚本粘贴到“pgsql_analyzer.sh”中。
pgsql_analyzer.sh脚本内容:
#!/bin/bash################### Utility functions##################syno_getopt(){local opt_cmd="$1"shift 1while getopts "$opt_cmd" opt; doif [[ -z "$OPTARG" ]]; theneval "opt_${opt}"="-"elseeval "opt_${opt}"="$OPTARG"fidone}cecho(){local color="$1"local message="$2"local NC='\033[0m'local codecase "$color" inred) code="1;31" ;;green) code="1;32" ;;blue) code="1;34" ;;gray) code="0;37" ;;yellow) code="1;33" ;;*) code="1;37" ;;esaccode='\033['$code'm'echo "${code}${message}${NC}"}errecho(){echo -e "$@" >&2}LOG(){local level="$1"local message="$2"local timestamp="$(date +'%Y-%m-%d %H:%M:%S')"# Echo to screenlocal colorcase "$level" in"ERROR"|"WARNING") color="red" ;;"NOTICE") color="blue" ;;"INFO") color="green" ;;*) color="gray" ;;esacformatted=$(cecho yellow "$timestamp ")$(cecho $color "[$level] $message")errecho $formatted}log_check_result(){local name="$1"local result="$2"if [[ "$result" = "true" || "$result" = "0" ]]; thenLOG INFO "Running $name ... Pass"return 0elseLOG WARNING "Running $name ... Error found, please check messages above."return 1fi}db_query(){local db="$1"local sql="$2"psql -At -F $'\t' -U postgres $db -c "$sql"}################### Test functions##################findfile(){local pattern="$1"local path="$2"local files="${path} ${path}.1 ${path}.1.xz ${path}.2.xz"local limit=20local result=falselocal extentsion file msgfor file in $files; doif [[ ! -f "$file" ]]; thencontinuefiLOG DEBUG "Searching error message from file: $file ..."extension="${file##*.}"case "$extension" ingz)msg="$(zcat "$file" | grep "$pattern" | tail -n $limit)";;xz)msg="$(xzcat "$file" | grep "$pattern" | tail -n $limit)";;*)msg="$(cat "$file" | grep "$pattern" | tail -n $limit)";;esacif [[ -n "$msg" ]]; thenresult=trueecho "$msg"breakfidone$result && return 0 || return 1}has_permission(){local path="$1"local permission="$2"local user="$3"local group="$4"local result=truelocal re f_permission f_user f_group# Check path existsif [[ ! -e "$path" ]]; thenLOG ERROR "File or directory does not exists: $path"return 1fi# Check path permissionif [[ -n "$permission" ]]; thenre="${permission//-/.}"f_permission="$(stat -c "%A" "$path")"if [[ ! "$f_permission" =~ $re ]]; thenLOG ERROR "Permission $permission is not match, path: $path, permission: $f_permission"result=falsefifi# Check path userif [[ -n "$user" ]]; thenf_user="$(stat -c "%U" "$path")"if [[ "$f_user" != "$user" ]]; thenLOG ERROR "User $user is not match, path: $path, user: $f_user"result=falsefifi# Check path groupif [[ -n "$group" ]]; thenf_group="$(stat -c "%G" "$path")"if [[ "$f_group" != "$group" ]]; thenLOG ERROR "Group $group is not match, path: $path, user: $f_group"result=falsefifi$result && return 0 || return 1}################### Test cases##################disklog_check(){local files="/var/log/dmesg /var/log/messages"local result=truelocal filefor file in $files; doif findfile "exception Emask\|status: {\|error: {\|SError: {" "$file"; thenLOG ERROR "Disk error(s) are found in $file"result=falsefidonefor file in $files; doif findfile "EXT4-fs .*: error\|EXT4-fs error\|EXT4-fs warning\|read error corrected" "$file"; thenLOG ERROR "Disk ext-4 error(s) are found in $file"result=falsefidonelog_check_result disklog_check $result}tune2fs_check(){local devs="/dev/md0 /dev/sda1 $(cut -d ' ' -f 1 /run/synostorage/volumetab)"local result=truelocal devfor dev in $devs; doif tune2fs -l "$dev" 2> /dev/null | grep "FS Error"; thenLOG ERROR "Filesystem error(s) are found in tune2fs -l $dev"result=falsefidonelog_check_result tune2fs_check $result}permission_check(){# Check directory permissionlocal volume="$(servicetool --get-service-volume pgsql)"local result=truehas_permission "$volume" drwxr-xr-x || result=falsehas_permission "${volume}/@database" drwxr-xr-x admin users || result=falsehas_permission "${volume}/@database/pgsql" drwx------ postgres postgres || result=falselog_check_result permission_check $result}user_check(){# Check root user name and postgres entry pointlocal countlocal result=true# Check root namecount=$(cat /etc/passwd | grep -c ':x:0:0:')if [[ "$count" -ne 1 ]]; thenLOG ERROR "User: root has $count rows in /etc/passwd"result=falsefiif ! cat /etc/passwd | grep 'root:x:0:0:' > /dev/null 2>&1; thenLOG ERROR "User: root is not found in /etc/passwd"result=falsefi# Check postgres entry pointcount=$(cat /etc/passwd | grep -c 'postgres')if [[ "$count" -ne 1 ]]; thenLOG ERROR "User: postgres has $count rows in /etc/passwd"result=falsefiif ! cat /etc/passwd | grep postgres | grep '/var/services/pgsql:/bin/sh' > /dev/nuill 2>&1; thenLOG ERROR "User: postgres entry point(/var/services/pgsql:/bin/sh) has been modified"result=falsefilog_check_result user_check $result}pglog_check(){local result=trueif findfile "FATAL" "/var/log/postgresql.log"; thenLOG ERROR "Fatal error(s) are found in postgresql log"result=falsefilog_check_result pglog_check $result}volume_check(){local volume="$(servicetool --get-service-volume pgsql)"local result=truelocal availif ! df -BG | grep -q "$volume"; thenLOG WARNING "Failed to parse volume size, Please check available space of volume manually"log_check_result volume_check falsereturnfi# Get available volume space in Gigabytesavail=$(df -BG | grep "$volume" | awk '{print $4}' | sed 's/G//g')if ((avail <= 1)); thenLOG ERROR "Available volume space is smaller than 1GB"result=falsefilog_check_result volume_check $result}calendar_check(){local result=truelocal answer=nif tail -n 20 /var/log/postgresql.log | grep '/tmp/synocalendar'; thenresult=falseread -p "Hit Calendar bug, missing directory: /tmp/synocalendar. Do you want to fix it? (Y/n):" answerecho ""if [[ -z "$answer" || "$answer" = "Y" || "$answer" = "y" ]]; thenLOG INFO "Directory created: /tmp/synocalendar. Restart pgsql manullay later ('synoservice --restart pgsql' on DSM6, '/usr/syno/bin/synosystemctl restart pgsql-adapter' on DSM7)."mkdir /tmp/synocalendarchown postgres:postgres /tmp/synocalendarfifilog_check_result calendar_check $result}##### Main and help functionrebuild_database(){local volume="$(servicetool --get-service-volume pgsql)"local path="${volume}/@database/pgsql"local dsm_version=`synogetkeyvalue /etc.defaults/VERSION major`LOG INFO "Rebuilding database ..."if [[ "$dsm_version" = "7" ]]; thenif ! /usr/syno/bin/synosystemctl stop-service-by-reason rebuild pgsql-adapter; thenLOG ERROR "Failed to stop pgsql"return 1fielseif ! synoservice --pause-by-reason pgsql rebuild; thenLOG ERROR "Failed to stop pgsql"return 1fifiBACKUP_PATH=${path}-$(date -u +"%Y%m%dT%H%M%SZ")mv "$path" "$BACKUP_PATH"LOG INFO "Backup Success: $BACKUP_PATH"rm /var/services/pgsqlif ! servicetool --set-pgsql; thenLOG ERROR "Failed to create psgql directory"return 1fiif [[ "$dsm_version" = "7" ]]; thenif ! /usr/syno/bin/synosystemctl start-service-by-reason rebuild pgsql-adapter; thenLOG ERROR "Failed to start pgsql"return 1fielseif ! synoservice --resume-by-reason pgsql rebuild; thenLOG ERROR "Failed to start pgsql"return 1fifiLOG INFO "Rebuild done."}vacuum_all(){local result=true##### List all databasesdbs="$(db_query postgres 'SELECT datname FROM pg_database WHERE datistemplate = false')"echo "Database list: $dbs"##### Vaccumfor db in $dbs; do# Skip large size databaseif [[ "$db" = "synoips" ]]; thenLOG DEBUG "Skip to vacuum $db"continuefiLOG DEBUG "Vacuuming $db"if ! db_query $db "VACUUM FULL"; thenLOG WARNING "Failed to vacuum $db"result=falsefidonelog_check_result vacuum_all $result}recreate_database(){local package="$1"local db=$(cat /var/packages/${package}/conf/resource | jq -r '.["pgsql-db"]["create-database"][0]["db-name"]')local db_tmp="${db}_$(date +'%Y%m%d%H%M%S')"if ! db_query postgres "ALTER DATABASE $db RENAME TO $db_tmp"; thenLOG ERROR "Failed to rename database from $db to $db_tmp"return 1fiif ! synopkghelper update $package pgsql-db; thenLOG ERROR "Failed to create database for package: $package"return 1fi}show_usage(){cat << EOFDescription:Analysis pgsql issuesUsage:$0 [options]Options:-h Show this help-r Rebuild pgsql database-d Recreate database-v Vacuum all databases (exluding synoips)EOF }main(){syno_getopt "hvrd:" "$@"shift $((OPTIND-1))if [[ -n "$opt_h" ]]; thenshow_usagereturnfi##### Rebuild databaseif [[ -n "$opt_r" ]]; thenrebuild_databasereturn $?fi##### Vacuum every databasesif [[ -n "$opt_v" ]]; thenvacuum_allreturn $?fi##### Recreate database for packageif [[ -n "$opt_d" ]]; thenrecreate_database "$opt_d"return $?fi##### Checkpglog_checkdisklog_checktune2fs_checkpermission_checkuser_checkvolume_checkcalendar_check}main "$@"
在shell窗口中,按 i 键进入编辑模式*
将上面的脚本内容粘贴到窗口中
依次按’ESC、‘:’ 键、‘w’ 键、 'q’键、回车
5.执行pgsql_analyzer.sh
root@xxx:/usr/pgsql# chmod a+x pgsql_analyzer.shroot@xxx:/usr/pgsql# ./pgsql_analyzer.sh -rroot@xxx:/usr/pgsql# reboot
此时系统会重新启动
启动完成,pgsql就启动好了
小白第一次写文章,希望能帮助到大家,写的不好请大家见谅。
有意见或建议烦请指出,感谢!
最后有个小问题,命令应该用什么样的代码块?我这里用的shell形式,感觉展示效果不好。有哪位大佬知道请帮忙解答一下,非常感谢!
来源地址:https://blog.csdn.net/jihong6787/article/details/127120125
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341