创建 powershell 脚本来备份文件并附加日期
目前我有一个一行批处理文件来备份文件。当我需要备份文件时,我手动运行它。我唯一想添加的是当前日期。这是我所拥有的:
xcopy /W /Y ACTIVE.DB ACTIVE.DB.BACKUP
目标文件应该只是 ACTIVE.DB.BACKUP.YYYYMMDD。我将如何创建一个脚本,允许我从 Windows 资源管理器中双击它并进行 xcopy?
Currently I have a one line batch file to back up a file. I run it manually when I need to back up a file. The only thing I would like to add to it is the current date. Here is what I have:
xcopy /W /Y ACTIVE.DB ACTIVE.DB.BACKUP
the destination file should simply be ACTIVE.DB.BACKUP.YYYYMMDD. How would I go about creating a script that will allow me to double click on it from Windows Explorer and make the xcopy happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是要指出,您可以使用 Copy-Item 来做到这一点,例如:
如果您想要健壮,那么我会使用robocopy.exe。
Just to point out that you can do this with Copy-Item e.g.:
If you're going for robust then I'd use
robocopy.exe
.您可以通过在 PowerShell 中的文件名中嵌入格式化的
[datetime]::now
来自定义文件名,如下所示:如果该行感觉繁忙且难以维护,您可以将其重构为多行:
要获得双倍-单击执行,我通常制作一个运行 PowerShell 命令的批处理文件,如下所述:
设置PowerShell脚本以自动执行
You can customize your filename by embedding a formatted
[datetime]::now
in the file name in PowerShell like so:If the line feels busy and unmaintainable, you can refactor it to multiple lines:
To get double-click execution, I usually make a batch file that runs the PowerShell command as described here:
Set up PowerShell Script for Automatic Execution
我本月刚刚在 Powershell 中制作了每日/每周/每月/每季度/每年的备份脚本,希望它有所帮助。
此 DWMQY 备份方案是将源文件夹压缩到以日期命名的 zip 文件,然后保留以下内容的 zip:
每天按计划运行,它将 zip 放入目标文件夹中,该文件夹也是 Microsoft OneDrive 的本地文件夹,因此 zip 也会远程同步到 OneDrive 服务器。那些过时的(非周五每日或非最后 DWMQY)zip 将被移动到非远程同步文件夹。
今天是 2016 年 3 月 5 日,目标文件夹中应包含以下 zip:
所以会有 23 个 zip (实际上比 DWMQY 中的 dups 少),我们的文件是 250 个文本文档,压缩后为 0.4 GB,所以它是 23*0.4 =总共 9.2 GB,小于 OneDrive 免费 15 GB 配额。
对于大型源数据,可以使用 7-zip,它提供最大 1600 万 TB 的 zip 大小。对于直接备份文件夹而不是zip,还没有尝试过。猜测这是从当前的zip方式转移的过程。
I just made a Daily/Weekly/Monthly/Quarterly/Yearly backup script in Powershell this month, and hope it helps.
This DWMQY backup scenario is to zip a source folder to a date-named zip file, then keep the zips of:
Running as scheduled task on daily basis, it puts the zips into a target folder which is also a Microsoft OneDrive's local folder, so the zips also being remotely sync'd to OneDrive server. Those outdated (non-Friday daily or non-last-DWMQY) zips will be moved to a non-remotely-sync'd folder.
It's March 5, 2016 today, the following zips should be in the target folder:
So there will be 23 zips (actually less since the dups among DWMQY), our files are 250 text documents which is 0.4 GB after zipping, so it's 23*0.4 = 9.2 GB in total, which is less than OneDrive free 15 GB quota.
For large source data, 7-zip can be used, which provides maximum 16 mil TB zip size. For directly backup folders instead of zips, haven't tried. Guessing it's a transferable procedure from the current zip way.