Salam to All,
Following are few backup batch files that I have used at my office to perform backup operation using scheduled backup tasks. I am posting them just for reference so they can be used as archiving or possibly useful for others too having same task.
+++++++++++++++++++++++++++++++++++++++
Few days back, a new oracle server was added in our IT dept. It is an oracle database server which serves many users request in our company. There was an annoying manual backup task that we have to perform on it on daily basis , the :@ task was to run 2 shortcut which creates backup dump files in E: drive, and then we have to manually rename the 2 dump files according to current date and then move them in other date-wise folder.
Quite annoying , isn’t it ? I mean still do we have to perform simple tasks like taking backup manually where every task is being done Automatically ?
Stupid enough !
It was quite easy to do this task automatically in LINUX environment, but as currently i am working in MS (Microshit or Microsoft environment) I manage to done it with built-in DOS commands. Following is the complete dos batch file or call it the Backup script , I just added in Schedule task, Now it runs daily and saves my time.
@echo off Echo Oracle Backup Organizer Script. Echo Developed by Syed Jahanzaib / aacable@hotmail.com Echo Make sure you have the backkup folder where backup is copied on daily basis by other script. Echo Setting current date set dt=%date:~-4,4%%date:~-10,2%%date:~-7,2% Echo Map File Server backup folder in local computer using admin account. net use B: \\FILES_SERVER\D$\home\cic /USER:zaib-admin@localdomain admin-passwd Echo Copying already created oracle Dump files (which you can create by using backup file scheduled to run daily) Echo into remote file server with current date for backup - FILES_SERVER copy e:\backup\Cic_final.Dmp B:\cic-backup.%dt%.dmp copy e:\backup\nae_final.dmp B:\nae-backup.%dt%.dmp Echo Renaming dump files to current date for the date wise backup ren e:\backup\Cic_final.Dmp cic-backup.%dt%.dmp ren e:\backup\nae_final.dmp nae-backup.%dt%.dmp echo. echo All Backup done. Allah Hafiz
NTBACKUP with EMAIL the LOGS and START/END Time Report
@echo off rem ####################################################### rem Setting various Descriptions via environment variables rem ####################################################### set dt=%date:~-4,4%%date:~-10,2%%date:~-7,2% set logpath="C:\Documents and Settings\administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data" rem you must create .bks file using ntbackup GUI , os it knows which data to backup below set backup-data=C:\backup\Users Autobackup of Users DATA.bks set description=Users Autobackup set jobname=Users Autobackup - DATA Backup TASK set attachment=C:\backup\fileserver-ntbackup-%dt%.log set mail-subject=Users Autobackup - DATA Backup TASKy set mail-body=Users Autobackup - DATA Backup TASK set mail-to="aacable@hotmail.com","email2@aacable.xyz" rem set totaletime=%tot% rem ###################################### rem Starting Backup in IBM LTO3 Tape Drive rem ###################################### start /wait C:\WINDOWS\system32\ntbackup.exe backup "@%backup-data%" /n "%jobname%" /d "%description%" /j "%jobname%" /l:s /p "LTO Ultrium" /UM /v:no /r:yes /rs:no /hc:on /m normal rem ### Copy current data last LOG to C:\backup rem cd %logpath% rem FOR /F %%i IN ('DIR /B /O:-D *.log') DO ( rem copy "%%i" %attachment% rem cd c:\backup forfiles -p %logpath% -s -m *.log -d 0 -c "cmd /c copy @file %attachment%" rem ######################### rem STart END Time Algorithm rem ######################### setlocal enableextensions enabledelayedexpansion set starttime=%time% ping -n 2 127.0.0.1 >nul: 2>nul: set endtime=%time% set total="echo Total = %tot%" set /a hrs=%endtime:~0,2% set /a hrs=%hrs%-%starttime:~0,2% set /a mins=%endtime:~3,2% set /a mins=%mins%-%starttime:~3,2% set /a secs=%endtime:~6,2% set /a secs=%secs%-%starttime:~6,2% if %secs% lss 0 ( set /a secs=!secs!+60 set /a mins=!mins!-1 ) if %mins% lss 0 ( set /a mins=!mins!+60 set /a hrs=!hrs!-1 ) if %hrs% lss 0 ( set /a hrs=!hrs!+24 ) set /a tot=%secs%+%mins%*60+%hrs%*3600 echo End = %endtime% echo Start = %starttime% echo Hours = %hrs% echo Minutes = %mins% echo Seconds = %secs% echo Total = %tot% rem ########## rem Email LOGS rem ########## rem goto :blat) rem :blat c:\blat\blat.exe -to %mail-to% -i MY_SERVER_NAME -s "%mail-subject%" -body "%mail-body%|Backup Report:|Start = %starttime%|End = %endtime%|Hours = %hrs%|Minutes = %mins%|Total = %tot%||Automated Backup & Email Logs Script Created by SYED_JAHANZAIB." -attach %attachment% endlocal rem ## THE END
To delete files olde then X days using FORFILES
forfiles /p “C:\test” /s /m *.* /c “cmd /c del @path” /d -15
Change /d -15 to match your requirements. you can change the del command to any other like to show the files, use echo. FORFILES is builtin command with Windwos 2003 /7. You can download it from
http://www.ipass.net/davesisk/forfiles.zip
Delete Files Over 15 Days Old using ROBOCOPY
set _robodel=%TEMP%\~robodel
MD %_robodel%
ROBOCOPY “C:\source_folder” %_robodel% /move /minage:15
del %_robodel% /q
Batch File to create SQL Backup
First create .sql file that can be called from batch file to create any specific DB backup.
for example c:\zaibdb.sql
BACKUP DATABASE [Promo] TO DISK = N'D:\DataBaseBackup\zaibdb.Bak' WITH NOFORMAT, INIT, NAME = N'Promo-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO
now create a batch file which will call the above .sql file to start backup.
for example c:\start_backup.bat
@echo off echo Creating FULL backup in D:\zaibdb.back echo %Date% >> D:\DataBaseBackup\zaibdb.txt sqlcmd -S .\SQLEXPRESS -i c:\zaibdb.sql > D:\DataBaseBackup\zaibdb.txt echo. echo Done rem ping 127.0.0.1 -n 10 > null echo . >> D:\DataBaseBackup\zaibdb.txt echo This backup set was last created at %Date% >> D:\DataBaseBackup\zaibdb.txt echo.
Another Backup script that map remote server folder locally
and then copy the required files and then delete the files older then 20 days
@echo off rem Syed Jahanzaib / ??? (Pvt) Ltd. IS Dept. rem This file will copy the today's SAP exports and cofiles only to files_server\export folder, rem which is actually mapped locally as B: drive. rem # rem # rem # rem # Mapping files_server Folder via agpis Credentials net use B: \\files_server\ rem # Copying Last Date Export files to \\file-server\B:\export folder forfiles -p "J:\required_folder" -s -m *.* -d 0 -c "cmd /c copy @path B:\" rem # Copying Last Date cofiles data to \\file-server\B:\cofiles folder forfiles -p "J:\required_folder" -s -m *.* -d 0 -c "cmd /c copy @path B:\" rem # Deleting export older then 20 Days . . . forfiles -p "J:\required_folder" -s -m *.* -d -20 -c "cmd /c del @path" rem # Deleting cofiles Logs older then 20 Days . . . forfiles -p "J:\required_folder" -s -m *.* -d -20 -c "cmd /c del @path"
.
Daily Backup of Required Folder using ROBOCOPY
Robocopy method which will copy only new file by matching source and destination folder/file timstamps. Its much better as it displays results in command prompt with percentage and all the necessary info any admin want. its my favorite. You can downlod Robocopy by downloading Microsoft Resource Kit from following link.
http://www.microsoft.com/en-us/download/details.aspx?id=17657
@echo off Robocopy /E /LOG+:h:\Softwares\Servers_Related\mail-backup-log.txt B:\archive\. T:\backup\archive Robocopy /E /LOG+:h:\Softwares\Servers_Related\mail-backup-log.txt T:\mailserver\c$\mt\. T:\backup\mt Robocopy /E /LOG+:h:\Softwares\Servers_Related\mail-backup-log.txt T:\mailserver\mail\. T:\backup\mail
The above commands description is as follows.
/E = Copy sub-directories, including Empty ones
/LOG+ = Output status to LOG file (append to existing log, so previous entries along with new one should be saved together, if you don’t use + , it will overwrite existing log-file, means delete old entries and overwrite new one)).
XCOPY Method
Following file will copy all the data from the source folder to target folder using windows builtin copy tool XCOPY.
@echo off echo * * * >> C:\backup-log.txt echo Mail Backup Starts at %date% - %time% >> C:\backup-log.txt C:\windows\system32\xcopy.exe B:\source-folder\*.* T:\targetfolder /S /D /C /Y echo Mail Backup Ends at %date% - %time% >> C:\backup-log.txt echo * * * >> C:\backup-log.txt
The above commands description is as follows.
/S = Copies directories and sub-directories except empty ones.
/D = If no date is given, copies only those files whose source time is newer than the destination time.
/C = Continues copying even if errors occur.
/Y = Overwrite existing files if any without prompting, its necessary when you are running xcopy via batch / script file.
Regard’s
Syed Jahanzaib
Filed under: Microsoft Related
