在 CentOS 系统中,“env” 通常指环境变量(environment variables)。备份环境变量的方式和你所说的“env”具体指什么有关。下面分几种常见情况说明:
如果你只是想备份当前终端会话中所有环境变量:
env > env_backup_$(date +%F).txt
或:
printenv > env_backup.txt
恢复(仅对当前 shell 有效):
set -a
source env_backup.txt
set +a
⚠️ 注意:
用户环境变量通常在以下文件中:
~/.bash_profile
~/.bashrc
~/.profile
备份方法:
mkdir -p ~/backup/env
cp ~/.bash_profile ~/.bashrc ~/.profile ~/backup/env/
恢复:
cp ~/backup/env/* ~/
source ~/.bashrc
系统级环境变量一般在:
/etc/profile
/etc/bashrc
/etc/environment
/etc/profile.d/*.sh
备份命令:
mkdir -p /backup/env
cp /etc/profile /etc/bashrc /etc/environment /backup/env/
cp -r /etc/profile.d /backup/env/profile.d
恢复(需 root):
cp /backup/env/profile /etc/
cp /backup/env/bashrc /etc/
cp /backup/env/environment /etc/
cp -r /backup/env/profile.d/* /etc/profile.d/
如果是 systemd 服务:
systemctl show <服务名> --property=Environment
或查看:
cat /etc/systemd/system/<服务名>.service
备份:
cp /etc/systemd/system/<服务名>.service /backup/
set > full_env_backup.txt
包含变量 + 函数(不推荐用于恢复)
| 场景 | 备份方式 |
|---|---|
| 当前会话 | env > file |
| 用户环境 | 备份 ~/.bashrc 等 |
| 系统环境 | 备份 /etc/profile.d |
| 服务环境 | 备份 service 文件 |
如果你能补充说明:
我可以给你更精确的备份方案。