rss logo

AutoIt script to backup user profile

Here is an easy AutoIt script that I use for basic users who need to automaticaly backup their datas to external drive. It backups current user Desktop, Downloads, Documents, Music, Pictures, Videos and Thunderbird folders.

Configuration

Instructions

Usage

Code

V1.1

#include <Date.au3>
#include <MsgBoxConstants.au3>
; Role : Backup User and Thunderbird profile to Z:\BACKUP\YYYY
; Author : http://shebangthedolphins.net/
; Instructions  : 
; - Allocate backup drive to Z:
; - use  Aut2Exe to convert script to .exe file
; 1.0 first version
; 1.1 Create a YYYY folder to destination, automaticaly use UserName path

;separate hour and date
;source : https://www.autoitscript.com/autoit3/docs/libfunctions/_DateTimeSplit.htm
Local $aMyDate, $aMyTime
_DateTimeSplit( _NowCalc(), $aMyDate, $aMyTime)

;Date variable which will include current date
Local $Date

;if month is less than 10 we add 0 in front of the value
If $aMyDate[2] < 10 Then
	$aMyDate[2] = "0" & $aMyDate[2]
	EndIf

;if days are less than 10 we add 0 in front of the value
If $aMyDate[3] < 10 Then
	$aMyDate[3] = "0" & $aMyDate[3]
	EndIf

;We concatenate variables in order to have date value with this format : YYYY.MM.JJ
;$Date = $aMyDate[1] & "." & $aMyDate[2] & "." & $aMyDate[3]
;We only select YYYY (current year) value
$Date = $aMyDate[1]

;Get all drives letters
;https://www.autoitscript.com/autoit3/docs/functions/DriveGetDrive.htm
Local $aArray = DriveGetDrive($DT_ALL)

