help with backup

Any other tech-related topics
Post Reply
Message
Author
garbanzo
Posts: 248
Joined: Thu May 15, 2008 3:00 am

help with backup

#1 Post by garbanzo »

i find myself needing to backup the contents of a particular folder when i start my system. preferably with winrar. i would also like to be be able to automatically delete any backups more than 10 days old.

anyone know an app that can achieve this? or a batch file or script?

thanks!

User avatar
m^(2)
Posts: 890
Joined: Sat Mar 31, 2007 2:38 am
Location: Kce,PL
Contact:

#2 Post by m^(2) »

A fork from my own backup script. 2 batches: The main one:

Code: Select all

@echo off
:::::::::::::::::::::::::::::::
:: SETTINGS
:::::::::::::::::::::::::::::::

::Compressed archive will be placed here
set backupdir=e:\backup
::And named: %archivename%-year-month-day.ext
set archivename=code_backup

:: Possible options: 7z, SQX, SQX_NO_REDUNDANCY, RAR
set COMPRESSOR=RAR

:: your OS language, EN or PL
:: needed for date formatting
set LANG=EN

::batch tools are here
set toolsdir=%~dp0

::These files will be backed up.
set to_backup=e:\code_backup\*

::--------------------------------------------------
:: DO NOT EDIT BELOW
::--------------------------------------------------

::::::::::::::::::::::::::::::::::::::::
:: SET COMPRESSION PARAMETERS
::::::::::::::::::::::::::::::::::::::::
if "%COMPRESSOR%"=="SQX" (
    set pack=sqx_pack
    set ext=sqx
) else if "%COMPRESSOR%"=="SQX_NO_REDUNDANCY" (
    set no_redundancy=1
    set pack=sqx_pack
    set ext=sqx
) else if "%COMPRESSOR%"=="RAR" (
    set pack=rar_pack
    set ext=rar
) else if "%COMPRESSOR%"=="7z" (
    set pack=7z_pack
    set ext=7z
) else (
    echo "%COMPRESSOR%" - unknown archiver
    pause >nul
    goto :eof
)

:: Get currnet date
call %toolsdir%\_getDate.bat

::::::::::::::::::::::::::::::::::::::::
:: PACK
::::::::::::::::::::::::::::::::::::::::

call :%pack% -compress -with_redundancy "%backupdir%\%archivename%-%yy:~2,2%.%mm%.%dd%.%ext%" %to_backup%
call :%pack% -add_redundancy "%backupdir%\%archivename%-%yy:~2,2%.%mm%.%dd%.%ext%"

goto :eof

::----------------------------------------------------------------------------------------------------------
:: SQX PACKING ROUTINE
:: [-store] archive_name file1 file2...
:: -store           = add to archive w/out compressing
:: -with_redundancy = adds redundant recovery data to fix archive in case of data corruption
:: -compress        = compress files as well as possible
:: -add_redundancy  = meant to add recovery blocks to existing archive, unsupported by sqx, makes routine quit
:: in case that %no_redundancy% is set, -with_redundancy means nothing
::------------------------------------------------------------------------------------------------------------
:sqx_pack
setlocal
set compressor=sqc a -fmt sqx2 -sc -ssd -sst -su -r -s -bc

set switches= 
:sqx_parse_switches
if "%1" == "-store" (
    set switches=%switches% -m0
    shift
    goto sqx_parse_switches
) else if "%1" == "-with_redundancy" (
    if "%no_redundancy%" == "" set switches=%switches% -rr5
    shift
    goto sqx_parse_switches
) else if "%1" == "-compress" (
    set switches=%switches% -m5 -au1 -fme1 -fmm1 -ppm1 -ppmm1024 -ppmo32 -rgb1 -uxx9 -md32768 -me -mm
    shift
    goto sqx_parse_switches
) else if "%1" == "-add_redundancy" (
    goto sqx_end
)

:sqx_add_file
if "%1"=="" goto sqx_has_filespec
if exist %1\ (
    ::it's a directory
    set filespec=%filespec% %1\*.*
) else (
    ::it's a file
    set filespec=%filespec% %1
)
shift
goto sqx_add_file

:sqx_has_filespec
if "%filespec%"=="" (
    echo No files to compress!
    goto sqx_end
)
%compressor% %switches% %filespec%
:sqx_end
endlocal
goto :eof

