没有不值得去解决的问题,也没有不值得去学习的技术!

Copy Files to NAS Storage on Container Startup with CakePHP v2.6.4

Check the container logs; it ran successfully and met expectations.

1. There is currently a new requirement to perform some initial data setup for the product during deployment. The corresponding SQL files are prepared in advance, but the file paths in the MySQL table, such as /upload/20200817/20203554555373.png, are stored in NAS storage rather than under the program’s directory. See Figure 1.

There is currently a new requirement to perform some initial data setup for the product during deployment. The corresponding SQL files are prepared in advance, but the file paths in the MySQL table, such as /upload/20200817/20203554555373.png, are stored in NAS storage rather than under the program's directory.
Figure 1

2. The plan is to automatically execute a PHP command line when the container starts. It will check whether the default files exist in the NAS storage; if they exist, it will exit, and if they do not, it will copy them.

3. Implement the command line to place all files from app/webroot/images/20200817 into app/webroot/upload/20200817. Create a new file: app/Console/Command/AssetShell.php

Plaintext
<?php
/**
 * Created by PhpStorm.
 * User: Qiang Wang
 * Date: 2020/08/18
 * Time: 15:04
 */
 
App::uses('Folder', 'Utility');
 
class AssetShell extends AppShell {
    public function sync() {
        $folder = new Folder(WWW_ROOT . 'images' . DS . '20200817');
        $folder->copy(UP_DIR . DS . 'upload' . DS . '20200817');
        $this->out('Asset sync successfully.');
    }
}

4. Navigate to the directory: E:\wwwroot\creditshop\app, and execute: Console/cake asset sync to copy all files from app/webroot/images/20200817 to app/webroot/upload/20200817. See Figure 2.

Navigate to the directory: E:\wwwroot\creditshop\app, and execute: Console/cake asset sync to copy all files from app/webroot/images/20200817 to app/webroot/upload/20200817.
Figure 2
Plaintext

PS E:\wwwroot\creditshop\app> Console/cake asset sync


Welcome to CakePHP v2.6.4 Console
---------------------------------------------------------------
App : app
Path: E:\wwwroot\creditshop\app\
---------------------------------------------------------------
Asset sync successfully.


5. Run the corresponding command in the container to copy all files from /mcloud/creditshop/app/webroot/images/20200817 to /webtv/wangjie/creditbak/upload/20200817. /webtv is the mounted NAS storage. See Figure 3.

Run the corresponding command in the container to copy all files from /mcloud/creditshop/app/webroot/images/20200817 to /webtv/wangjie/creditbak/upload/20200817. /webtv is the mounted NAS storage.
Figure 3
Plaintext

[root@465e0aa491eb /]# cd /webtv/wangjie/creditbak/upload
[root@465e0aa491eb upload]# ls -l
total 44
drwxrwxrwx 2 nginx nginx  106 Dec 17  2018 20181217
drwxrwxrwx 2 nginx nginx 4096 Jan 24  2019 20190124
drwxrwxrwx 2 nginx nginx  156 Feb 21  2019 20190221
drwxrwxrwx 2 nginx nginx 4096 Feb 22  2019 20190222
drwxrwxrwx 2 nginx nginx 4096 Feb 25  2019 20190225
drwxrwxrwx 2 nginx nginx 4096 Feb 28  2019 20190228
drwxrwxrwx 2 nginx nginx  106 Apr  9  2019 20190409
drwxrwxrwx 2 nginx nginx  155 Sep 11  2019 20190911
drwxrwxrwx 2 nginx nginx 4096 Nov 11  2019 20191111
drwxrwxrwx 2 nginx nginx  130 Nov 12  2019 20191112
drwxrwxrwx 2 nginx nginx 4096 Nov 13  2019 20191113
drwxrwxrwx 2 nginx nginx  130 Nov 14  2019 20191114
drwxrwxrwx 2 nginx nginx 4096 Nov 19  2019 20191119
drwxrwxrwx 2 nginx nginx  106 Nov 27  2019 20191127
drwxrwxrwx 2 nginx nginx 4096 Nov 29  2019 20191129
drwxrwxrwx 2 nginx nginx  129 Dec 21  2019 20191221
drwxrwxrwx 2 nginx nginx 4096 Jul 16 22:33 20200716
drwxrwxrwx 2 nginx nginx 4096 Aug  3 22:34 20200803
drwxrwxrwx 2 nginx nginx 4096 Aug 18 16:04 20200818
[root@465e0aa491eb upload]# cd /mcloud/creditshop/app
[root@465e0aa491eb app]# Console/cake asset sync
bash: Console/cake: Permission denied
[root@465e0aa491eb app]# chmod +x Console/cake
[root@465e0aa491eb app]# Console/cake asset sync

