AWS托管更新覆盖本地文件,如何保留数据并启用更新?
问题
我用AWS Beanstalk托管了一个应用,应用会把配置存在.property文件,历史数据存在.csv文件,原本运行正常。某天突然发现所有数据被回滚到初始状态:应用能正常运行,但.property里的配置和.csv的历史数据全没了。
查看Beanstalk实例历史,发现AWS执行了系统更新操作,推测是维护时用我上次上传的应用部署.zip包重新部署了应用。
我知道可以用数据库持久化数据,但改动太大。现在暂时关闭了Managed updates功能,但考虑到更新补丁,实例会逐渐过时。
请问怎么在开启托管更新的前提下,不用上次的.zip包部署,而是更新后用当前工作目录(推测是/app目录)部署?
AWS托管更新操作日志
2022-08-12 23:15:46 UTC+1000 INFO Environment update completed successfully. 2022-08-12 23:15:46 UTC+1000 INFO Successfully deployed new configuration to environment. 2022-08-12 23:14:38 UTC+1000 INFO Updating environment xxx-env's configuration settings. 2022-08-12 23:14:30 UTC+1000 INFO Environment update is starting. 2022-08-12 07:13:20 UTC+1000 INFO Environment health has transitioned from Info to Ok. Configuration update completed 83 seconds ago and took 11 minutes. 2022-08-12 07:12:37 UTC+1000 INFO Environment update completed successfully. 2022-08-12 07:12:37 UTC+1000 INFO Successfully deployed new configuration to environment. 2022-08-12 07:12:21 UTC+1000 INFO Removed instance [i-025394c6b5dc] from your environment. 2022-08-12 07:09:09 UTC+1000 INFO Deployment succeeded. Terminating old instances and temporary Auto Scaling group. 2022-08-12 07:07:50 UTC+1000 INFO Updating environment xxx-env's configuration settings. 2022-08-12 07:07:20 UTC+1000 INFO Waiting for post-deployment configuration to complete. 2022-08-12 07:07:16 UTC+1000 INFO Instance deployment completed successfully. 2022-08-12 07:06:39 UTC+1000 INFO Starting post-deployment configuration on new instances. 2022-08-12 07:06:11 UTC+1000 INFO Attached new instance(s) to the permanent auto scaling group awseb-e-wiuxjqswqj-stack-AWSEBAutoScalingGroup-AV. 2022-08-12 07:06:07 UTC+1000 INFO Detached new instance(s) from temporary auto scaling group awseb-e-wiuxjqswqj-immutable-stack-AWSEBAutoScalingGroup-1I2W. 2022-08-12 07:04:29 UTC+1000 INFO Waiting for instance(s) (i-0463ad18900a) to pass health checks. 2022-08-12 07:03:21 UTC+1000 INFO Added instance [i-0463ad18900a] to your environment. 2022-08-12 07:03:21 UTC+1000 INFO Environment health has transitioned from Ok to Info. Configuration update in progress on 1 instance. 0 out of 1 instance completed (running for 2 minutes). All instances are in same availability zone (us-west-2c). 2022-08-12 07:00:52 UTC+1000 INFO Created temporary auto scaling group awseb-e-wiuxjqswqj-immutable-stack-AWSEBAutoScalingGroup-1I2. 2022-08-12 07:00:21 UTC+1000 INFO Immutable deployment policy enabled. Launching one instance with the new settings to verify health. 2022-08-12 07:00:13 UTC+1000 INFO Environment update is starting. 2022-08-12 07:00:11 UTC+1000 INFO Managed platform update is in-progress.
解决方案
1. 迁移数据到实例持久化目录
Beanstalk重新部署时会覆盖/var/app/current目录,但实例EBS卷上的其他目录不会被改动。操作步骤:
- 修改应用代码,将
.property和.csv文件的存储路径改为/var/app/data - 在应用根目录创建
.ebextensions/01-setup-data-dir.config配置文件,内容如下:
commands: 01_create_dir: command: "mkdir -p /var/app/data && chown webapp:webapp /var/app/data"
该配置会在每次部署或实例更新时自动创建目录并设置正确权限,数据会持久化在EBS卷上,不会被覆盖。
2. 切换部署策略为滚动更新
日志显示当前启用了不可变部署策略,该策略会创建全新实例并重新部署原始包,旧实例销毁后数据丢失。切换为滚动更新:
- 进入Beanstalk控制台,打开目标环境的「配置」>「部署」页面
- 将部署策略改为「滚动更新」,单实例环境可设置批次大小为100%
- 滚动更新会在现有实例上直接应用平台更新,不会重建实例,数据得以保留。
3. 配置托管更新不重新部署应用
在托管更新设置中,可指定仅更新平台配置,不重新部署应用包:
- 进入环境的「配置」>「平台更新」
- 在「托管更新」模块,将「应用部署选项」设为「仅更新平台配置,不重新部署应用」
- 此设置下,AWS仅更新操作系统、运行时补丁等,不会覆盖应用目录内的数据。
4. 可选:定期备份数据到S3
为防止EBS卷故障导致数据丢失,可添加定时任务同步数据到S3:
- 创建
.ebextensions/02-s3-backup.config配置文件:
files: /etc/cron.daily/s3-backup: mode: "0755" owner: root group: root content: | #!/bin/bash aws s3 sync /var/app/data s3://your-bucket-name/app-data/ --delete
- 给Beanstalk实例的IAM角色添加
s3:PutObject和s3:ListBucket权限,确保能正常同步。
内容的提问来源于stack exchange,提问作者Zhang Zhan
相关产品推荐
相关产品推荐

