mac命令行将输出写入文件
介绍
(
Introduction
)
Sometimes we need to move our local files, SQL scripts, backups from our local machine to Azure or vice versa. This can be done manually by accessing to Azure and using a browser, but there are other methods to automatically do this. This article describes the Microsoft Azure Storage Tool. This is a command line tool used to upload data to Azure from a local machine or to download data from Azure to our local machine. The article will describe step by step how to work with this tool.
有时我们需要将本地文件,SQL脚本,备份从本地计算机移至Azure,反之亦然。 可以通过访问Azure并使用浏览器来手动完成此操作,但是还有其他方法可以自动执行此操作。 本文介绍了Microsoft Azure存储工具。 这是一个命令行工具,用于将数据从本地计算机上载到Azure或将数据从Azure下载到我们的本地计算机。 本文将逐步介绍如何使用此工具。
Figure
0
.
图
0
。
要求
(
Requirements
)
-
An Azure subscription.
Azure订阅。
-
A local machine with Windows installed.
安装Windows的本地计算机。
-
It comes with the PowerShell installer, which can be downloaded
here.
-
它带有PowerShell安装程序,可在
此处
下载
。
-
You will also require having a storage in Azure. For more information about creating a storage in Azure, refer to our
article to create storage section
.您还需要在Azure中具有存储。 有关在Azure中创建存储的更多信息,请参阅我们的
文章“创建存储”部分
。
Req
1. The Azure storage
-
In the storage, it is requited a container. For more information, refer to our
article related to storage and containers.
在存储中,它被退还到一个容器中。 有关更多信息,请参阅
与存储和容器有关
的
文章。
Req
2.
The storage container
-
The storage contains an option to manage access keys. This option will let you handle the keys.
存储中包含一个用于管理访问密钥的选项。 此选项将让您处理按键。
Req3. The manage access keys
要求3。
管理访问密钥
-
The access keys will be used to connect to azure from the AzCopy command line.
快捷键将用于从AzCopy命令行连接到Azure。
Req
4. The different access keys.
要求
4.不同的访问键。
入门
(
Getting started
)
-
Once installed the PowerShell as specified, open the Microsoft Azure Storage command line.
按照指定的步骤安装PowerShell后,打开Microsoft Azure存储命令行。
Figure 1
. The Microsoft Azure Storage command line
图1
。
Microsoft Azure存储命令行
-
Let’s start copying files from the local machine to Azure. Imagine that we have the following folder with SQL notes and SQL Server scripts in our local machine:
让我们开始将文件从本地计算机复制到Azure。 想象一下,我们在本地计算机中具有以下文件夹,其中包含SQL注释和SQL Server脚本:
Figure 2. The local folder
图2.本地文件夹
-
In order to copy the information from the local folder to our container created on the requirements section, use the following command:
为了将信息从本地文件夹复制到在“需求”部分创建的容器,请使用以下命令:
AzCopy /Source:c:\scripts3\
/Dest:https://mysqlshackstorage.blob.core.windows.net/cont1/
/DestKey:pGFSbfuUMjDBQZqIbfJuwksVp5
AzCopy / Source:c:\ scripts3 \
/目标:https://mysqlshackstorage.blob.core.windows.net/cont1/
/ DestKey:pGFSbfuUMjDBQZqIbfJuwksVp5
AzCopy is the command to copy file(s) from/to Azure. It is similar to the copy command line in the cmd. It requires a source and a destination. In this example, source is the c:\scripts3 folder. The destination is the URL of the container (see the req 2 picture on requirements). The container will store the files of the scripts folder. Finally, the DestKey is the key to access to Azure. You can find the key in the req 4 picture of the requirements section.
AzCopy是用于将文件从Azure复制到Azure的命令。 它类似于cmd中的复制命令行。 它需要一个源和一个目的地。 在此示例中,源是c:\ scripts3文件夹。 目的地是容器的URL(请参阅需求的第2张图片)。 容器将存储脚本文件夹的文件。 最后,DestKey是访问Azure的关键。 您可以在需求部分的需求4图片中找到该密钥。
-
Once you run the commands, you will have a transfer summary result similar to this one:
运行命令后,您将获得与以下内容相似的传输摘要结果:
Figure
3. The
AzC
opy
results
图
3.
AzC
opy
结果
-
In order to verify, go to storage and click on the storage created on the requirements.
为了验证,请转到存储并且单击在需求创建的存储。
Figure 4. The storage
图4.存储
-
In the storage, go to the container created on the requirements.
在存储中,转到根据需求创建的容器。
Figure 5. The storage container
-
You will find the files of the local folder of the Figure 2 copied to Azure.
您会发现复制到Azure的图2的本地文件夹中的文件。
Figure 6. The file copied to the container.
-
In order to obtain more information about the azcopy command, write this commands at the command line:
为了获得有关azcopy命令的更多信息,请在命令行中编写以下命令:
Azcopy /?
Azcopy /?
-
The command will show information about the parameters and some useful examples.
该命令将显示有关参数的信息和一些有用的示例。
Figure 7. The Azcopy help
图7. Azcopy帮助
-
Let’s try another example. In this example we will copy the myfile.txt from Azure to a local folder:
让我们尝试另一个示例。 在此示例中,我们将myfile.txt从Azure复制到本地文件夹:
AzCopy
/Source:https://mysqlshackstorage.blob.core.windows.net/cont1/
/Dest:c:\test\
/SourceKey:pGFSbfuUMjDBQZqIbfJuwksV/Pattern:”myfile.txt”
AzCopy
/来源:https://mysqlshackstorage.blob.core.windows.net/cont1/
/目标:c:\ test \
/SourceKey:pGFSbfuUMjDBQZqIbfJuwksV/Pattern:“myfile.txt”
To source is the url of the azure storage container. The destination is the c:\test folder. This forder is empty. The Azure container contains multiple files. In this example we only one to copy the file myfile.txt. We will use the Pattern parameter for this purpose.
来源是天蓝色存储容器的URL。 目的地是c:\ test文件夹。 这是空的。 Azure容器包含多个文件。 在此示例中,我们仅复制一个文件myfile.txt。 为此,我们将使用Pattern参数。
-
You will receive a message similar to this one after executing the command:
执行以下命令后,您将收到类似于此消息:
Figure
8
. Downloading results
图
8
。
下载结果
-
You will be able to see the file copied from azure to your local machine:
您将能够看到从azure复制到本地计算机的文件:
Figure 9
.
The local folder with the file copied from Azure
图9
。
从Azure复制文件的本地文件夹
-
There are other parameters that the command line includes:
命令行还包含其他参数:
/S which means recursive mode and includes the subfolders.
/Y is used to supress the confirmation prompts
/L is used to list operations
/A is used to upload files with the Archive Attribute Set
/MT is used to keep the source modified date time
/XN excludes files if they are newer than the destination
/XO excludes the source files if they are older than the destination
/V:[verbose log-file] this parameter is used to save the output in a log file, by default the log file is in the %LocalAppData%\Microsoft\Azure\AzCopy/ S表示递归模式,包括子文件夹。
/ Y用于禁止确认提示
/ L用于列出操作
/ A用于上载具有存档属性集的文件
/ MT用于保留源修改日期时间
/ XN如果文件比目标文件新,则将其排除
/ XO如果源文件早于目标文件,则将其排除
/ V:[详细日志文件]此参数用于将输出保存到日志文件中,默认情况下,日志文件位于%LocalAppData%\ Microsoft \ Azure \ AzCopy中 -
In this new example, we have a folder with multiple files. We want to copy only the PowerShell scripts:
在这个新示例中,我们有一个包含多个文件的文件夹。 我们只想复制PowerShell脚本:
Figure 10.
Several files in a local folder
图10.
本地文件夹中的几个文件
-
The following command will copy all the files with ps (PowerShell) extension and store the results in a log file:
以下命令将复制所有扩展名为ps(PowerShell)的文件,并将结果存储在日志文件中:
AzCopy /Source:c:\scripts\
/Dest:https://mysqlshackstorage.blob.core.windows.net/cont1/
/DestKey:pGFSbfuUMjDBQZqIbfJuwksVp5 == /Pattern:”*.Ps” /V
AzCopy / Source:c:\ scripts \
/目标:https://mysqlshackstorage.blob.core.windows.net/cont1/
/ DestKey:pGFSbfuUMjDBQZqIbfJuwksVp5 == /模式:“ *。Ps” / V
-
If everything is OK, you will receive a message similar to this one:
如果一切正常,您将收到类似于以下消息:
Figure 11. The copy results
图11.复制结果
-
You will also be able to see the results in a log file. The /V parameter is used to store the information in this folder %LocalAppData%\Microsoft\Azure\AzCopy.
您还可以在日志文件中查看结果。 / V参数用于将信息存储在此文件夹%LocalAppData%\ Microsoft \ Azure \ AzCopy中。
Figure 12. The Azure Log file
-
You will also be able to see the the ps files copied in Azure.
您还将能够看到在Azure中复制的ps文件。
Figure 13. The Po
werShell script copied to Azure
-
Finally, you can execute the commands from a file. Let’s create a file with AzCopy commands named AzCommand.txt:
最后,您可以从文件执行命令。 让我们使用名为AzCommand.txt的AzCopy命令创建一个文件:
/Source:c:\scripts\
/Dest:https://mysqlshackstorage.blob.core.windows.net/cont1/
/DestKey:pGFSbfuUMjDBQZqIbfJuwksVp5BkD7CFv/GxvdrOwiWvAYGLc5D5J5ZKjtIpipb2djiaEmOX3QhExWVOHSC0sQ== /Pattern:”*.Ps” /V
/来源:c:\ scripts \
/目标:https://mysqlshackstorage.blob.core.windows.net/cont1/
/ DestKey:pGFSbfuUMjDBQZqIbfJuwksVp5BkD7CFv / GxvdrOwiWvAYGLc5D5J5ZKjtIpipb2djiaEmOX3QhExWVOHSC0sQ == /模式:“ *。Ps” / V
-
Now, execute the txt file with the following command line:
现在,使用以下命令行执行txt文件:
azcopy /@:”C:\azcommand.txt”
azcopy /@:“C:\azcommand.txt”
-
The command will execute the txt content. If everything is OK you will have the following result:
该命令将执行txt内容。 如果一切正常,您将得到以下结果:
-
Figure 14. The transfer summary
图14.转移摘要
-
You can also verify in Azure that the files were copied
您还可以在Azure中验证文件是否已复制
结论
(
Conclusion
)
As you can see, the Azure Storage Tools can be used to automate some tasks using the command line. Once you have a Storage with a container with Azure, the process to upload or download files is very simple. We also learned different parameters and how to specify patterns to choose specific files.
如您所见,Azure存储工具可用于通过命令行自动执行某些任务。 一旦有了带有容器的Azure存储,上传或下载文件的过程就非常简单。 我们还学习了不同的参数以及如何指定模式以选择特定文件。
Note
: This AzCopy command line is currently in the version 4.1 (Current Previous Version). The new versions may include new commands in the future. Make sure to have the last version.
注意
:此AzCopy命令行当前处于版本4.1(当前先前版本)中。 新版本将来可能包含新命令。 确保具有最新版本。
mac命令行将输出写入文件