Welcome to CakePHP v2.6.4 Console
---------------------------------------------------------------
App : app
Path: /mcloud/creditshop/app/
---------------------------------------------------------------
Asset sync successfully.
[root@465e0aa491eb app]# cd /webtv/wangjie/creditbak/upload
[root@465e0aa491eb upload]# ls -l
total 44
drwxrwxrwx 2 nginx nginx  106 Dec 17  2018 20181217
drwxrwxrwx 2 nginx nginx 4096 Jan 24  2019 20190124
drwxrwxrwx 2 nginx nginx  156 Feb 21  2019 20190221
drwxrwxrwx 2 nginx nginx 4096 Feb 22  2019 20190222
drwxrwxrwx 2 nginx nginx 4096 Feb 25  2019 20190225
drwxrwxrwx 2 nginx nginx 4096 Feb 28  2019 20190228
drwxrwxrwx 2 nginx nginx  106 Apr  9  2019 20190409
drwxrwxrwx 2 nginx nginx  155 Sep 11  2019 20190911
drwxrwxrwx 2 nginx nginx 4096 Nov 11  2019 20191111
drwxrwxrwx 2 nginx nginx  130 Nov 12  2019 20191112
drwxrwxrwx 2 nginx nginx 4096 Nov 13  2019 20191113
drwxrwxrwx 2 nginx nginx  130 Nov 14  2019 20191114
drwxrwxrwx 2 nginx nginx 4096 Nov 19  2019 20191119
drwxrwxrwx 2 nginx nginx  106 Nov 27  2019 20191127
drwxrwxrwx 2 nginx nginx 4096 Nov 29  2019 20191129
drwxrwxrwx 2 nginx nginx  129 Dec 21  2019 20191221
drwxrwxrwx 2 nginx nginx 4096 Jul 16 22:33 20200716
drwxrwxrwx 2 nginx nginx 4096 Aug  3 22:34 20200803
drwxr-xr-x 2 root  root   126 Aug 18 19:57 20200817
drwxrwxrwx 2 nginx nginx 4096 Aug 18 16:04 20200818
[root@465e0aa491eb upload]# cd 20200817
[root@465e0aa491eb 20200817]# ls -l
total 488
-rwxr-xr-x 1 root root 183116 Aug 18 17:16 20201164114857.png
-rwxr-xr-x 1 root root  32149 Aug 18 17:16 20202146638315.png
-rwxr-xr-x 1 root root 183116 Aug 18 17:16 20202503127232.png
-rwxr-xr-x 1 root root  96546 Aug 18 17:16 20203554555373.png
[root@465e0aa491eb 20200817]#

6. When the container starts, edit the Shell script to automatically run the PHP command line.

Plaintext
env | grep "CREDITSHOP_CFG_PIC_DATASTATUS" || export CREDITSHOP_CFG_PIC_DATASTATUS="off"
	if [[ $CREDITSHOP_CFG_PIC_DATASTATUS == "off" ]]
	then
		echo "Do not run command"
	elif [[ $CREDITSHOP_CFG_PIC_DATASTATUS == "on" ]]
	then
		cd /mcloud/creditshop/app
		chmod +x Console/cake
		Console/cake asset sync
		echo "Run command"
	fi

7. Check the container logs; it ran successfully and met expectations. See Figure 4.

Check the container logs; it ran successfully and met expectations.
Figure 4
Plaintext
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_TENANT_URL=http://tenant1.xcttest.chinamcloud.cn/
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_APPNAME=creditshop
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_SECRET=02bcebf67be82fc6c596f7aaab159833090189d8
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_PIC_PORT=80
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_SERVER_PORT=80
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_MEMBERAPI_URL=http://memberapi.wjtest.chinamcloud.cn/memberapi
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_IDA_URL=http://ida.xcttest.chinamcloud.cn
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_QYWX_KEY=okqb7vqm8y2I6Q7zeyyY9K1JfTzs
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_QYWX_URL=qywx.wjtest.chinamcloud.cn
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_PIC_DIR=/webtv/wangjie/creditbak
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_FILE_DIR=/webtv/wangjie/creditbak
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_MSERVER_URL=http://mserver.wjtest.chinamcloud.cn/creditbak
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_QYHD_URL=http://morefunapi.wjtest.chinamcloud.cn
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_BMSHD_URL=http://default.bms.wjtest.chinamcloud.cn
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_ACCESS_KEY_ID=DdEBQtT801LjMuYw
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_ACCESS_KEY_SECRET=FyVST8x73D1AKlhG9brEHvgWkwdeNipz
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_CHECK_USER_LOGIN_URL=http://cmcconsole.wjtest.chinamcloud.cn/interface/get-user-auth
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_USER_LOGIN_URL=http://cmcgroup.wjtest.chinamcloud.cn/?call_back=http://creditshop.wjtest.chinamcloud.cn
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_REDIS_HOST=192.168.2.6
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_REDIS_PASSWORD=q5dHP@xCBUUJY^L
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_REDIS_DB=5
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_FILE_DIR=/webtv/wangjie/creditbak
2020/8/19 下午 3:56:30 CREDITSHOP_CFG_PIC_DATASTATUS=on
2020/8/19 下午 3:56:30
2020/8/19 下午 3:56:30 Welcome to CakePHP v2.6.4 Console
2020/8/19 下午 3:56:30 ---------------------------------------------------------------
2020/8/19 下午 3:56:30 App : app
2020/8/19 下午 3:56:30 Path: /mcloud/creditshop/app/
2020/8/19 下午 3:56:30 ---------------------------------------------------------------
2020/8/19 下午 3:56:30 Asset sync successfully.
2020/8/19 下午 3:56:30 Run command

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系