在容器启动时,复制文件至 NAS 存储中,基于 CakePHP v2.6.4 实现
1、现阶段存在一个新需求,需要在部署产品时,针对产品做一些初始化数据的工作。预先准备好相应的 SQL 文件,但是对于 MySQL 表中的文件路径:/upload/20200817/20203554555373.png。其存储于 NAS 存储中。而并非程序的目录下。如图1
2、计划在容器启动时,自动执行一个 PHP 命令行,判断 NAS 存储中,默认的文件是否存在,如果存在,则退出。不存在,则复制。
3、实现将 app/webroot/images/20200817 下的所有文件放入 app/webroot/upload/20200817 中的命令行。新建文件:app/Console/Command/AssetShell.php
<?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、进入目录:E:\wwwroot\creditshop\app,执行:Console/cake asset sync,将 app/webroot/images/20200817 下的所有文件复制至 app/webroot/upload/20200817 中。如图2
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、在容器中运行相应的命令,将 /mcloud/creditshop/app/webroot/images/20200817 下的所有文件复制至 /webtv/wangjie/creditbak/upload/20200817 中。/webtv 为所挂载的 NAS 存储。如图3
[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、在容器启动时,编辑 Shell 脚本,自动运行 PHP 命令行。
env | grep "CREDITSHOP_CFG_PIC_DATASTATUS" || export CREDITSHOP_CFG_PIC_DATASTATUS="off" if [[ $CREDITSHOP_CFG_PIC_DATASTATUS == "off" ]] then echo "不运行命令" elif [[ $CREDITSHOP_CFG_PIC_DATASTATUS == "on" ]] then cd /mcloud/creditshop/app chmod +x Console/cake Console/cake asset sync echo "运行命令" fi
7、查看容器的日志,成功运行,符合预期。如图4
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 运行命令
近期评论