Following document help to copy latest files from local drive and
paste on the network drive.
this document also delete files
older than 10 days from the network drive where we are copying the backup
files.
to copy backup files we will
create following files on the c:\users\sarzaidi\documents\ path .
we would also need to create a
folder named log under c:\users\sarzaidi\documents\
1. backup.bat.
this file will copy backup from
local drive to mapped network drive.
drive will be mapped at
runtime.
2. sendemail.bat
this file will use powershell
and emailcommand.ps1 file to send email .
3. emailcommand.ps1
this file contains command to
send email.
in this file we can specify
sender ,receiver ,email subject and body we can also use this file to attach
file to email.
4.main.bat
this file calls backup.bat and
sendemail.bat to perform backup and send email.
we can execute this file from
command prompt or we can schedule this file to execute the task whenever we
need.
1. backup.bat
set
backuplog=backup_db_%date:~10%%date:~4,2%%date:~7,2%.log
echo set
batchscriptdir="c:\program files\postgresql\backups\"
echo set
backupsourcedir="c:\program files\postgresql\backups\"
echo set
backupdestinationdir=\\ccps-sql-01.ad.ccps.net\ccps_backups\db_backups\kp_dev
pg_dump -u postgres
-p 5432 -f c -b -v -d eor4 > "c:\program
files\postgresql\backups"\%backupname%
net use x:
\\ccps-sql-01.ad.ccps.net\ccps_backups\db_backups\kp_dev
x:
echo " xcopy /y /f
/i /c: %backupsourcedir%*.* x:\"
echo backup started from
local:%backupsourcedir% to azure . >> %batchscriptdir%logs\%backuplog%
xcopy /y /f /i
/d:%date:~4,2%-%date:~7,2%-%date:~10% %backupsourcedir%*.* x: >>
%batchscriptdir%logs\%backuplog%
echo backup completed at
\\ccps-sql-01.ad.ccps.net\ccps_backups\db_backups\kp_dev >>
%batchscriptdir%logs\%backuplog%
x:
echo delete older than 10 day
started. >> %batchscriptdir%logs\%backuplog%
forfiles /m *.bak /d -10 /c
"cmd /c echo @path "
echo delete older than 10 day
completed >> %batchscriptdir%logs\%backuplog%
c:
net use x: /delete /y
echo done
2. sendemail.bat
set
powershellscriptpath=%batchscriptdir%emailcommand.ps1
powershell -noprofile
-executionpolicy bypass -command "& '%powershellscriptpath%'";
3. emailcommand.ps1
$from_ =
"sarzaidi@mydomain.com"
$to_ =
"sarzaidi@mydomain.com","arzaidi@lmkr.com"
$subject_ = "doa dev 168
backup"
$body_ = "backup completed
logs at avaliable at \\infoportal-dev\logs "
$smtpserver_ =
"outlook.mydomain.com"
send-mailmessage -from $from_
-to $to_ -subject $subject_ -body $body_ –smtpserver $smtpserver_
4.main.bat
set batchscriptdir="c:\program
files\postgresql\backups\"
set
logdir=eor_backup_%date:~10%%date:~4,2%%date:~7,2%
set
backuplog=backup_%date:~10%%date:~4,2%%date:~7,2%.log
set
emaillog=sendemail_%date:~10%%date:~4,2%%date:~7,2%.log
set
backupsourcedir="c:\program files\postgresql\backups\"
set
backupdestinationdir=\\ccps-sql-01.ad.ccps.net\ccps_backups\db_backups\kp_dev
set
backupname=vo8_%date:~10%%date:~4,2%%date:~7,2%.backup
c:
cd %batchscriptdir%logs
md %logdir%
move /y *.log %logdir%
c:
cd %batchscriptdir%
echo backup.bat started.
>> %batchscriptdir%logs\%backuplog%
call backup.bat >>
%batchscriptdir%logs\%backuplog%
echo backup.bat ended. >>
%batchscriptdir%logs\%backuplog%
c:
cd %batchscriptdir%
echo sendemail started.
>> %batchscriptdir%logs\%emaillog%
call sendemail.bat >>
%batchscriptdir%logs\%emaillog%
echo sendemail ended. >>
%batchscriptdir%logs\%emaillog%
rem cd %batchscriptdir%logs
rem md
unittestlogs_%date:~10%%date:~4,2%%date:~7,2%
rem mv
%batchscriptdir%logs\*.log unittestlogs_%date:~10%%date:~4,2%%date:~7,2%
forfiles /p
"%batchscriptdir%logs" /s /d -6 /c "cmd /c if @isdir == true rd
/s /q @path"
cd %batchscriptdir%
echo done