;Folders table which contain all folders we want to save
Local $SFolders[6]
$SFolders[0] = "Desktop"
$SFolders[1] = "Downloads"
$SFolders[2] = "Documents"
$SFolders[3] = "Music"
$SFolders[4] = "Pictures"
$SFolders[5] = "Videos"

    For $i = 1 To $aArray[0]
		;Backup only if Z: drive is found
		If StringUpper($aArray[$i]) == "Z:" Then
			For $element in $SFolders
				RunWait(@ComSpec & " /c " & 'robocopy "C:\Users\' & @UserName & '\' & $element & '" Z:\BACKUP\' & $Date & '\' & $element & ' /MIR /R:2 /W:2 /X /V /NP /LOG:Z:\BACKUP\log_' & $element & '.txt /TEE', "", @SW_MAXIMIZE)
			Next
			;Thunderbird Backup
			RunWait(@ComSpec & " /c " & 'robocopy "C:\Users\' & @UserName & '\AppData\Local\Thunderbird" Z:\BACKUP\' & $Date & '\Thunderbird\Local /MIR /R:2 /W:2r /X /V /NP /LOG:Z:\BACKUP\thunderbird_Local.txt /TEE', "", @SW_MAXIMIZE)
			RunWait(@ComSpec & " /c " & 'robocopy "C:\Users\' & @UserName & '\AppData\Roaming\Thunderbird" Z:\BACKUP\' & $Date & '\Thunderbird\Roaming /MIR /R:2 /W:2r /X /V /NP /LOG:Z:\BACKUP\thunderbird_Roaming.txt /TEE', "", @SW_MAXIMIZE)
		EndIf
    Next

V1.2

I've updated the script. It will now create a YYYY.MM_HOSTNAME destination folder and will get the Desktop, Downloads, Documents, Music, Pictures, Videos paths from Windows Registry (in case the location has been changed).

#include <Date.au3>
#include <MsgBoxConstants.au3>
; Role : Backup User and Thunderbird profile to Z:\BACKUP\YYYY.MM_HOSTNAME
; Author : http://shebangthedolphins.net/
; Instructions  : 
; - Allocate backup drive to Z:
; - use  Aut2Exe to convert script to .exe file
; 1.0 first version
; 1.1 Create a YYYY folder to destination, automaticaly use UserName path
; 1.2 Create a YYYY.MM_HOSTNAME folder to Z:\BACKUP, automaticaly get User Profile path from registry
;separate hour and date
;source : https://www.autoitscript.com/autoit3/docs/libfunctions/_DateTimeSplit.htm
Local $aMyDate, $aMyTime
_DateTimeSplit( _NowCalc(), $aMyDate, $aMyTime)

;Date variable which will include current date
Local $Date

;if month is less than 10 we add 0 in front of the value
If $aMyDate[2] < 10 Then
	$aMyDate[2] = "0" & $aMyDate[2]
	EndIf

;if days are less than 10 we add 0 in front of the value
If $aMyDate[3] < 10 Then
	$aMyDate[3] = "0" & $aMyDate[3]
	EndIf

;We concatenate variables in order to have date value with this format : YYYY.MM.JJ
;$Date = $aMyDate[1] & "." & $aMyDate[2] & "." & $aMyDate[3]
;We only select YYYY.MM (current year and current month) value
$Date = $aMyDate[1] & "." & $aMyDate[2]
;We only select YYYY (current year) value
;$Date = $aMyDate[1]

;Get all drives letters
;https://www.autoitscript.com/autoit3/docs/functions/DriveGetDrive.htm
Local $aArray = DriveGetDrive($DT_ALL)

;Folders table which contain all folders we want to save
Local $SFolders[6]
$SFolders[0] = "Desktop"
$SFolders[1] = "{374DE290-123F-4565-9164-39C4925E467B}"  ;For Downloads Folder, see https://www.autoitscript.com/forum/topic/187782-downloads-folder-macro/
$SFolders[2] = "Personal"
$SFolders[3] = "My Music"
$SFolders[4] = "My Pictures"
$SFolders[5] = "My Video"

    For $i = 1 To $aArray[0]
		;Backup only if Z: drive is found
		If StringUpper($aArray[$i]) == "Z:" Then
			For $element in $SFolders
			   Local $sFilePath = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders", $element)
			   $sFilePath = StringReplace($sFilePath, "%USERPROFILE%", @UserProfileDir)
			   ;Local $Folder = StringReplace($sFilePath, @UserProfileDir & "\", "") ; See https://www.autoitscript.com/autoit3/docs/macros/Directory.htm for macroreference variables
			   Local $Folder = StringRegExpReplace($sFilePath, '.*\\', "")
			   ConsoleWrite ( $sFilePath & " " & $Folder & " " )
			   ;RunWait(@ComSpec & " /c " & 'robocopy "' & $sFilePath & '" Z:\BACKUP\' & $Date & '_' & @ComputerName & '\' & $Folder & ' /MIR /R:2 /W:2 /X /V /NP /LOG:Z:\BACKUP\log_' & $Folder & '.txt /TEE', "", @SW_MAXIMIZE)
			   RunWait(@ComSpec & " /c " & 'robocopy "' & $sFilePath & '" "Z:\BACKUP\' & $Date & '_' & @ComputerName & '\' & $Folder & '" /MIR /R:2 /W:2 /X /V /NP /LOG:"Z:\BACKUP\log_' & $Folder & '.txt" /TEE', "", @SW_MAXIMIZE)
			Next
			;Thunderbird Backup
			RunWait(@ComSpec & " /c " & 'robocopy "C:\Users\' & @UserName & '\AppData\Local\Thunderbird" Z:\BACKUP\' & $Date & '_' & @ComputerName & '\Thunderbird\Local /MIR /R:2 /W:2r /X /V /NP /LOG:Z:\BACKUP\thunderbird_Local.txt /TEE', "", @SW_MAXIMIZE)
			RunWait(@ComSpec & " /c " & 'robocopy "C:\Users\' & @UserName & '\AppData\Roaming\Thunderbird" Z:\BACKUP\' & $Date & '_' & @ComputerName & '\Thunderbird\Roaming /MIR /R:2 /W:2r /X /V /NP /LOG:Z:\BACKUP\thunderbird_Roaming.txt /TEE', "", @SW_MAXIMIZE)
		EndIf
    Next

V1.3

Once again, I've updated the script. We don't have to specify Z: drive letter anymore. The script will automatically search for a BACKUP label drive and get the associate letter.

#include <Date.au3>
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
; Role : Backup User and Thunderbird profile to Z:\BACKUP\YYYY.MM_HOSTNAME
; Author : http://shebangthedolphins.net/
; Instructions  : 
; - Allocate backup drive to Z:
; - use  Aut2Exe to convert script to .exe file
; 1.0 first version
; 1.1 Create a YYYY folder to destination, automaticaly use UserName path
; 1.2 Create a YYYY.MM_HOSTNAME folder to Z:\BACKUP, automatically get User Profile path from registry
; 1.3 Backup automaticaly on the BACKUP label drive
;separate hour and date
;source : https://www.autoitscript.com/autoit3/docs/libfunctions/_DateTimeSplit.htm

Local $aArray = DriveGetDrive($DT_ALL)	; https://www.autoitscript.com/autoit3/docs/functions/DriveGetDrive.htm
Local $Drive	; drive letter with BACKUP label

If @error Then
    ; An error occurred when retrieving the drives.
    MsgBox($MB_SYSTEMMODAL, "", "It appears an error occurred.")
	Exit 1
Else
   For $i = 1 To $aArray[0]
	  Local $sLabel = DriveGetLabel(StringUpper($aArray[$i]) & "\") ; Find the volume label of the current drive.
	  If (StringUpper($sLabel) == "BACKUP") Then
		 $Drive = StringUpper($aArray[$i]) ;Drive variable : "Letter:"
		 ExitLoop ;One find, exit loop
	  EndIf
   Next
   if Not ($Drive) Then
	  MsgBox($MB_SYSTEMMODAL, "", "No BACKUP disk found")
	  Exit 1
   EndIf
EndIf


;separate hour and date
;source : https://www.autoitscript.com/autoit3/docs/libfunctions/_DateTimeSplit.htm
Local $aMyDate, $aMyTime
_DateTimeSplit( _NowCalc(), $aMyDate, $aMyTime)

;Date variable which will include current date
Local $Date

;if month is less than 10 we add 0 in front of the value
If $aMyDate[2] < 10 Then
	$aMyDate[2] = "0" & $aMyDate[2]
	EndIf

;if days are less than 10 we add 0 in front of the value
If $aMyDate[3] < 10 Then
	$aMyDate[3] = "0" & $aMyDate[3]
	EndIf

;We concatenate variables in order to have date value with this format : YYYY.MM.JJ
;$Date = $aMyDate[1] & "." & $aMyDate[2] & "." & $aMyDate[3]
;We only select YYYY.MM (current year and current month) value
$Date = $aMyDate[1] & "." & $aMyDate[2]
;We only select YYYY (current year) value
;$Date = $aMyDate[1]

;Get all drives letters
;https://www.autoitscript.com/autoit3/docs/functions/DriveGetDrive.htm
;Local $aArray = DriveGetDrive($DT_ALL)

;Folders table which contain all folders we want to save
Local $SFolders[6]
$SFolders[0] = "Desktop"
$SFolders[1] = "{374DE290-123F-4565-9164-39C4925E467B}"  ;For Downloads Folder, see https://www.autoitscript.com/forum/topic/187782-downloads-folder-macro/
$SFolders[2] = "Personal"
$SFolders[3] = "My Music"
$SFolders[4] = "My Pictures"
$SFolders[5] = "My Video"

For $element in $SFolders
   Local $sFilePath = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders", $element)
   $sFilePath = StringReplace($sFilePath, "%USERPROFILE%", @UserProfileDir)
   ;Local $Folder = StringReplace($sFilePath, @UserProfileDir & "\", "") ; See https://www.autoitscript.com/autoit3/docs/macros/Directory.htm for macroreference variables
   Local $Folder = StringRegExpReplace($sFilePath, '.*\\', "")
   ConsoleWrite ( $sFilePath & " " & $Folder & " " )
   RunWait(@ComSpec & " /c " & 'robocopy "' & $sFilePath & '" "' & $Drive & '\BACKUP\' & $Date & '_' & @ComputerName & '\' & $Folder & '" /MIR /R:2 /W:2 /X /V /NP /LOG:"' & $Drive & '\BACKUP\log_' & $Folder & '.txt" /TEE', "", @SW_MAXIMIZE)
Next
   ;Sauvegarde de Thunderbird
   RunWait(@ComSpec & " /c " & 'robocopy "C:\Users\' & @UserName & '\AppData\Local\Thunderbird" ' & $Drive & '\BACKUP\' & $Date & '_' & @ComputerName & '\Thunderbird\Local /MIR /R:2 /W:2r /X /V /NP /LOG:' & $Drive & '\BACKUP\thunderbird_Local.txt /TEE', "", @SW_MAXIMIZE)
   RunWait(@ComSpec & " /c " & 'robocopy "C:\Users\' & @UserName & '\AppData\Roaming\Thunderbird" ' & $Drive & '\BACKUP\' & $Date & '_' & @ComputerName & '\Thunderbird\Roaming /MIR /R:2 /W:2r /X /V /NP /LOG:' & $Drive & '\BACKUP\thunderbird_Roaming.txt /TEE', "", @SW_MAXIMIZE)
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address