I currently have to windows batch files the first takes all files in a directory ending .bak and adds them to a rar file with todays date in the name
the second one ftp the rar file to another server.
the first ones code is
set path="C:\Program Files\WinRAR\";%path%
set date=%dd-mm-yyyy%
rar a D:\backups\dbs.rar -ri1 -mt2 -ag[dd-mm-yyyy] -m5 D:\backups\*.BAK
del D:\backups\*.BAK
which is called from inside sql server manager when its finished backing up the databases but could easily be run from a scheduled task
the seconds code is
#echo off
echo user Dbusser> ftpuploader.dat
echo ftppassword>> ftpuploader.dat
echo bin>> ftpuploader.dat
echo put %1>> ftpuploader.dat
echo quit>> ftpuploader.dat
ftp -n -s:ftpuploader.dat 127.0.0.1
del ftpuploader.dat
which I manually called from a cmd promt with the name of the rar file as an argument
ftpuploader.bat d:\pathtorar\dbs[21-10-2013].rar
what I want to know is can anyone tell me how to either merge the two files so it automatically uploads the file once its created and finished it or automate the schedule task to that it changes the date bit in the name of the rar file every day
thanks
This requires XP Pro and higher to use Wmic.
It sets the variable with the date, in a reliable manner, and uses that to create the RAR filename and also launch the FTP batch file.
I changed this line if not errorlevel 1 del D:\backups\*.BAK which should protect the *.bak files from being deleted if the RAR archiving fails for any reason.
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "datestamp=%DD%-%MM%-%YYYY%"
set path="C:\Program Files\WinRAR\";%path%
rar a D:\backups\dbs[%datestamp%].rar -ri1 -mt2 -m5 D:\backups\*.BAK
if not errorlevel 1 del D:\backups\*.BAK
ftpuploader.bat D:\backups\dbs[%datestamp%].rar
Few things that I don't like in your script: I have no idea what set date=%dd-mm-yyyy% is doing .Not sure when the *bak files are deleted.If you are trying to upload binary files you need to set also the ftp script in binary mode. Here's one possible way to do this without temp files:
!&cls&echo off&setlocal ENABLEDELAYEDEXPANSION
!goto :end_ftp
!rem :: ftp part
open server
Dbusser
ftppassword
prompt
binary
mput *.BAK
bye
:end_ftp
set path="C:\Program Files\WinRAR\";%path%
rem get _date in format DD-MM-YYYY
Set /A "Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12"
Type Nul >"%TEMP%\~.ddf"
Makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName=Nul /F "%TEMP%\~.ddf" >Nul
Set /P "timestamp=" <"%TEMP%\~.rpt"
For /F "tokens=3-9 delims=: " %%a In ("%timestamp%") Do (
Set /A "year=%%g,month=%%b,day=1%%c-100"
)
Del /Q "%TEMP%\~.*"
Echo year=%year% month=%month% day=%day%
set "_date=%day%-%month%-%year%"
rem date is ready to use
rar a D:\backups\dbs.rar -ri1 -mt2 -ag[%_date%] -m5 D:\backups\%_date%\*.BAK
pushd D:\backups\%_date%\*.BAK
ftp -s:%~dpsnx0
popd
del D:\backups\%_date%\*.BAK /Q /F
RD /S /Q D:\backups\%_date%\
The idea is that you are creating a directory with date name (I suppose your date format is dd-mm-yyyy) , then starts the ftp in this directory and using prompt (to surpass the prompting) and then using mput to upload all files with .bak extension.So no parameters are needed for the ftp script.For here are more explanations for self-contained ftp scripts.
Related
I am trying to create a batch script to take a directory within Windows, and compress it with 7-zip using a batch script, but limit the maximum file size of each compression part to max 4GB each, limiting the total amount of revision compression sets to 7 (for a weekly backup, Monday to Sunday). The oldest will be removed on the next backup job.
I tried playing with the REM CHECK FOR REVISIONS code, but am not knowledgeable enough to understand how to proceed with this.
#ECHO OFF
REM Script created for AVIMark Backup
REM Install 7-zip and make sure to create an environment variable to allow for shortcuts
REM https://www.7-zip.org/download.html
REM set PATH=%PATH%;C:\Program Files\7-Zip\
REM echo %PATH%
REM 7z
REM 7ZIP ENVIRONMENT VARIABLE PATH
set PATH=%PATH%;C:\Program Files\7-Zip\
7z
cls
REM SET SOURCE, DESTINATION AND REVISION AMOUNT VARIABLES
SET source="C:\AVImark"
SET destination="C:\AVImarkBackup"
SET revisions=7
REM Change Directory to the source data folder
cd "%source%"
REM Run the command to compress the directory, gather the date stamp and insert compressed file into destination folder
7z a -r -v4g "%destination%\%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4% Backup".7z
REM CHECK FOR REVISIONS
cd "%destination%"
for /f "skip=%revisions% eol=: delims=" %%F in ('dir /b /o-d /a-d *.7z') do #del "%%F"
EXPECTED RESULTS
I'd like to have every backup set (in its date format) to be limited to 7 revisions in total.
ie: https://imgur.com/a/Q50n0bD
ACTUAL RESULTS
There is no revision check and cleanup happening on the oldest job, it keeps adding more sets.
#echo off
setlocal
rem Script created for AVIMark Backup
rem Install 7-zip and make sure to create an environment variable to allow for shortcuts
rem https://www.7-zip.org/download.html
rem set PATH=%PATH%;C:\Program Files\7-Zip\
rem echo %PATH%
rem 7z
rem 7ZIP ENVIRONMENT VARIABLE PATH
set "PATH=%PATH%;C:\Program Files\7-Zip"
rem SET SOURCE, DESTINATION AND REVISION AMOUNT VARIABLES
set "source=C:\AVImark"
set "destination=C:\AVImarkBackup"
set "revisions=7"
set "datestamp=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%"
rem Change Directory to the source data folder
pushd "%source%" && (
rem Run the command to compress the directory, gather the date stamp and insert compressed file into destination folder
7z a -r -v4g "%destination%\%datestamp% Backup.7z"
popd
)
rem CHECK FOR REVISIONS
pushd "%destination%" && (
rem Option 1
rem Delete by last modified filedate
forfiles /d -7 /m "*Backup.7z.*" /c "cmd /c echo del #path"
rem Or use:
rem Option 2
rem Delete by logged date. Requires datestamp yyyy-MM-dd for correct sorting
if exist Backup.log (
findstr /c:"%datestamp%" Backup.log || >> Backup.log echo %datestamp%
) else (
> Backup.log echo %datestamp%
)
for /f "skip=%revisions% delims=" %%A in ('type Backup.log ^| sort /r') do (
echo del "%%~A Backup.7z.*"
)
popd
)
Changed datestamp to yyyy-MM-dd instead of dd.MM.yyyy.
The former datestamp is better for sorting.
If you want a datestamp independant of locale, search for
wmic os get localdatetime commands on this site.
Use of pushd and popd instead of cd.
The use of && is to run following command if the last command was successful.
|| is to run following command if the last command failed.
2 options are offered for use to delete the revisions:
The 1st is the use of forfiles.
Currently as set, it will delete revisions older
than 7 days from the current date.
This might be suitable, unless no backups have been
done for 7 days which may result in no backups.
The 2nd is the use of Backup.log.
The date is appended to the log file and then the
for loop reads the log file with type, and sort
reverses the sort to make the oldest 1st and latest
last. The skip avoids the processing of the 1st
7 revisions. The rest will be used to delete the
archives by the file pattern.
Remove the code of the option not wanted.
del commands are echoed for testing. If satisfied,
remove the echoes to make the deletion actually work.
I've been using the following batch file to backup a certain directory on a Windows Server machine for a long time. The batch file is run by a Windows Scheduled Task every day at a certain time and it zips a specific folder to a backup location after deleting the oldest .7z file (7z = www.7-zip.org).
This way I only ever have the number of .7z files specified in BackupNr in my backup folder and I don't have to manually delete the oldest .7z file.
The problem is the "Set BackupNr=1" line. Using BackupNr=1 I always have TWO .7z archives in my backup folder after the batch file has run.
I can't figure out what I need to change so that only ONE archive is kept whenever the batch file runs. How can I fix this?
#echo off
:: The name of the backup file that will be created. It will use the current date as the file name.
#For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do #(
Set FileName=%%A%%B%%C%%D
)
:: The name of the folders where all the backup .zip and report files will be stored.
Set ArchiveFolder=D:\Backup\Manual\Archives
Set ReportFolder=D:\Backup\Manual\Reports
:: The name of the folder that will be backed up, i.e. the AppData folder.
Set FolderToBackup=C:\AppData
:: The number of .zip files and .txt reports to keep. Older archives will be deleted according to their age. This ensures we only keep the most recent X number of backups.
Set BackupNr=1
:: Delete oldest backup archives so we only keep the number of archives specified in "skip=".
echo.>> DeletedBackups.txt
date /t >> DeletedBackups.txt
echo.>> DeletedBackups.txt
echo These older backups were deleted:>> DeletedBackups.txt
for /F "skip=%BackupNr% delims=" %%a in ('dir /b /o-d %ArchiveFolder%\*') do (
echo %ArchiveFolder%\%%a%1>> DeletedBackups.txt
del /f /q %ArchiveFolder%\%%a%1
)
echo.>> DeletedBackups.txt
echo These older reports were deleted:>> DeletedBackups.txt
for /F "skip=%BackupNr% delims=" %%a in ('dir /b /o-d %ReportFolder%\*') do (
echo %ReportFolder%\%%a%1>> DeletedBackups.txt
del /f /q %ReportFolder%\%%a%1
)
echo Starting the backup: %DATE% %TIME% >> %ReportFolder%\%FileName%.txt
:: Adds all files to 7z archive using BCJ2 converter, LZMA with 8 MB dictionary for main output stream (s0), and LZMA with 512 KB dictionary for s1 and s2 output streams of BCJ2.
C:\PROGRA~1\7-Zip\7z.exe a -t7z %ArchiveFolder%\%FileName%.7z %FolderToBackup%\* -m0=BCJ2 -m1=LZMA:d23 -m2=LZMA:d19 -m3=LZMA:d19 -mb0:1 -mb0s1:2 -mb0s2:3 >> %ReportFolder%\%FileName%.txt
echo Backup finished at: %DATE% %TIME% >> %ReportFolder%\%FileName%.txt
:: Write the backup Start and End times to the BackupInfo.csv file.
gawk -f BackupRecord.awk %ReportFolder%\%FileName%.txt >> %ReportFolder%\BackupReport.csv
:: Write the file size of the backup .zip file that was just created to the log file.
set size=0
call :filesize %ArchiveFolder%\%FileName%.7z
echo Backup archive file size: %size% bytes >> %ReportFolder%\%FileName%.txt
exit
:: set filesize of 1st argument in %size% variable, and return
:filesize
set size=%~z1
exit /b 0
Thanks.
The /b switch in the dir command uses no header nor summary. result is a plain list of archives, in this case orderer descending by creation time, the most recent first.
The problem is the skip=%BackupNr% in
for /F "skip=%BackupNr% delims=" %%a in ('dir /b /o-d %ReportFolder%\*') do (
echo %ReportFolder%\%%a%1>> DeletedBackups.txt
del /f /q %ReportFolder%\%%a%1
)
as skip the first line, so the delete command erases all archives EXCEPT the most recent.
then you execute the backup, so a new file is added to the directory. Result, two files.
If you want only the last one, you should first perform the backup and after the deletion, or remove the skip=%BackupNr% from the for command.
So I'm trying to copy a backup of the data from my app. I wrote the batch script below to do this, but the script takes forever to run.
I start the batch script at 1am, and it is still running at 8:30am. This seems weird to me because when I copy the backup of my app manually in Windows File Explorer, it copies in 7-15 minutes depending on network traffic.
I REM the %backupcmd% "C:\Program Files\App\App Server\Data\Backups" "%drive%\" line. That was the original line of batch script I used to backup the data, and it worked efficiently up till a month ago.
So I tried the xcopy command with /d, so it would only copy source files that have been changed on or after that date (the current date), and the backups I'm copying are made at 12:01am every night and the copy backup script starts at 1am.
Any advice as to how to speed up my xcopy would be appreciated. If you think I should use powershell for this task too, I'm open to that option as well.
#echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set yyyy=%ldt:~0,4%
set mm=%ldt:~4,2%
set dd=%ldt:~6,2%
:: variables
set drive=Z:\RootSoft\App\Data Backups
set backupcmd=xcopy /s /c /d /e /h /i /r /y /f /z
echo ### Backing up Backup...
REM %backupcmd% "C:\Program Files\App\App Server\Data\Backups" "%drive%\"
xcopy "C:\Program Files\App\App Server\Data\Backups" "Z:\RootSoft\App\Data Backups" /D:%mm%-%dd%-%yyyy% /s /c /e /h /i /r /y /f /z
:: use below syntax to backup other directories...
:: %backupcmd% "...source directory..." "%drive%\...destination dir..."
echo Backup Complete!
echo %errorlevel%
pause
You could try with ROBOCOPY and /MT switch which could accelerate the copy.
Also you can make some test by measuring the during process with TimeThis that can be found here (no need to be installed, just extract the exe with 7z in the current batch file folder)
netsh interface tcp show global
netsh int tcp set heuristics disabled
netsh int tcp set global autotuninglevel=disabled
netsh int ip set global taskoffload=disabled
I have multiple folders that each contain a config folder. In the config folder there is a .cfg file and a backup folder. I would like to copy the .cfg file to the backup folder with a date extension.
Here is my folder structure:
\Folder1\config
folder1.cfg
\backup
\Folder2\config
folder2.cfg
\backup
\Folder3\config
folder3.cfg
\backup
Each of the above folders has a config folder. In each config folder there is a .cfg file and a backup folder. How can I step through each Folder*\config directory and copy the folder*.cfg file to the backup folder and rename it to folder3.cfg.yyyymmdd? I actually have about 40 folders that I would need to this for on a Windows server. Any help would be greatly appreciated.
REM
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
for /f "delims=." %%a in ('wmic OS Get localdatetime ^| find "."') do set "wmicdt=%%a"
SET "wmicdt=%wmicdt:~0,8%"
FOR /f "delims=" %%a IN ('dir /b /ad "%sourcedir%" ' ) DO (
IF EXIST "%sourcedir%\%%a\config\%%a.cfg" (
MD "%sourcedir%\%%a\config\backup" 2>NUL
COPY /b "%sourcedir%\%%a\config\%%a.cfg" "%sourcedir%\%%a\config\backup\%%a.cfg.%wmicdt%" >nul
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit.
The SET "wmicdt=%wmicdt:~0,8%" line selects the first 8 characters of the date/time string in wmicdt, which is yyyymmdd. If you want the time as well, simply omit this line.
Here is your answer.
#echo off
:loop
for /l %%g in (1,1,3) do (
copy "Folders\Folder%%g\config\Folder%%g.cfg" "Folders\Folder%%%g\config\Backup\"
rename "Folders\Folder%%g\config\Backup\Folder%%g.cfg" "Folder %date%.cfg"
timeout>nul 1
)
:end
cls
echo.
echo ========
echo = Done =
echo ========
echo.
pause>nul
I was trying to make a file with modification date as name but i had some problems with time (22-03-2012 12:22).
This 12 ==>:<== 22 is trouble me. I don't know how to get rid of this. So instead you have current date as a file name.
Sorry for my English.
Here is file with that batch and folders, so you can see how it works
I have to move my latest files alone into a folder named 'archive' using cmd .how should i do it?
for ex: i have files which are created in may,june,july,aug and sep in my_folder.
now i need to move all the files which are created in may,june,july and aug into my_archive and sep files alone have to stay in my_folder.
i used move c:\my_folder*.* c:\my_archive. but this moves all the files. what should be the correct script for doing this?
second thing is , i am thinking of using task scheduler to run the batch file which has this move script? will this help?
Thanks,
ResKing
Assuming that your date format is MM/dd/yyyy this might work:
#echo off
set month=%date:~0,2%
set year=%date:~-4%
mkdir temp_folder
rem move this month's files out of the way
forfiles /d +%month%/01/%year% /c "%comspec% /c move \"#path\" temp_folder"
rem move all other files to my_archive
forfiles /d -%month%/01/%year% /c "%comspec% /c move \"#path\" my_archive"
rem restore this month's files
move temp_folder\*.* .
rmdir temp_folder
This batch will cycle through all old files (not from current month) and move them one by one to my_archive directory.
I'm creating a new file just in case there are no new files in this directory (I'm using a new file as a break rule for the loop). This file is deleted in the end of the batch file.
The dir parameters are to show only files and to sort them by date. you can additionally specify the timestamp you prefer:
/T:C - Creation
/T:A - Last Access
/T:W - Last Written
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET SRC_PATH=c:\my_folder
SET DST_PATH=c:\my_archive
SET MONTH=%date:~4,2%
SET YEAR=%date:~-4%
echo blah > !SRC_PATH!\to_delete.txt
for /f "skip=4 tokens=1,5 delims= " %%g in ('dir /A:-D /O:D !SRC_PATH!') do (
set date=%%g
set m=!date:~0,2!
set y=!date:~-4!
if !YEAR! gtr !y! (move !SRC_PATH!\%%h !DST_PATH!\%%h) else (
if !MONTH! gtr !m! (move !SRC_PATH!\%%h !DST_PATH!\%%h) else goto :end
) )
:end
del !SRC_PATH!\to_delete.txt