如何将我的 backup.cmd 转换为可以在 Linux 中运行的内容?

如何将我的 backup.cmd 转换为可以在 Linux 中运行的内容?

以前当我还在使用 Windows(并且是 IT 方面的新手)的时候,我非常喜欢批处理脚本,因此我编写了很多脚本,其中一个让我非常自豪的是我的 backup.cmd(见下文)。

我对 linux bash sudo/apt-get/sl/ls/locate
/updatedb/etc 非常熟悉...

我真的不知道终端的全部功能。如果您看到下面的代码,我可以让它在 (Ubuntu)linux 下工作吗 :) 通过使用 linux 等效代码重写一些 windows 代码(顺便说一句:这在​​ xp/vista/7 | 荷兰语/英语下有效)

@echo off
title back it up
:home
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º                                                      º
echo º typ A/B for the options                              º
echo º                                                      º
echo ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
echo º                                                      º
echo º "A"=backup options                                   º
echo º                                                      º
echo º "B"=HARDDISK Options                                 º
echo º                                                      º
echo º                                                      º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍŒ
set /p selection=Choose:
Goto %selection%
:A
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º                                                      º
echo º typ 1 to start that backup                           º
echo º                                                      º
echo ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
echo º                                                      º
echo º "A"=backup options                                   º
echo º  È1=Documents,Pictures,Music,Videos,Downloads        º
echo º                                                      º
echo º "B"=HARDDISK Options                                 º
echo º                                                      º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍŒ
set /p selection=Choose:
Goto %selection%
:B
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º                                                      º
echo º typ HD to start the disk check                       º
echo º                                                      º
echo ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
echo º                                                      º
echo º "A"=backup options                                   º
echo º                                                      º
echo º "B"=HARDDISK Options                                 º
echo º  ÈHD=find and repair bad sectors                     º
echo º                                                      º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍŒ
set /p selection=Choose:
Goto %selection%

:1
cls
if exist "%userprofile%\desktop" (set desk=desktop) else (set desk=Bureaublad)
if exist "%userprofile%\documents" (set docs=documents) else (set docs=mijn documenten)
if exist "%userprofile%\pictures" (set pics=pictures) else (echo cant find %userprofile%\pictures)
if exist "%userprofile%\music" (set mus=music) else (echo cant find %userprofile%\music)
if exist "%userprofile%\Videos" (set vids=videos) else (echo cant find %userprofile%\videos)
if exist "%userprofile%\Downloads" (set down=downloads) else (echo cant find %userprofile%\Downloads)
cls
echo. examples (D:\) (D:\Backup) (D:\Backup\18-4-2011)
echo.
echo. if there is no "D:\backup" folder then the folder will be created
echo. 
set drive=
set /p drive=storage:
echo start>>backup.log
echo Name:%username%>>backup.log
echo Date:%date%>>backup.log 
echo Time:%time%>>backup.log
echo ========================================%docs%===========================================>>backup.log
echo %docs%  
echo Source:"%userprofile%\%docs%" 
echo Destination:"%drive%\%username%\%docs%"
echo %time%>>backup.log
xcopy "%userprofile%\%docs%" "%drive%\%username%\%docs%" /E /I>>Backup.log
echo 20%%
cls
echo ========================================"%pics%"=========================================>>backup.log
echo "%pics%" 
echo Source:"%userprofile%\%pics%" 
echo Destination:"%drive%\%username%\%pics%"
echo %time%>>backup.log
xcopy "%userprofile%\%pics%" "%drive%\%username%\%pics%" /E /I>>Backup.log
echo 40%%
cls
echo ========================================"%mus%"=========================================>>backup.log
echo "%mus%" 
echo Source:"%userprofile%\%mus%" 
echo Destination:"%drive%\%username%\%mus%" 
echo %time%>>backup.log
xcopy "%userprofile%\%mus%" "%drive%\%username%\%mus%" /E /I>>Backup.log
echo 60%%
cls
echo ========================================"%vids%"========================================>>backup.log
echo %vids% 
echo Source:"%userprofile%\%vids%" 
echo Destination:"%drive%\%username%\%vids%"
echo %time%>>backup.log
xcopy "%userprofile%\%vids%" "%drive%\%username%\%vids%" /E /I>>Backup.log
echo 80%%
cls
echo ========================================"%down%"========================================>>backup.log
echo "%down%" 
echo Source:"%userprofile%\%down%" 
echo Destination:"%drive%\%username%\%down%"
echo %time%>>backup.log
xcopy "%userprofile%\%down%" "%drive%\%username%\%down%" /E /I>>Backup.log
echo end>>backup.log
echo %username% %date% %time%>>backup.log
echo 100%%
cls
echo backup Compleet

copy "backup.log" "%drive%\%username%"
del "backup.log"
pushd "%drive%\%username%"
echo close backup.log to continue with backup script
"backup.log"
echo press any key to retun to the main menu
pause>nul
goto :home

:HD
echo finds and repairs bad sectors
echo typ in harddisk letter (C: D: E:)
set HD=
set /p HD=Hard Disk:
chkdsk %HD% /F /R /X
pause
goto :home

答案1

它会非常相似,但您必须更改很多语法。运行man bashman cp获取大量信息。对于此脚本,cp命令会执行大部分繁重工作。它将取代 copy 和 xcopy。Echo 仍然是 echo。您可以使用“>>”运算符写入日志。

Bash 非常简洁,因为您可以定义函数并使用真正的 ifif(...){... } else{ ...}语句if ... goto(如果您真的想要,您仍然可以使用标签)。尝试像学习批处理一样学习 Bash,并享受学习的乐趣。您会很高兴花时间从头开始重写这个脚本,并且您将学到很多新工具。

答案2

我建议你熟悉一下 Python。这是一种非常好且易于学习的语言。不需要编译或类似的东西。但是,它有库,可以让你做任何你想做的事情。

因此,您可能首先希望用文本替代当前脚本。然后,稍后您可以为其添加 GUI。这很容易。当然,Bash 也很有用,但功能有限。如果您只打算学习一种新语言,我绝对推荐 Python。但当然,同时学习 Bash 和 Python 也没什么错 :)

答案3

我认为大多数人会建议“rsync”,它基本上是与 Microsoft RoboCopy 最接近的东西:

https://superuser.com/questions/29232/linux-equivalent-to-robocopy

在我看来,该批处理文件基本上有点过度了。

相关内容