::------------------------------------------------------------------------------------------------------------
:: 7z PACKING ROUTINE
:: [-store] archive_name file1 file2...
:: -store           = add to archive w/out compressing
:: -with_redundancy = here it's meaningless
:: -compress        = compress files as well as possible
:: -add_redundancy  = meant to add recovery blocks to existing archive, unsupported by 7z, makes routine quit
::------------------------------------------------------------------------------------------------------------
:7z_pack
setlocal

set compressor=7z a -t7z -y -m0=LZMA -ms=on 

set switches= 
:7z_parse_switches
if "%1" == "-store" (
    set switches=%switches% -mx0
    shift
    goto 7z_parse_switches
) else if "%1" == "-with_redundancy" (
    shift
    goto 7z_parse_switches
) else if "%1" == "-compress" (
    set switches=%switches% -mx9 -md=48m
    shift
    goto 7z_parse_switches
) else if "%1" == "-add_redundancy" (
    goto 7z_end
)

:7z_add_file
if "%1"=="" goto 7z_has_filespec
set filespec=%filespec% %1
shift
goto 7z_add_file

:7z_has_filespec
if "%filespec%"=="" (
    echo No files to compress!
    goto 7z_end
)
echo %compressor% %switches% %filespec%
%compressor% %switches% %filespec%

:7z_end
endlocal
goto :eof

::------------------------------------------------------------------------------------------------------------
:: RAR PACKING ROUTINE
:: [-store] archive_name file1 file2...
:: -store           = add to archive w/out compressing
:: -with_redundancy = here it's meaningless, I add recovery data after archive is created and only for stored file
:: -compress        = compress files as well as possible
:: -add_redundancy  = add recovery blocks to existing archiv
::------------------------------------------------------------------------------------------------------------
:rar_pack
setlocal

set compressor=rar
set switches=-ms -os -r0

:rar_parse_switches
if "%1" == "-store" (
    set command=a
    set switches=%switches% -m0
    shift
    goto rar_parse_switches
) else if "%1" == "-with_redundancy" (
    shift
    goto rar_parse_switches
) else if "%1" == "-compress" (
    set command=a
    set switches=%switches% -m5 -md4096 -mm
    shift
    goto rar_parse_switches
) else if "%1" == "-add_redundancy" (
    set command=rr5p
    set switches= 
    shift
    goto rar_add_file
)

:rar_add_file
if "%1"=="" goto rar_has_filespec
set filespec=%filespec% %1
shift
goto rar_add_file

:rar_has_filespec
if "%filespec%"=="" (
    echo No files to compress!
    goto rar_end
)
echo %compressor% %command% %switches% %filespec%
%compressor% %command% %switches% %filespec%

:rar_end
endlocal
goto :eof
The second is more advanced:
_getdate.bat:

Code: Select all

@ECHO OFF
CLS
::.
:: SortDate.bat, Version 4.00 for Windows NT 4 / 2000 / XP
:: Display day, month and year, independent of Windows'
:: Regional Settings, and using internal commands only.
::.
:: Based on Simon Sheppard's GetDate.bat
:: http://www.ss64.com/ntsyntax/GetDate.txt
::.
:: This version demonstrates how to adjust the code for non-English Windows
::.
:: Written by Rob van der Woude
:: http://www.robvanderwoude.com
::.
:: Modified by Maciej Adamczyk to set %dd% %mm% and %yy% variables
::.
SETLOCAL
:: Get the format: dd-mm-yy, or mm/dd/yy, or whatever other format your PC uses
FOR /F "tokens=2 delims=()" %%A IN ('ECHO. ^| DATE') DO SET Format=%%A
:: Get the delimiter used: the first character that is different
SET Char1=%Format:~0,1%
SET Char2=%Format:~1,1%
SET Char3=%Format:~2,1%
SET Char4=%Format:~3,1%
SET Char5=%Format:~4,1%
IF NOT [%Char1%]==[%Char2%] (
    SET Delim=%Char2%
    GOTO Parse
)
IF NOT [%Char2%]==[%Char3%] (
    SET Delim=%Char3%
    GOTO Parse
)
IF NOT [%Char3%]==[%Char4%] (
    SET Delim=%Char4%
    GOTO Parse
)
IF NOT [%Char4%]==[%Char5%] (
    SET Delim=%Char5%
) ELSE (
    ECHO Error finding delimiter.
    ECHO Aborting . . .
    GOTO:EOF
)

