1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <?php
use OSS\OssClient;
function handler($event, $context) { date_default_timezone_set('PRC');
$bucket = 'hongfs-backup-mysql-r7b0pl3buuwrg9g3'; $region = 'cn-shenzhen';
$host = 'mysql.hongfs.cn'; $port = '3306'; $user = 'hongfs'; $password = 'FKDdjYVV813lfj3H'; $database = 'hongfs';
$filename = $database . '-' . date('YmdHis') . '.sql'; $filepath = getenv('HOME') . '/' . $filename;
exec(sprintf('%smysqldump-57 --host=%s --port=%s --user=%s --password=%s --compact %s > %s', getenv('FC_FUNC_CODE_PATH'), $host, $port, $user, $password, $database, $filepath));
if(!is_file($filepath)) { return '失败'; }
$oss = new OssClient( $context['credentials']['accessKeyId'], $context['credentials']['accessKeySecret'], 'https://oss-' . $region . '-internal.aliyuncs.com', false, $context['credentials']['securityToken'] );
$oss->uploadFile($bucket, 'backup/' . $filename, $filepath);
return '完成'; }
|