尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Shell使用软链接挂载目录的最佳实践

Shell使用软链接挂载目录的最佳实践 目录一、目标二、一般性的简单实现一编写思路二具体脚本代码三进一步优化优化建议具体实现三、复杂应用实现案例一背景说明二具体编写思路三初版脚本代码配置文件config.txt脚本代码manage_softlink.sh四思考进一步优化优化建议优化的脚本部分YAML 配置文件config.yaml优化后的脚本代码四、总结干货分享感谢您的阅读在现代运维管理中目录的软链接挂载是一项常见且重要的操作。通过软链接不仅可以简化目录结构管理还能在多个应用或服务之间实现数据共享与灵活配置。然而手动操作软链接可能会引发权限管理、日志追踪、备份恢复等问题因此自动化脚本的使用变得尤为重要。本文将介绍如何通过 Bash 脚本实现软链接的挂载、管理及其在复杂应用中的实际案例帮助运维人员提高工作效率和系统稳定性。一、目标在日常运维工作中我们常常需要将某一目录通过软链接挂载到目标目录。这种做法不仅有助于提高系统的管理效率还能方便地进行数据分析和维护。本文将详细介绍如何编写脚本来实现这一目标。二、一般性的简单实现一编写思路设置打印日志在脚本中加入日志打印功能方便定位和排查问题。采用函数方式使日志功能可以在脚本的多个部分被调用。实现软链接创建函数将软链接创建的逻辑封装到一个函数中便于多次使用。同时考虑到目录的权限控制和备份需求确保操作的安全性和可靠性。业务逻辑实现结合具体业务需求调用上述函数实现目录的软链接挂载和管理。二具体脚本代码#!/bin/bash #Program: #History: 2018.12.20 zyf #print timestamp getTimeStamp() { echo date %Y-%m-%d %H:%M:%S } #print log for analyzing log() { echo $1 echo getTimeStamp : $1 ${LOG_TEMPFILE2} } common_softlink() { targetdir$1 sourcedir$2 if [ ! -d ${targetdir} ] then mkdir -p ${targetdir} log mkdir -p ${targetdir} fi if [ -L ${sourcedir} ] then log rm ${sourcedir} rm ${sourcedir} fi if [ -d ${sourcedir} ] then log mv -f ${sourcedir} ${sourcedir}_${NOWTIME} mv -f ${sourcedir} ${sourcedir}_${NOWTIME} fi ln -s ${targetdir} ${sourcedir} chmod 755 ${sourcedir} chown zyf:root ${sourcedir} chmod -R 755 ${targetdir} chown -R zyf:root ${targetdir} } init_softlink() { UNAMEuname -a MICRONAME1echo $UNAME | cut -d -f 2 MICRONAME2echo $MICRONAME1 | cut -d - -f 1-3 log_dir/home/log/$MICRONAME2/$MICRONAME1 common_softlink ${log_dir} /home/zyf/microsvr/zyfcloud-order-service/logs/ } ###############main############## log init_softlink start... init_softlink log init_softlink end... log [~INIT_SOFTLINK OPERATE SUCCESS~]三进一步优化优化建议日志管理改进日志文件路径和名称管理避免覆盖旧日志。错误处理添加更多错误处理机制确保每个步骤都能成功执行或能捕获错误并记录。变量规范使用统一的变量命名规范增强可读性。权限检查在执行权限变更前检查是否有必要。参数化脚本使脚本能够接受命令行参数以便更加灵活地调用。具体实现#!/bin/bash # Program: Manage soft links for directories # History: 2018.12.20 zyf LOG_FILE/var/log/init_softlink.log NOWTIME$(date %Y%m%d%H%M%S) # Print timestamp getTimeStamp() { echo $(date %Y-%m-%d %H:%M:%S) } # Print log for analyzing log() { echo $1 echo $(getTimeStamp) : $1 ${LOG_FILE} } # Create a soft link for directories common_softlink() { local targetdir$1 local sourcedir$2 if [ ! -d ${targetdir} ]; then mkdir -p ${targetdir} log Created directory ${targetdir} fi if [ -L ${sourcedir} ]; then log Removing existing symlink ${sourcedir} rm ${sourcedir} elif [ -d ${sourcedir} ]; then log Backing up existing directory ${sourcedir} mv -f ${sourcedir} ${sourcedir}_${NOWTIME} fi ln -s ${targetdir} ${sourcedir} log Created symlink from ${targetdir} to ${sourcedir} # Update permissions if necessary if [ ! -w ${sourcedir} ]; then chmod 755 ${sourcedir} chown zyf:root ${sourcedir} chmod -R 755 ${targetdir} chown -R zyf:root ${targetdir} log Updated permissions for ${sourcedir} and ${targetdir} fi } # Initialize the soft link setup init_softlink() { local UNAME$(uname -a) local MICRONAME1$(echo $UNAME | cut -d -f 2) local MICRONAME2$(echo $MICRONAME1 | cut -d - -f 1-3) local log_dir/home/log/${MICRONAME2}/${MICRONAME1} common_softlink ${log_dir} /home/zyf/microsvr/zyfcloud-order-service/logs/ } ############### Main ############### log init_softlink start... init_softlink log init_softlink end... log [~INIT_SOFTLINK OPERATE SUCCESS~]三、复杂应用实现案例一背景说明这次我们将设计一个脚本不仅实现软链接挂载还包括以下功能备份和恢复功能在创建软链接之前备份现有目录并提供恢复功能。多目录处理支持多个目录的批量处理。日志轮转实现日志文件的轮转避免日志文件过大。邮件通知在操作完成后发送邮件通知包含操作结果。配置文件支持使用配置文件定义需要处理的目录和其他参数增强灵活性。也就是说需要我们设计一个脚本通过软链接将多个目录挂载到目标目录并包含备份、恢复、日志管理和邮件通知等功能。二具体编写思路日志管理实现日志文件轮转确保日志文件不会过大。同时要求添加详细的日志记录方便问题定位。软链接创建和权限管理在创建软链接前备份现有目录提供恢复功能以防操作失误。批量处理多个目录并设置相应的权限。邮件通知在操作完成后发送邮件通知包含操作结果。配置文件支持使用配置文件定义需要处理的目录和其他参数增强脚本的灵活性。三初版脚本代码配置文件config.txt# Directory mapping: target_directory source_directory /home/target/dir1 /home/source/dir1 /home/target/dir2 /home/source/dir2 # Email settings email_recipientadminexample.com脚本代码manage_softlink.sh#!/bin/bash # Program: Advanced management of soft links for directories # History: 2024.07.06 by [Your Name] LOG_DIR/var/log/manage_softlink LOG_FILE${LOG_DIR}/manage_softlink_$(date %Y%m%d).log MAX_LOG_SIZE10485760 # 10 MB NOWTIME$(date %Y%m%d%H%M%S) CONFIG_FILEconfig.txt # Ensure log directory exists mkdir -p ${LOG_DIR} # Print timestamp getTimeStamp() { echo $(date %Y-%m-%d %H:%M:%S) } # Print log for analyzing log() { echo $1 echo $(getTimeStamp) : $1 ${LOG_FILE} } # Rotate logs if size exceeds MAX_LOG_SIZE rotate_logs() { if [ -f ${LOG_FILE} ] [ $(stat -c%s ${LOG_FILE}) -ge ${MAX_LOG_SIZE} ]; then mv ${LOG_FILE} ${LOG_FILE}.${NOWTIME}.bak log Rotated log file ${LOG_FILE} fi } # Create a soft link for directories common_softlink() { local targetdir$1 local sourcedir$2 if [ ! -d ${targetdir} ]; then mkdir -p ${targetdir} log Created directory ${targetdir} fi if [ -L ${sourcedir} ]; then log Removing existing symlink ${sourcedir} rm ${sourcedir} || { log Error removing symlink ${sourcedir} return 1 } elif [ -d ${sourcedir} ]; then log Backing up existing directory ${sourcedir} mv -f ${sourcedir} ${sourcedir}_${NOWTIME} || { log Error backing up directory ${sourcedir} return 1 } fi ln -s ${targetdir} ${sourcedir} || { log Error creating symlink from ${targetdir} to ${sourcedir} return 1 } log Created symlink from ${targetdir} to ${sourcedir} # Update permissions if necessary chmod 755 ${sourcedir} chown zyf:root ${sourcedir} chmod -R 755 ${targetdir} chown -R zyf:root ${targetdir} log Updated permissions for ${sourcedir} and ${targetdir} } # Initialize the soft link setup init_softlink() { while IFS read -r line; do if [[ $line ~ ^#.*$ ]] || [[ -z $line ]]; then continue fi targetdir$(echo $line | awk {print $1}) sourcedir$(echo $line | awk {print $2}) common_softlink ${targetdir} ${sourcedir} done ${CONFIG_FILE} } # Send email notification send_email() { local recipient$1 local subject$2 local body$3 echo $body | mail -s $subject $recipient } # Main script execution log init_softlink start... rotate_logs init_softlink log init_softlink end... log [~INIT_SOFTLINK OPERATE SUCCESS~] # Read email recipient from config email_recipient$(grep email_recipient ${CONFIG_FILE} | cut -d -f2) # Send email notification send_email ${email_recipient} Softlink Initialization Completed The softlink initialization script has completed successfully. log Notification sent to ${email_recipient}四思考进一步优化优化建议错误处理对所有关键操作添加错误捕获和处理机制确保操作的可靠性。在脚本的各个阶段添加错误处理逻辑记录并报告错误。配置文件增强配置文件支持更多的自定义参数如日志文件路径、备份路径等。使用更具可读性的配置文件格式如YAML或JSON提高配置文件的可维护性。日志轮转改进使用系统日志管理工具如logrotate进行日志轮转简化脚本逻辑。邮件通知增强支持SMTP配置确保邮件发送的可靠性和安全性。优化的脚本部分下面是针对上述优化建议的部分实现。YAML 配置文件config.yamldirectories: - target: /home/target/dir1 source: /home/source/dir1 - target: /home/target/dir2 source: /home/source/dir2 email: recipient: adminexample.com smtp: server: smtp.example.com port: 587 user: userexample.com pass: password优化后的脚本代码#!/bin/bash # Program: Advanced management of soft links for directories # History: 2018.12.20 zyf # Updated: 2024.07.06 by [Your Name] LOG_DIR/var/log/manage_softlink LOG_FILE${LOG_DIR}/manage_softlink_$(date %Y%m%d).log CONFIG_FILEconfig.yaml NOWTIME$(date %Y%m%d%H%M%S) # Ensure log directory exists mkdir -p ${LOG_DIR} # Print timestamp getTimeStamp() { echo $(date %Y-%m-%d %H:%M:%S) } # Print log for analyzing log() { echo $1 echo $(getTimeStamp) : $1 ${LOG_FILE} } # Rotate logs if size exceeds MAX_LOG_SIZE rotate_logs() { if [ -f ${LOG_FILE} ] [ $(stat -c%s ${LOG_FILE}) -ge ${MAX_LOG_SIZE} ]; then mv ${LOG_FILE} ${LOG_FILE}.${NOWTIME}.bak log Rotated log file ${LOG_FILE} fi } # Create a soft link for directories common_softlink() { local targetdir$1 local sourcedir$2 if [ ! -d ${targetdir} ]; then mkdir -p ${targetdir} log Created directory ${targetdir} fi if [ -L ${sourcedir} ]; then log Removing existing symlink ${sourcedir} rm ${sourcedir} || { log Error removing symlink ${sourcedir} return 1 } elif [ -d ${sourcedir} ]; then log Backing up existing directory ${sourcedir} mv -f ${sourcedir} ${sourcedir}_${NOWTIME} || { log Error backing up directory ${sourcedir} return 1 } fi ln -s ${targetdir} ${sourcedir} || { log Error creating symlink from ${targetdir} to ${sourcedir} return 1 } log Created symlink from ${targetdir} to ${sourcedir} # Update permissions if necessary chmod 755 ${sourcedir} chown zyf:root ${sourcedir} chmod -R 755 ${targetdir} chown -R zyf:root ${targetdir} log Updated permissions for ${sourcedir} and ${targetdir} } # Initialize the soft link setup init_softlink() { while IFS read -r line; do if [[ $line ~ ^#.*$ ]] || [[ -z $line ]]; then continue fi targetdir$(echo $line | awk {print $1}) sourcedir$(echo $line | awk {print $2}) common_softlink ${targetdir} ${sourcedir} done (yq eval .directories[] | \(.target) \(.source) ${CONFIG_FILE}) } # Send email notification send_email() { local recipient$1 local subject$2 local body$3 local smtp_server$(yq eval .email.smtp.server ${CONFIG_FILE}) local smtp_port$(yq eval .email.smtp.port ${CONFIG_FILE}) local smtp_user$(yq eval .email.smtp.user ${CONFIG_FILE}) local smtp_pass$(yq eval .email.smtp.pass ${CONFIG_FILE}) sendmail -S ${smtp_server}:${smtp_port} -au${smtp_user} -ap${smtp_pass} \ -f ${smtp_user} -t ${recipient} -u ${subject} -m ${body} } ############### Main ############### log init_softlink start... rotate_logs init_softlink log init_softlink end... log [~INIT_SOFTLINK OPERATE SUCCESS~] # Read email recipient from config email_recipient$(yq eval .email.recipient ${CONFIG_FILE}) # Send email notification send_email ${email_recipient} Softlink Initialization Completed The softlink initialization script has completed successfully. log Notification sent to ${email_recipient}四、总结在本文中我们深入探讨了如何通过 Bash 脚本有效管理软链接涵盖了从基础操作到复杂应用的实现着重介绍了日志管理、备份恢复、邮件通知等功能。通过优化脚本提高了脚本的灵活性和可靠性。希望读者能够通过本文获得有效的运维工具提升工作效率并减少人为错误进而更好地管理软链接操作推动日常运维任务的自动化和高效化。
返回列表