asd 内存硬盘
最后更新于:2022-04-02 03:51:57
[TOC]
> [参考鸟哥](https://www.laruence.com/2020/02/15/4982.html)
>[asd 官网](https://wiki.archlinux.org/index.php/Anything-sync-daemon_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87))
## 概述
使用内存硬盘(tmpfs)来加速你的网站,操作前必须备份原始目录内容
## 创建内存硬盘目录
```
mount -t tmpfs -o size=256M tmpfs /ramdisk
```
256M,会按实际占用的大小分配内存,不会直接占用256M
## Anything-sync-daemon 同步目录
```
wget https://github.com/graysky2/anything-sync-daemon/archive/master.zip
cd anything-sync-daemon-master/
make install-systemd-all
```
问题:如果 没有把服务安装到 /usr/lib/systemd/system 下
手动移动
```
mv asd-resync.service asd-resync.timer asd.service /lib/systemd/system
systemctl daemon-reload
```
### 修改 asd.conf
`/etc/asd.conf` or `/run/asd.conf`
```
#要同步的目录 ,多目录用逗号隔开
WHATTOSYNC=('/home/huixinchen/www/htdocs/')
VOLATILE="/ramdisk" // tmpfs目录
```
### 设置同步时间
vim /lib/systemd/system/asd-resync.timer
```
[Unit]
Description=Timer for Anything-sync-daemon - 1Hour
PartOf=asd-resync.service asd.service
[Timer]
# 分钟 10min
OnUnitActiveSec=24h
```
### `asd p` 检查
```
$ asd p
Anything-sync-daemon v5.85 on Ubuntu 18.04.4 LTS
Daemon pid file is not present.
Resync cronjob is not present.
Overlayfs technology is currently inactive.
Asd will manage the following per /etc/asd.conf settings:
owner/group id: huixinchen/1000
target to manage: /home/huixinchen/www/htdocs
sync target: /home/huixinchen/www/.htdocs-backup_asd
tmpfs target: /ramdisk/asd-huixinchen/home/huixinchen/www/htdocs
dir size: 237M
recovery dirs: none
```
1. asd会把`/home/huixinchen/www/htdocs `目录同步到 `/ramdisk/asd-huixinchen/home/huixinchen/www/htdocs`
2. 并且会把tmpfs上的更新内容按照时间写回到 `/home/huixinchen/www/.htdocs-backup_asd`
3. 当我们停止asd的服务的时候,asd会把`.htdocs-backup_asd`在mv成`htdocs`,这样就不用担心你的内容会因为服务器突然断电丢失了
### 启动 asd
```
systemctl restart asd
```
### 软连接映射
* 启动 asd 后 `/home/huixinchen/www` 下的`htdocs`自动映射到 `/ramdisk/asd-huixinchen/home/huixinchen/www/htdocs/`
* 关闭 asd,映射自动取消,所以 nginx 或apache 的 htdocs 不需要改
```
> systemctl stop asd
> ll im_webserver
drwxrwxr--. 14 im_user im_user 4096 Feb 17 09:24 apache
drwxrwxr-x. 2 im_user im_user 4096 May 25 2017 common
drwxrwxrwx. 15 im_user im_user 4096 Feb 17 09:24 mysql
drwxrwxr--. 7 im_user im_user 60 Feb 17 09:24 php
drwxrwxr-x. 7 im_user im_user 4096 Feb 17 15:11 www
> systemctl start asd
> ll /home/im_user/im_server/im_webserver
drwxrwxr--. 14 im_user im_user 4096 Feb 17 09:24 apache
drwxrwxr-x. 2 im_user im_user 4096 May 25 2017 common
drwxrwxrwx. 15 im_user im_user 4096 Feb 17 09:24 mysql
drwxrwxr--. 7 im_user im_user 60 Feb 17 09:24 php
lrwxrwxrwx. 1 im_user im_user 60 Feb 18 11:18 www -> /ramdisk/asd-im_user/home/im_user/im_server/im_webserver/www
```
';