Batch processing to achieve the perfect FTP remote backup data _dos/bat

Source: Internet
Author: User
Tags create directory ftp file parent directory temporary file storage

After another effort, and finally completed their own more satisfactory version, we criticize more ...

Batch processing function:

Enter a few parameters, let batch processing on behalf of you to complete the cumbersome work bar! Remote FTP files to the local computer, you can drag into the boot or task plan, scheduled to download remote designated data, I hope we have more testing, more suggestions, more perfect, thank you! On the code!

An example is provided:

I need to download the files from the server 202.33.22.102 to the local computer every morning, then you need to specify the following parameters

One, remote ip,is NOT NULL

Second, the FTP user name, is not NULL

Third, remote password, is not null

Four, download which directory under the file, if it is the root of the input point, that is, "..." (no double quotes)

Five, subdirectories, you may want to download a folder under a number of directories? If so, please separate them with commas, if there is nothing to set (including spaces), folder name please Mes do not have spaces, so as not to unnecessary trouble, when you specify a subdirectory, the program will download only the files in the subdirectory, and the next folder does not do any action.

The local directory, please specify an existing directory, including "\", the program will automatically create the same folder on the server

Seven, the log record file directory, also exists.

OK, you can now double-click this batch to perform the operation!

@echo off Echo ********====================================================== Echo ********======================== ============================== echo ++++++++ Batch function: Remote Data FTP Backup to local echo ++++++++ Date:%date:~0,4%-%date:~5,2%-%date:~8, 2% echo ++++++++ author:zc_0101 echo ++++++++ email:zc_0101@163.com echo ++++++++ qq:275374095 echo +++ +++++ Mark:echo ++++++++ Please define the parameters in strict accordance with the parameters, input parameters echo ++++++++ try to be normal, do not bring special characters including spaces Echo ++++++++ if the file already exists, skip, do not support the short point of continuation E
Cho ++++++++ does not detect the integrity of backup data, users can modify the Echo ++++++++ PS: Reproduced please indicate the source (^_^), thank you! Echo ********====================================================== Echo ********================================ ====================== pause rem Remote IP (make sure to open Server_u) set remote_ip=103.111.124.123 rem telnet user name (if there are batch symbols, escape) set Remo Te_user=test Rem telnet Password (if there are batch symbols, escape) Set remote_pwd=fkhvbweos^! sd4al%% Rem Remote Operation directory, if the root directory, please fill out "..." (unique quotes), otherwise directly fill in the directory name, such as Databaseback set remote_path=databaseback Rem remote directory list to download (Note: Download only the files in these directories, do not download subdirectories under the file), please use the funny, if there is no subdirectory (make sure that there are files available to download in the current directory), please do not fill in any (including spaces); another: folder Please do not have space Set Remote_directory=db_hxxl,db_oa,master,model,msdb Rem Local backup directory, please fill in an existing directory (this program will automatically create and remote server in this directory the same subdirectory, do not bring "\" slash) set Local_path=d:\dbback Rem operation log and temporary file storage path (be sure to bring the "\" slash) set

Log_path=d:\ Rem gets the current date as the log file name, in this case the log is stored directory plus date set today= "%log_path%%date:~0,4%-%date:~5,2%-%date:~8,2%-%time:~0,2%%time:~3,2 %%time:~6,2%.txt "Echo is basically testing the parameters you entered ... echo is detecting input parameters ... >%today% if"%remote_ip% "EQU" "(Echo remote IP cannot be empty!)!!!!!!!! Please return to REMOTE_IP assignment echo remote IP for NULL, program exit. >>%today% pause exit) If "%remote_user%" EQU "" (Echo telnet name cannot be empty!) Please return to the Remote_user assignment echo remote login with NULL, program exit. >>%today% pause exit) If "%remote_pwd%" EQU "" (Echo remote login password cannot be empty!) Please return the REMOTE_PWD assignment echo remote login password to null and the program exits. >>%today% pause exit) If "%remote_path%" EQU "" (Echo Remote operation directory cannot be empty!)
  Please return to Remote_path and if you are working on the root directory, enter ^ (dot ^), that is, "..." The Echo remote operation directory is empty and the program exits. >>%today% pause exit) If "%remote_directory%" EQU "" (Echo remote subdirectory is wrong!) Are you assigning a blank to the remote_directory? If there are no subdirectories pleaseDo not assign values, including spaces. The Echo remote subdirectory is incorrect and the program exits. >>%today% pause exit) if "%local_path%\" EQU "" (Echo local operations directory cannot be empty!) Please return to the Local_path assignment echo local operations directory is empty and the program exits. >>%today% pause exit) If "%log_path%" EQU "" (Echo local log storage directory cannot be empty!) Please return to the Log_path assignment echo local log storage directory as empty and the program exits. >>%today% pause exit) echo parameter Basic test passed, ready to perform database backup echo parameter basic test passed, database backup now begins >>%today% Echo today is%date%, time is:%time:~ 0,2%:%time:~3,2%:%time:~6,2%, performing a database backup ... echo today is%date%, time is:%time:~0,2%:%time:~3,2%:%time:~6,2%, performing Database backup ..... ......... >>%today% Echo is creating an FTP file that generates a list of files, please wait ... echo is creating an FTP file that generates a list of files, please wait ... >>%today% ping-n 3 127.1>nul Echo is Write remote login username to ftp file ... >>%today% echo%remote_user%>%log_path%command.txt Echo is writing the remote login password to the ftp file ... the >>% of ..... today% Echo%remote_pwd%>>%log_path%command.txt Echo is writing the download way to the ftp file ... >>%today% echo type binary>> %log_path%command.txt Echo is determining whether the remote parent directory is a root directory ... >>%today% if "%remote_path%" Neq "..." (If "%remote_path%" "NEQ" (Echo is entering the remote directory%remote_path% the statement to the FTP file ... >>%today% echo CD%remote_path%>>%log_path%command.txt) echo is judging the remote subdirectory ............>> %today% if "%remote_directory%" NEQ "" (Echo is writing the build statement of the subdirectory list to the ftp file ... >>%today% for%%i in (%remote_directory%) Do (Echo is writing the build statement for the subdirectory list%%i to the ftp file ... >>%today% echo MLS%%i%log_path%%%i_temp.txt>>%log_path%comman D.txt) Else (Echo writes the build statement of the file list under the root directory to the FTP file because the subdirectory is not populated ... >>%today% echo MLS-%log_path%filelist_temp.txt> >%log_path%command.txt) echo Close>>%log_path%command.txt Echo Quit>>%log_path%command.txt Echo is working on CO Mmand.txt file to determine if not exist "%log_path%command.txt\" (if Exist "%log_path%command.txt" (Echo%log_path%command.tx
    T-file Creation success echo%log_path%command.txt file creation successful >>%today% else (echo%log_path%command.txt file creation failed, program exits. echo%log_path%command.txt file creation failed, program exits >>%today% pause Echo exit) Else (echo the parameter you entered has a log path named comm
And.txt folders, please remove them manually and re-run the program.  echo the parameter you entered contains a folder named Command.txt in the log path, and the program exits. >>%today% pause exit) echo is performing a list of server directory files to create, please wait ... echo is performing a list of server directory files to create, please wait ... >>%today% ping-n 3 127.1. >nul ftp-i-s:%log_path%command.txt%remote_ip% Echo is checking the list of server directory files created, please wait ... echo is checking the list of server directory files created, please wait while ............> >%today% if "%remote_directory%" Neq "" (For%%i in (%remote_directory%) does (if not exist "%log_path%%%i.txt\" (if Exist "%log_path%\%%i_temp.txt" (mshta "Javascript:var fso=new activexobject" (' Scripting.filesystemobje CT '); OpenTextFile ('%log_path%\temp1.txt ', 2,true). Write (FSO. OpenTextFile ('%log_path%\%%i_temp.txt '). ReadAll (). Replace (/\r\n/g, ' \ n ')); Close () "Mshta" Javascript:var fso=new activexobject (' Scripting.filesystemobje CT '); OpenTextFile ('%log_path%\temp2.txt ', 2,true). Write (FSO. OpenTextFile ('%log_path%\temp1.txt '). ReadAll (). Replace (/\r/g, ')); Close () "Mshta" Javascript:var fso=new activexobject (' Scripting.FileSystemObject '); f So. OpenTextFile ('%log_path%\%%i.txt ', 2,true). Write (FSO. OpenTextFile ('%log_path%\temp2.txt '). ReadAll (). Replace (/\n\n/g, ' \ r \ n ')); "Close ()") if Exist "%log_path%%%i.txt" (Echo%log_path%%%i.txt
        File Creation Successful! echo%log_path%%%i.txt file creation success! >>%today%) Else (echo Sorry,%log_path%%%i.txt file creation failed!)
        Program will exit! echo%log_path%%%i.txt file creation failed! Program termination >>%today% Pause exit) Else (echo the parameter you entered holds the log path%log_path% there is a name%%i.tx
      Folder for T, rerun the program after you manually delete it. echo the parameter you entered is stored in the log path%log_path% there is a folder named%%i.txt and the program exits. >>%today% pause exit)) Else (if not exist "%log_path%filelist.txt\" (if exist%LOG_PA Th%\filelist_temp.txt "(mshta" Javascript:var fso=new activexobject (' Scripting.FileSystemObject '); OpenTextFile ('%log_path%\temp1.txt ', 2,true). Write (FSO. OpenTextFile ('%log_path%\filelist_temp.txt '). ReadAll (). Replace (/\r\n/g, ' \ n ')); Close () "Mshta" Javascript:var fso=new activexobject (' Scripting.filesystEmobject '), FSO. OpenTextFile ('%log_path%\temp2.txt ', 2,true). Write (FSO. OpenTextFile ('%log_path%\temp1.txt '). ReadAll (). Replace (/\r/g, ')); Close () "Mshta" Javascript:var fso=new activexobject (' Scripting.FileSystemObject '); fs O.opentextfile ('%log_path%\filelist.txt ', 2,true). Write (FSO. OpenTextFile ('%log_path%\temp2.txt '). ReadAll (). Replace (/\n\n/g, ' \ r \ n ')); "Close ()") if Exist "%log_path%filelist.txt" (Echo%log_path%filelist.txt
      File Creation Successful! echo%log_path%filelist.txt file creation success!
      >>%today%) Else (echo Sorry,%log_path%filelist.txt file creation failed!) echo%log_path%filelist.txt file creation failed! Program termination >>%today% Pause exit) Else (echo the parameter you entered holds the log path%log_path% there is a folder named FileList.txt, please delete it manually
    , run the program again. echo the parameter you entered is stored in the log path%log_path% there is a folder named FileList.txt and the program exits. >>%today% pause exit) The Echo program starts downloading files, please be patient ... the Echo program starts downloading the file, please be patient ... >>%today% if "%remote_direc" ....... tory% "Neq" (for%%i in (%remote_directory%) does (Echo is creating a directory%local_Path%\%%i ... echo create directory%local_path%\%%i............>>%today% MD%local_path%\%%i echo is downloading%remote_path%\%%i folder backup. , please wait .... Echo is downloading the%remote_path%\%%i folder Backup, please wait ... >>%today% ping-n 3 127.1>nul for/f "delims=.", and so on. tokens=1,2 '%%j in (%log_path%%%i.txt) does (if not exist '%local_path%\%%i\%%j.%%k\ ' (if not exist%local_
          Path%\%%i\%%j.%%k "(Echo is creating a script to download the file%%j.%%k, please wait ... echo is creating a script to download the file%%j.%%k, please wait ... >>%today%..) Echo%remote_user%>%log_path%getfiles.txt Echo%remote_pwd%>>%log_path%getfiles.txt E Cho type Binary>>%log_path%getfiles.txt if "%remote_path%" Neq "" (IF)%remote_path% "Neq". "(Echo CD%remote_path%>>%log_path%getfiles.txt)) echo CD%%i> >%log_path%getfiles.txt echo LCD%local_path%\%%i>>%log_path%getfiles.txt echo mget "%%j.%%k" >>%log_path%getfiles.txt
          echo Close>>%log_path%getfiles.txt Echo quit>>%log_path%getfiles.txt ftp-i-S: %log_path%getfiles.txt%remote_ip% Echo is checking file%local_path%\%%i\%%j.%%k, please wait ... echo is checking files%local_path%\% %i\%%j.%%k, please wait ... >>%today% if exist "%local_path%\%%i\%%j.%%k\" (Echo ———— download%local_path%\%%i\%
            %j.%%k Success! echo ———— download%local_path%\%%i\%%j.%%k success!
            >>%today%) Else (echo--for unknown reason, download%local_path%\%%i\%%j.%%k failed! echo--for unknown reasons, download%local_path%\%%i\%%j.%%k failed! >>%today%) Else (Echo%local_path%\%%i\%%j.%%k already exists, skipping ———— echo%local_pat H%\%%i\%%j.%%k already exists, skipping ———— >>%today%) Else (echo the parameter you entered holds the backup path%local_path%\%%i has a name%%j.%
        %k folders, please remove them manually and re-run the program. echo the parameter you entered is stored in the backup path%local_path%\%%i there is a folder named%%j.%%k, and the program exits. >>%today% pause exit))) Else (echo because there is no server subdirectory specified, the program willThe files in the parent directory of the service are backed up to the local directory you specified ... echo because no server subdirectory is specified, the program backs up files in the server's parent directory to the local directory you specify ... >>%today% for/f "delims=. tokens=1,2 '%%j in (%log_path%filelist.txt) does (if not exist '%local_path%\%%j.%%k\ ' (if not exist%local_pat H%\%%j.%%k "(Echo is creating a script to download the file%%j.%%k, please wait ... echo is creating a script to download the file%%j.%%k, please wait ... >>%today% echo."). %remote_user%>%log_path%getfiles.txt echo%remote_pwd%>>%log_path%getfiles.txt echo type BINARY&G T;>%log_path%getfiles.txt if "%remote_path%" Neq "" (IF)%remote_path% "Neq" ... "
        (Echo CD%remote_path%>>%log_path%getfiles.txt))  echo LCD%local_path%>>%log_path%getfiles.txt echo mget "%%j.%%k" >>%log_path%getfiles.txt Echo Close>>%log_path%getfiles.txt echo quit>>%log_path%getfiles.txt ftp-i-s:%log_path%getfiles. TXT%remote_ip% echo is checking file%local_path%\%%j.%%k, please wait ... echo is checkingFile%local_path%\%%j.%%k, please wait ... >>%today% if exist "%local_path%\%%j.%%k" (Echo ———— download%local_path%\%%
          J.%%k Success! echo ———— download%local_path%\%%j.%%k success!
          >>%today%) Else (echo--for unknown reason, download%local_path%\%%j.%%k failed! echo--for unknown reasons, download%local_path%\%%j.%%k failed! >>%today%) Else (Echo%local_path%\%%j.%%k already exists, skipping ———— echo%local_path%\%%j.%%k already saved
      In, skipping ———— >>%today%)) Else (echo the parameter you entered holds the backup path%local_path%\ there is a folder named%%j.%%k, please remove it manually and re-run the program. echo the parameter you entered is stored in the backup path%local_path%\ there is a folder named%%j.%%k, and the program exits. >>%today% pause exit)) The Echo program is purging temporary files, please wait ... set temp_str=command.txt,temp1.txt,temp2.txt,g EtFiles.txt for%%i in (%temp_str%) does (the Echo program deletes%log_path%%%i files ... >>%today% del%log_path%%%i if exist "%l Og_path%%%i "(Echo%log_path%%%i file deletion failed, program continues to run Echo%log_path%%%i file deletion failed, program continues to run >>%today%) Else (Echo
    %log_path%%%i File Deletion succeededecho%log_path%%%i file deletion succeeded >>%today%) The Echo program is deleting the temporary file list file, please wait ... the echo program is deleting the temporary file list file, please wait ... >>%today% if "". "" %remote_directory% "Neq" (for%%i in (%remote_directory%) do (Del%log_path%%%i.txt,%log_path%%%i_temp.txt i F exist "%log_path%%%i.txt" (Echo%log_path%%%i.txt file deletion failed, program continues to run Echo%log_path%%%i.txt file deletion failed, program continues to run >>% today%) Else (Echo%log_path%%%i.txt file deletion succeeded Echo%log_path%%%i.txt file deletion succeeded >>%today%)) Els E (del%log_path%filelist.txt,%log_path%filelist_temp.txt if Exist "%log_path%filelist.txt" (Echo%log_path%file List.txt file deletion failed, program continues to run Echo%log_path%filelist.txt file deletion failed, program continues to run >>%today%) Else (Echo%log_path%filelist.t XT file deletion succeeded Echo%log_path%filelist.txt file deletion successful >>%today%) echo Congratulations, this backup has been successfully completed!
Please check the%today% log file for details! Echo Congratulations, this backup work has been successfully completed! >>%today% echo data has been backed up into the%local_path%\ directory.
 >>%today% echo Completion time:%time:~0,2%:%time:~3,2%:%time:~6,2%>>%today% pause exit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.