Robocopy Snippets


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# create some test data
mkdir -p /mnt/c/test/test/documents /mnt/c/test/test/secret /mnt/c/test/test/symbfolder \ 
/mnt/c/test/backup/documents /mnt/c/test/backup/secret /mnt/c/test/backup/symbfolder && \ 
touch /mnt/c/test/test/apple.txt /mnt/c/test/test/apple2.txt /mnt/c/test/test/documents/cv.pdf \ 
/mnt/c/test/backup/apple.txt /mnt/c/test/backup/apple2.txt /mnt/c/test/backup/documents/cv.pdf

# create some symblinks
mklink /d test_symblink_dir c:\test\test\documents && \ 
mklink test_symblink_file1 c:\test\test\documents\cv.pdf && \ 
mklink test_symblink_file2 c:\test\test\apple.txt

# robocopy commands
robocopy C:\test\test C:\test\backup * /E /PURGE /ZB /SL /MT:20 /XO /A-:HS /COPY:DAT /DCOPY:DAT /W:0 /R:1 /ETA
robocopy C:\test\test C:\test\backup * /E /PURGE /ZB /SL /XJ /MT:20 /XO /A-:HS /COPY:DAT /DCOPY:DAT /W:0 /R:1 /ETA

# test the solution
function safe_backup() {
    # some variables
    source="C:\test\test"
    dest="C:\test\backup"
    dest_find="/mnt/c/test/test/backup"
    robocopyoptions="/E /PURGE /ZB /SL /MT:20 /XO /A-:HS /COPY:DAT /DCOPY:DAT /W:0 /R:1 /ETA"

    # remove all links
    find $dest_find -type l -print0 | xargs -0 rm -v --

    # perform the backup
    cmd.exe /c robocopy "$source" "$dest" "*" $robocopyoptions
}
safe_backup