Configuration Files
在 Linux 下,进行配置文件文件备份。实现的功能:
1)对配置文件进行备份。备份工具可以使用 rsync 命令的复制功能。备份不能使用 cp 命令,主要是因为配置文件重名或者发生调整时,cp 命令无法对原文件进行操作。
2)且能够自动对配置文件进行备份。自动备份可以使用 Cron 定时任务实现。
3)由版本控制,能切换到指定时间点。版本控制可以使用 git。在 Rsync 同步结束后,执行 git 提交变更。
3)灾后,能够实现快速恢复服务。使用 Rsync 进行反向复制即可。
配置文件丢失或者无法恢复,真的非常要命。
需要说明的几点
这里的“配置文件备份”区别于 cp file.conf{,.backup} 形式的备份。只要是为了解决灾后恢复、配置回滚等问题。
备份是按照配置文件在文件系统中的目录结构进行存储配置的。
解决方案(单机备份)
#1 创建脚本
备份脚本 /usr/local/bin/rsync-conf-backup.sh 内容如下:
#!/bin/bash
################################################################################
# 服务器配置文件备份
################################################################################
WORKTREE='/srv/backups/sysconf'
########################################
# 执行检查
########################################
echo -n "# check..."
# 创建配置文件保存目录
if [ ! -d "$WORKTREE" ]
then
echo "# $WORKTREE not exist"
echo "# creating ${WORKTREE}..."
mkdir -pv $WORKTREE
fi
cd $WORKTREE
if [ ! -f "$WORKTREE/.rsyncd.filter" ]
then
echo "# .rsyncd.filter not exist!"
echo "# creating .rsyncd.filter..."
cat <<EOF > .rsyncd.filter
################################################################################
# 系统配置文件备份
################################################################################
# 由于要删除备份目录中不存在的文件,所以要对某些文件进行排除
P/ /***/.rsyncd.filter
P/ /***/.git
-/ **
EOF
fi
if [ ! -d "$WORKTREE/.git" ]
then
echo "# .git/ not exist!"
echo "# git init..."
git init
fi
echo " done"
########################################
# 进行同步
########################################
echo -n "# rsync..."
# 执行同步
TMPFILE="$(mktemp)"
echo "####### rsync log start..." >> $TMPFILE
rsync -avz --delete-excluded \
--filter='merge /srv/backups/sysconf/.rsyncd.filter' / . >> $TMPFILE
echo "####### rsync log end..." >> $TMPFILE
echo " done"
########################################
# 进行版本控制
########################################
echo -n "# git..."
# 进行版本控制
git add .
git commit --quiet --file=$TMPFILE &>/dev/null
echo " done"
可以单独执行。
#2 配置定时任务
在 Debian 发行版中:
ln -s /usr/local/bin/rsync-conf-backup.sh /etc/cron.hourly/rsync-conf-backup
#3 指定需要备份的文件
注意事项
该备份只备份到了单机上。如果要备份到不同的机器上,可以:
- 使用 Rsync 同步到其他主机;
- 将备份目录挂载到网络文件系统上;
这并不是最佳实践,以为这个方法并不适用于主机集群。要是有十几二十几台机器就不能这么搞了。
更进一步(多机备份)
单击备份的方案很显然不适用于多台主机。下面是我目前正在使用的多机备份方案:
操作流程如下:
- 在 Test Server 上修改配置;
- 配置修改、测试通过后,使用 Rsync 推送到 Repo。并在 Repo 上进行版本控制;
- 然后使用 PSSH 配合 Rsync 将 Repo 上的配置推送到其他主机上;
Socket Files
Unix local sockets are created as soon as a program attempts to listen on the given path for connections, and despite being a type of file they only act as pointers to in-memory structures; so they are useful only as long as the program is still running (and only within the same machine; no NFS or anything such).
After the program exits, the socket file is no longer useful (and is normally deleted by the program itself); in fact, if the program gets restarted, it has to delete the old socket before listening on the same path – otherwise it would receive an “Address already in use” (the same as if two programs were trying to take the same TCP port).
This is somewhat different from named pipes (aka fifos), which work in a much simpler way (one process writes, one process reads) and therefore are reusable; a named pipe can be created using the mkfifo or mknod p … commands.