Skip to main content

Chuyển data từ Amazon S3 về server

Using Amazon S3 data in PHP - Switch from S3 to server VPS

Bài này note lại một số lưu ý khi chuyển data từ S3 về server hay local, dừng dịch vụ S3.
Các bước dừng dịch vụ S3:
1. Download dữ liệu từ S3 về.
2. Sửa mã để dùng data trên server (hay cloud khác) thay vì S3. Cập nhật Database.
3. Hoàn tất: testing và các vđ liên quan.

1. Download: dùng awscli (trên ubuntu, osx cài dùng pip ), web interface hay wget, CURL ...
- Extract s3 url dùng mysql
mysql -e "select picture from post where picture like 'https://s3%'" -u username -p db_name > s3_post_list.txt
Dùng PHP nếu lệnh query phức tạp và đòi hỏi tính toán nhiều hơn. Như ở trên thì chỉ cần tìm record có trường picture có chữ https://s3-*** vì logic ở đây lưu cả URL S3. Nếu chỉ lưu tên object (như tên file, ảnh) trên S3 thì cần logic khác.
Việc lọc bớt ảnh chỉ của S3 vì có thể ảnh là embed (link từ gippy hay 1 cloud nào đó) để tránh lấy nhầm ảnh ko phải từ S3.
Sau khi có list ảnh thì download, vd dùng wget:

wget -i post_list.txt

Lệnh này tài hết file trong list, các link file ở các dòng.

2. Sửa code
Tùy theo logic trang web mà sửa. Sẽ hơi vất chút khi bạn chỉ maintain mà ko code từ đầu.
Lưu ý:
- Link ảnh, file trên server (dùng server lưu data) cần để ý https://host_name ...
- S3 ko có khái niệm folder, tất cả là object, nếu lúc đầu không phân folder trên S3. Bây h lưu data trên server mới phân folder thì cũng phải code lại logic.
Path file trong Database SQL sẽ cần lưu ý vd trước kia trên S3 là abcdeuteuhsou.png
bây h là 2016/01/01/abceuteuhsou.png ... nên nhiều logic liên quan như view, controller cần cập nhật.
- Số lượng, kích thước data lớn quá sẽ cần các thao tác khác bọt để port dữ liệu. Wget download file số lượng lớn tốt nhất chạy trên VPS vì thường tốc độ đường truyền trên VPS tốt hơn nhiều. Vd Digital Ocean download 1GB mất tầm 40s (tính cả tg linh tinh + vào), speed trung bình là 40MB/s.
Mạng cáp quang ở các cty cỡ vừa cũng thường được vài MB/s. Nhưng so với Server thì thua xa.
Sau khi tải file S3 về server thì có thể dùng rsync trên linux để backup dữ liệu.
rsync --ignore-existing --recursive /src /dst

digit ocean rsync
3. Hoàn tất
Test lại cẩn thận các thao tác.


Comments

Popular posts from this blog

Rand mm 10

https://stackoverflow.com/questions/2447791/define-vs-const Oh const vs define, many time I got unexpected interview question. As this one, I do not know much or try to study this. My work flow, and I believe of many programmer is that search topic only when we have task or job to tackle. We ignore many 'basic', 'fundamental' documents, RTFM is boring. So I think it is a trade off between the two way of study language. And I think there are a bridge or balanced way to extract both advantage of two method. There are some huge issue with programmer like me that prevent we master some technique that take only little time if doing properly. For example, some Red Hat certificate program, lesson, course that I have learned during Collage gave our exceptional useful when it cover almost all topic while working with Linux. I remember it called something like RHEL (RedHat Enterprise Linux) Certificate... I think there are many tons of documents, guide n books about Linux bu

Martin Fowler - Software Architecture - Making Architecture matter

  https://martinfowler.com/architecture/ One can appreciate the point of this presentation when one's sense of code smell is trained, functional and utilized. Those controlling the budget as well as developer leads should understand the design stamina hypothesis, so that the appropriate focus and priority is given to internal quality - otherwise pay a high price soon. Andrew Farrell 8 months ago I love that he was able to give an important lesson on the “How?” of software architecture at the very end: delegate decisions to those with the time to focus on them. Very nice and straight-forward talk about the value of software architecture For me, architecture is the distribution of complexity in a system. And also, how subsystems communicate with each other. A battle between craftmanship and the economics and economics always win... https://hackernoon.com/applying-clean-architecture-on-web-application-with-modular-pattern-7b11f1b89011 1. Independent of Frameworks 2. Testable 3. Indepe