File: D:/bin/webbackup/webbackup - Copy (14).cmd
rem SS, 24/3/06 version for 1and1 server
rem SS, 10/10/08 version for new 1and1 server
rem SS, 25/09/09 improved log to add date/time and dir of FTP folder, also send today's log as email
rem SS, 20/11/09 added backup to office web server (now new BE unlimited connection)
rem SS, 01/12/09 adjusted backup to office web server from 21 to 14 days due to limited space on c drive
rem SS, 04/03/10 added backup of c:\bin, moved ftp 1and1 from first to third position, start time adjusted from 2am to 3am
:: SS, 05/05/12 changed mysql backup to backup each database to a separate .sql file, easier to restore
:: SS, 06/05/12 changed to use 7-Zip instead of WinZip, improved look for log, with '---' and date time on one line,
:: also file dates are nowin ISO format with hyphen separator (e.g. 06052012.zip becomes 2012-05-06.7z)
:: each database folder is listed and start time shown
:: SS, 07/05/12 minor improvements, separate webbak.zip and mysqldump.zip non longer created, last 7 days kept on server
:: SS, 08/05/12 moved the secure data, DNS, etc to the start after noticing it was taking 5 mins to add to zip, even though quite small in size
:: SS, 10/05/12 replaced two of the cmddate's with d:\bin\cmddate\cmddate-old to delete using old style dates
:: SS, 24/05/12 changed backups to delete from ontheworldweb.com from 31 to 65 days ago
:: SS, 26/06/12 now on new FastHosts server, removed 1and1 FTP, changed d:\backups to e:\backups, e: partition used just to hold backups, added data to path D:\MySQL Datafiles\
:: SS, 05/07/12 Windows FTP command not working, i.e. doesn't support passive transfers, changed to use WinSCP
:: SS, 12/07/12 Changed to use secure (FTPS) connection
:: SS, 15/07/12 Speed improvements, max compression done using one command to prevent creation of temp file, now using LZMA2 for all 4 threads of new Core i5. Now keeping 30 days instead of 7, zipping to t: drive (different physical drive to mysql dumps for speed and less stress)
:: SS, 16/03/13 Added a newline after the FTP because it was adding the '---..' to the same line in the log
:: SS, 03/09/13 Added FTPing the back to new backup server
:: SS, 20/04/14 Changed from 65 to 92 days for home server (i.e. keep 3 months, quarter)
:: SS, 22/04/14 Added set tmpbakpath=e:\backups, set to t:\backups to swap e: and t: to check and optimise performance
:: might perform better if OS reads mainly from drive 0 of mirror, writing to drive 1 (where t: drive is, should be faster)
:: SS, 22/04/14 Was no quicker, in fact, it was a little slower if anything, reverted back
:: SS, 22/04/14 Installed latest WinSCP 5.5.3 (was 4.3.8) seemed to improve the speed. FileZilla Client has commandline only to make the connection, not tp transfer the account file (what's the point of that?)
:: SS, 27/04/14 Changed from 7-Zip to FreeArc
:: SS, 30/04/14 Change to SFTP (for WinSCP) to use blowfish (faster encryption)
:: SS, 22/12/14 Change to tmpbakpath due to issues with E: partition on primary drive. Only T: partition now used for backup.
:: SS, 03/09/15 Modified keep past 2 weeks only (was 30 days) due to T partition being resized from 581GB to 300GB (was resized due to errors on drive T, hopefully only on last 281GB of it)
:: SS, 11/09/15 Added timing subroutines
:: SS, 14/09/15 Changed from -m2 to -m1 from FreeArc to see how much quicker or slower it'll be
:: SS, 15/09/15 Changed from -m1 to -mx from FreeArc to see how much quicker or slower it'll be
:: SS, 15/09/15 changed from -mx to -m2 to speed up backup (due to a hard drive issue this morning)
:: SS, 06/02/17 Modified deletion of previous backups from 14 to 30 days, i.e. 30 days now kept
:: SS, 12/08/18 Change to SFTP to Hyperoptic connection at home instead of plusnet (not sure it'll work due to certificate)
:: SS, 11/01/19 Modified to pass a parameter (%1) which is the SS home connection host to use this will be hyperoptic, virginmedia or plusnet
:: SS, 14/01/19 Added %1 to call to sendemail.cmd (because error check was failing), also modified sendemail.cmd to use %1, host name is used in one of the checks
:: SS, 02/06/19 New server for backup, installed new version of WinSCP to fix issue
:: SS, 08/06/19 Modified to bypass first ftp to backup server (no longer used)
d:
cd d:\bin\webbackup
:: (SS,11/9/15) save path of log file needed when timing mysqldump which changes directory
set logfile=%cd%\logtoday.txt
:: now using WinSCP, ftpexe is the path to the app
set ftpexe=d:\bin\winscp\winscp.com
:: (SS,27/4/14) set part to arc.exe (because it wasn't working in Task Scheduler with path)
set arcexe="C:\Program Files (x86)\FreeArc\bin\arc.exe"
:: (SS,15/7/12) added following for one place to change the backup destination, change to e: for time being due to issue with t: drive
:: (SS,22/07/12) changed back to t:, issue with t: drive now resolved (new motherboard, i.e. chassis as Fasthosts referred to)
:: (SS,22/04/14) changed from t:\backups to e:\backups, later back to t:\..
set bakpath=t:\backups
:: (SS,22/04/14) added following to allow swapping of e & t drives to check performance, was e:\backups, set to t:\backups, later back to e:\..
::set tmpbakpath=e:\backups
:: (SS,22/12/14) having issues with our primary drive, causing server to crash. I think the bad sector(s) is/are in the e: failed when FTPing hyperflight.sql to home server
:: until hard drive is replaced decide to use only the t drive to do the on secondary disk
set tmpbakpath=t:\ebackups
call :start_main_time
echo ============================================================>logtoday.txt
echo %date% %time% Start>>logtoday.txt
rem --- delete backup of 30 days ago (only keep past 30 days)
rem cmddate -s -30 del %bakpath%\#.7z>op.bat
:: (SS,27/4/14) added FreeArc
:: (SS,3/9/15) modified from 30 to 14 to keep past 2 weeks only due to T partition being resized from 581GB to 300GB
:: (SS,6/2/17) modified from 14 to 30 days
cmddate -s -30 del %bakpath%\#.arc>op.bat
:: (SS,7/5/12) set name f backup file YYYY-MM-DD.7z
:: (SS,15/7/12) changed from e: to t: drive, different physical drive to mysql dumps for speed and less stress
:: (SS,27/4/14) changed from .7z to .arc
cmddate -s 0 set bakfile=%bakpath%\#.arc>>op.bat
call op.bat
:: goto ftp2
call :start_section_time
echo ------------------------------------------------------------>>logtoday.txt
echo %date% %time% Start secure data to zip>>logtoday.txt
rem --- secure data backup, added 26/4/06, SS
::del d:\backups\securedata.7z
:: wzzip d:\backups\securedata.zip d:\data\*.* -rp -whs -ex -smaxis5oscaris10blahblah#~#~$*![(])blah456
:: (SS,6/5/12) winzip replaced with 7z
:: (SS,17/7/12) switched to LZMA2
::7z a -t7z -mx=9 -m0=LZMA2 -r -pmaxis5oscaris10blahblah#~#~$*![(])blah456 %tmpbakpath%\securedata.7z d:\data\*.*
:: (SS,27/4/14) switched to FreeArc default settings, also added output to logtoday.txt, -i0 disables the progress indicator
%arcexe% a -i0 -r -pmaxis5oscaris10blahblah#~#~$*![(])blah456 %tmpbakpath%\securedata.arc d:\data\*.* >>logtoday.txt
call :end_section_time
echo ------------------------------------------------------------>>logtoday.txt
echo %date% %time% Start mysqldump>>logtoday.txt
echo.>>logtoday.txt
rem --- mysql backup
rem --- use mysql dump command to do dump all the databases
rem (SS,21/3/12) added --routines to make sure functions and procedures are also backed up
rem NB. the --opt options does the following --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset.
rem all tables in a database are locked before the database is added to the dump
rem mysqldump --opt --user=root --password=oscaris10 --all-databases --routines >d:\backups\mysql_backup.sql
call :start_time
:: (SS,5/5/12) Replaced above with following to individually dump each db
:: go through each folder in mysql datafolder, running the mysqldump command for each database to the d:\backups\mysql folder
pushd "D:\MySQL Datafiles\data"
for /d %%f in (*) do (
mysqldump.exe --user=root --password=oscaris10 --routines --databases %%f > "%tmpbakpath%\mysql\%%f.sql"
call :end_time
echo - %%f>>%logfile%
)
popd
:: list sql files in name order
dir %tmpbakpath%\mysql /a-d >>logtoday.txt
:: list sql files in size order
::dir d:\backups\mysql /a-d /o-s >>logtoday.txt
call :end_section_time
echo ------------------------------------------------------------>>logtoday.txt
echo %date% %time% Start adding mysql, web, bin, DNS, securedata to zip>>logtoday.txt
rem delete previous zip
rem del d:\backups\mysql_backup.7z
rem --- zip it up
rem wzzip d:\backups\mysql_backup.zip d:\backups\mysql_backup.sql -whs -ex -smaxis5
rem (SS,3/5/12) replaced above with following, now held in folder
:: wzzip d:\backups\mysql_backup.zip d:\backups\mysql\*.sql -whs -ex -smaxis5
:: (SS,6/5/12) winzip replaced with 7z
:: 7z a -t7z -mx=9 -ms=256m -r -pmaxis5 %bakfile% e:\backups\mysql
:: (SS,15/7/12) replaced above with following to do all with one command (prevents creation of temp file), also switched to LZMA2 to use all 4 threads on new Core i5 (quicker)
:: 7z a -t7z -mx=9 -m0=LZMA2 -r -pmaxis5 %bakfile% %tmpbakpath%\mysql d:\web -x!*.avi -x!*.mov -x!*.mpg d:\bin\*.* C:\WINDOWS\system32\dns\*.dns %tmpbakpath%\securedata.7z
:: (SS,27/4/14) switched to FreeArc default settings, also added output to logtoday.txt, -i0 disables the progress indicator
:: -m2 gave quickest performance taking file upload into account
:: (SS,14/9/15) changed from -m2 to -m1 to see how much quicker or slower it'll be
:: (SS,15/9/15) changed from -m1 to -mx to see how much quicker or slower it'll be
:: (SS,15/9/15) changed from -mx to -m2 to speed up backup (due to a hard drive issue this morning)
%arcexe% a -m2 -i0 -r -pmaxis5 %bakfile% %tmpbakpath%\mysql d:\web -x!*.avi -x!*.mov -x!*.mpg d:\bin\*.* C:\WINDOWS\system32\dns\*.dns %tmpbakpath%\securedata.arc >>logtoday.txt
rem --- delete files which are now in zip
rem del d:\backups\mysql_backup.sql
rem (SS,3/5/12) replaced above with following, now more than one file held in new mysql folder
del %tmpbakpath%\mysql\*.sql
:: delete the added zips
:: (SS,27/4/14) changed from .7z to .arc for FreeArc
del %tmpbakpath%\securedata.arc
call :end_section_time
rem === START OF FTP ====
:: (SS,8/6/19) bypass first FTP (to old backup server) no longer used
goto ftp2
rem ==== FTP to backup server ====
rem (SS,3/9/13)
echo ------------------------------------------------------------>>logtoday.txt
echo %date% %time% Start FTP to backup server >>logtoday.txt
echo.>>logtoday.txt
rem --- build the FTP operations file
:: (SS,3/9/12) for winscp, the open command specifies username and password, and we have to prefix with ftp for normal ftp (see http://winscp.net/eng/docs/scripting)
:: (SS,30/4/14) added [-rawsettings Cipher=blowfish] which improved the upload speed from 6,000 KiB/s to 11,000 KiB/s (approx) nearly double! default encryption is aes which is more CPU intensive
echo open sftp://backup:oscaris10@backup.itpartnership.com:22 -hostkey="8b:b8:6c:0a:c2:bf:a1:55:99:46:b9:9c:bd:43:47:7c" -rawsettings Cipher=blowfish>ftpop1.txt
::echo echo on>>ftpop2.txt
echo option batch continue>>ftpop1.txt
echo option confirm off>>ftpop1.txt
echo binary>>ftpop1.txt
rem following deletes backup file from 7 days ago, included the previous type DDMMYYYY.zip
cmddate -s -7 rm #.7z>>ftpop1.txt
:: (SS<27/4/14) added FreeArc
cmddate -s -7 rm #.arc>>ftpop1.txt
rem following uploads todays backup file
cmddate -s 0 put %bakfile%>>ftpop1.txt
echo exit>>ftpop1.txt
rem --- FTP the file to backup server
%ftpexe% /script=ftpop1.txt>>logtoday.txt
echo. >>logtoday.txt
call :end_section_time
rem ==== End of FTP to home ====
:ftp2
rem ==== FTP to home ====
echo ------------------------------------------------------------>>logtoday.txt
echo %date% %time% Start FTP to SS home server >>logtoday.txt
echo.>>logtoday.txt
rem --- build the FTP operations file
:: (SS,5/7/12) for winscp, the open command specifies username and password, and we have to prefix with ftp for normal ftp (see http://winscp.net/eng/docs/scripting)
::echo open ftp://backup:oscaris10@ftp.ontheworldweb.com:5821>ftpop2.txt
:: (SS,12/7/12) replaced above with following for secure connection, certificate fingerprint is also passed
::echo open ftps://backup:oscaris10@secure.ontheworldweb.com:5821 -explicittls -certificate="59:80:cb:c7:53:c1:89:7e:ab:75:b9:3d:20:7f:10:8a:e6:61:7d:22">ftpop2.txt
:: (SS,25/6/14) replaced above with following
::echo open sftp://webbackup:oscaris10@secure.ontheworldweb.com:22 -hostkey="0b:5a:0b:9b:34:29:01:2c:96:d9:38:26:4a:28:34:90" -rawsettings Cipher=blowfish>ftpop2.txt
:: (SS,12/8/18) changed to use new 1Gb/s hyperoptic connection
:: (SS,11/1/19) replaced hyperoptic with %1 to allow a parameter to set it. I wanted to test with virginmedia@ontheworldweb.com
:: need to make sure one run is done manually to ensure the new host key has been added to the cache in winscp
::echo open sftp://webbackup:oscaris10@%1.ontheworldweb.com:22 -hostkey="0b:5a:0b:9b:34:29:01:2c:96:d9:38:26:4a:28:34:90" -rawsettings Cipher=blowfish>ftpop2.txt
:: (SS,2/6/19) replaced above with following, also installed latest verison of WinSCP to fix issue causing error
echo open sftp://webbackup:oscaris10#@%1.ontheworldweb.com:22 >ftpop2.txt
::echo echo on>>ftpop2.txt
echo option batch continue>>ftpop2.txt
echo option confirm off>>ftpop2.txt
echo binary>>ftpop2.txt
rem following deletes backup file from 65 days ago, included the previous type DDMMYYYY.zip
:: (SS,5/7/12) delete replaced with rm for winscp
:: (SS,20/4/14) changed from 65 to 92 days (i.e. 3 months, quarter)
cmddate -s -92 rm #.7z>>ftpop2.txt
:: (SS<27/4/14) added FreeArc
cmddate -s -92 rm #.arc>>ftpop2.txt
rem following uploads todays backup file
cmddate -s 0 put %bakfile%>>ftpop2.txt
echo exit>>ftpop2.txt
rem --- FTP the file to ontheworldweb server
%ftpexe% /script=ftpop2.txt>>logtoday.txt
:: (SS,16/3/13) added following because it was added the "---.." to the same line
echo. >>logtoday.txt
call :end_section_time
rem ==== End of FTP to home ====
rem === END OF FTP ====
echo ------------------------------------------------------------>>logtoday.txt
echo %date% %time% Finish>>logtoday.txt
echo ============================================================>>logtoday.txt
call :end_main_time
:: append to main log file
echo. >>log.txt
type logtoday.txt >>log.txt
echo ------------------------------------------------------------------------------->>log.txt
:: send results in email
:: (SS,14/1/18) added %1 and modified sendemail.cmd to use %1, host name is used in one of the checks
call sendemail.cmd %1
goto :eof
:: ======================
:: subroutines for timing
:: SS,11/09/2015
:: ======================
:start_main_time
set start_main=%time%
exit /b
:end_main_time
call :get_time_taken %start_main% %time%
echo. >>%logfile%
call :start_main_time
exit /b
:start_section_time
set start_section=%time%
exit /b
:end_section_time
call :get_time_taken %start_section% %time%
echo. >>%logfile%
call :start_section_time
exit /b
:start_time
set start=%time%
exit /b
:end_time
call :get_time_taken %start% %time%
call :start_time
exit /b
:get_time_taken
set l_start=%1
set l_end=%2
rem from http://stackoverflow.com/questions/673523/how-to-measure-execution-time-of-command-in-windows-command-line
REM Calculate time taken in seconds, to the hundredth of a second.
REM Assumes start time and end time will be on the same day.
set options="tokens=1-4 delims=:."
for /f %options% %%a in ("%l_start%") do (
set /a start_s="(100%%a %% 100)*3600 + (100%%b %% 100)*60 + (100%%c %% 100)"
set /a start_hs=100%%d %% 100
)
for /f %options% %%a in ("%l_end%") do (
set /a end_s="(100%%a %% 100)*3600 + (100%%b %% 100)*60 + (100%%c %% 100)"
set /a end_hs=100%%d %% 100
)
set /a s=%end_s%-%start_s%
set /a hs=%end_hs%-%start_hs%
if %hs% lss 0 (
set /a s=%s%-1
set /a hs=100%hs%
)
if 1%hs% lss 100 set hs=0%hs%
echo | set /p=%s%.%hs% secs>>%logfile%
exit /b