:: Get the current date string
:Parse
FOR /F "tokens=1* delims= " %%A IN ('DATE/T') DO IF "%%B"=="" (SET Date=%%A) ELSE (SET Date=%%B)

:: Display the intermediate results
::ECHO Date=%Date%        Format=%Format%        Delim=%Delim%
::ECHO.

:: Parse the Date string using the delimiter found earlier
FOR %%? IN (1,2,3) DO CALL :ParseVal %%?


:: This is correct in english and polish
ENDLOCAL & set mm=%mm%& set dd=%dd%& set yy=%yy%%rr%
GOTO:EOF


:ParseVal
:: Get the day, month or year variable name
FOR /F "tokens=%1 delims=%Delim% " %%A IN ('ECHO.%Format%') DO SET Var%1=%%A
:: Get the day, month or year variable value
FOR /F "tokens=%1 delims=%Delim% " %%A IN ('ECHO.%Date%')   DO SET Val%1=%%A
:: Assingn the value
CALL SET %%Var%1%%=%%Val%1%%
:: Display the result
CALL SET %%Var%1%%
GOTO:EOF
You have to add deleting old ones yourself. :P
EDIT: minor bugfix
Last edited by m^(2) on Mon Aug 18, 2008 8:26 am, edited 1 time in total.

ashghost
Posts: 384
Joined: Wed Feb 06, 2008 2:55 pm
Location: South Carolina

#3 Post by ashghost »

Since you have WinRAR and you only want to backup a single folder you don't need all that - you only need one line. WinRAR's help details all of the command line switches.

Here's an example

Code: Select all

"C:\Program Files\WinRAR\winrar.exe" a -r "C:\Path\to\backup_storage\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.rar" "C:\Path\to\folder_to_backup"
This will give you an archive of your folder (the -r switch recurses subdirectories) titled with the date, like 2008-08-18.rar. I got the date format bit from http://cybernetnews.com/2008/01/03/cybe ... e-backups/

I don't know how to do the cleanup.

User avatar
m^(2)
Posts: 890
Joined: Sat Mar 31, 2007 2:38 am
Location: Kce,PL
Contact:

#4 Post by m^(2) »

But this date trick depends on locale.
That's why I use the separate batch - it requires less tweaking and considering that I didn't write it - didn't take much time either. :)

garbanzo
Posts: 248
Joined: Thu May 15, 2008 3:00 am

#5 Post by garbanzo »

wow, thank you m^(2), but that is greek to me. i have absolutely no idea how to change that to fit my needs.

i managed to get ashghost's suggestion working, which is a start, but it's the cleanup part that i really need.

perhaps this will help (this does not have to be portable, by the way):

what to archive:
C:\Documents and Settings\All Users\Application Data\GPSoftware\Directory Opus

where to put it:
C:\Documents and Settings\All Users\Application Data\GPSoftware\Backup

edit - i posted this on another forum too, and i've had four people give me batch files, but nothing has worked.

is there an app that will do this??

ashghost
Posts: 384
Joined: Wed Feb 06, 2008 2:55 pm
Location: South Carolina

#6 Post by ashghost »

I think I found your answer: FORFILES

http://www.ss64.com/nt/forfiles.html

So you would use the following:

FORFILES -p "C:\Documents and Settings\All Users\Application Data\GPSoftware\Backup" /d -09 /m *.rar /c "cmd /c del @file"

I tried it out and it works like a charm for me in CMD.exe

garbanzo
Posts: 248
Joined: Thu May 15, 2008 3:00 am

#7 Post by garbanzo »

thanks ashghost. i got the same suggestion on the other forum, but we have not been able to get it to work right i'll try your method in the morning, i got things to do now unfortunately...

garbanzo
Posts: 248
Joined: Thu May 15, 2008 3:00 am

#8 Post by garbanzo »

well here's one i got from another forum and it finally works!

Code: Select all

@ECHO OFF
cd "C:\Documents and Settings\All Users\Application Data\GPSoftware\"

set path="%programfiles%\WinRAR\";%path%
rar a -agYYYYMMDD "Backup\backup.rar" "Directory Opus"

forfiles -pBackup\ -s -m*.rar -d-10 -c"CMD /C del /q @FILE"
thanks for the replies everyone :)

Post Reply