Previously I wrote an AutoIt script called RoboBackup. That script used Robocopy to automate backing up files into three (or more) folders. It cycled through those folders synchronising files each time.
In this article I am going to explain a slightly more advanced script that also backs up files but does so by using both the Robocopy command and also the 7-Zip 7za.exe command line program. I've called this backup script 'RoboBackup7z'.
The idea behind RoboBackup7z is that the latest backup is a synchronised folder managed by Robocopy. But previous Robocopy backups are archived in 7z files for safe-keeping. This means the latest data is easy to recover and the previous backups are still available but are smaller compressed files you could copy elsewhere - to DVD or cloud storage perhaps.
First let's have a look at how this script works and after how it is put together. I hope you'll find this interesting.
Setup
The idea is that you create a folder on a removable drive or USB flash drive and run the script from there. In the following screen shot you can see my example:
In the above example I have an external USB hard disk drive that is drive G: on my computer. I am backing up files from my D: drive (source) and so I've created a folder called "D" (I could've called it anything but this makes sense to me).
Inside the "D" folder I have the following files:
Edit RoboBackup7z.ini
Add a folder to backup. This can also be a drive letter, in our example we'll use D:. This backs up all folders and files found on drive D:.
Run
Double click RoboBackup7z.exe
The first time it runs the following will be created:
The second time it runs the following happens:
The third time it runs:
In the above example I have run the script more than twice. The Backup folder contains the files of the latest copy and the Archive folder contains the 7z files. The log is the latest log file.
Here's what it looks like inside the Archive folder:
In the above screen shot you can see the txt files are the logs. You can open these in Notepad. They are the original log files produced by Robocopy when it originally ran.
The other files are the 7z files. They are in 300 MB chunks. To view and extract files open the first one (001) with 7-Zip for Windows.
IMPORTANT: There is no control over how much disk space is used or how many backups can be made. You must manage the disk space yourself. Move files from the Archive folder to another location as needed.
Source Code
In the following screen shot you'll see the code as I see it when I writing it. On the left hand side you can see the line numbers. Below the image I'll explain line by line what the script does but first a few basics, here's how the colour coding works:
Green - comments, text to explain what the script is doing. Therefore lines 1 to 11 is just for information.
Black - variables, they also start with a dollar sign $
Blue - functions like IF.
Orange - operators like <>=
Red - text
--- Line 15
This defines the $ini variable that identifies the RoboBackup7z.ini file. It's a configuration file containing the drive/folder to backup and the name of the last log file.
--- Line 17 to 25
These are the variables and what their initial values are. This includes reading data from the ini file.
--- Line 27 to 39
First this checks if the 7za.exe exists. If it does not, it'll skip this part.
If 7za.exe does exist then it'll check for the destination folder (Backup), if this exists then it'll run the 7za program to archive the files from the destination (Backup) folder.
--- Line 32
This is the 7za line. It compresses the files from the Backup folder into one or more 7z compressed (zipped) files. The 7za.exe has a number of parameters:
-r = recurse, meaning to archive sub-folders/files
-v300m = this splits the 7z archive file into smaller files (volumes). In this case we're splitting to 300MB files. You can change this to any size that fits your needs. If you do not use this parameter then the archive will be one large 7z file. Such a file might be very big, depending on what files you have to backup, larger than 2GB, therefore it makes sense to split to smaller files to make them easier to copy. Many services do not support files larger than 2GB.
--- Line 36 and 37
These copy the log file to the Archive folder. This is so the log is stored along with the backup it was made from.
--- Line 42
Save the date (name of the log file) to the ini file. This so it can be used the next time the backup runs to identify the 'last' backup.
--- Line 45
Runs ROBOCOPY to synchronise files from the source to the backup (destination) folder.
--- Line 57
Open Notepad and display the log file. This will happen at the end and shows you what has happened during this backup. It's the usual output from Robocopy.
--- Line 59
Exit just closes the script. You don't really need this but usually put it just to show where it ends.
My RoboBackup7z Script Source Code for Copy/Paste
Here's the same script as shown above in the screen shot. But this time it's text so you can copy/paste it and use it. Feel free you change it :-)
Other required files
For the script to work you'll need Robocopy, this command is included with Windows as standard. Also you'll need the 7za.exe file, you can download it free of charge from http://www.7-zip.org/
For more detailed information please see the following article:
https://mgxp.blogspot.ch/2016/04/7-zip-7za-command-line-zip-tool.html
Conclusion
It's amazing what you can do with free tools! AutoIt is of course the easiest way to make a script that's compact and simple. Just look at how few lines of code I used. Robocopy and 7za too, excellent. This script works very nicely and I've been using it for several months now. But it has some shortcomings. For instance, I have to manage the disk space myself. Also it only backs up from one location (drive or folder location and sub-folders). But this is fine, it means I have more work to do in the future!
Disclaimer
Reference
AutoIt
https://www.autoitscript.com
Robocopy at Wikipedia
https://en.wikipedia.org/wiki/Robocopy
Various articles about Robocopy
https://mgxp.blogspot.ch/search/label/Robocopy
7za, the 7-zip command line program
The idea is that you create a folder on a removable drive or USB flash drive and run the script from there. In the following screen shot you can see my example:
In the above example I have an external USB hard disk drive that is drive G: on my computer. I am backing up files from my D: drive (source) and so I've created a folder called "D" (I could've called it anything but this makes sense to me).
Inside the "D" folder I have the following files:
- 7za.exe
- RoboBackup7z.au3
- RoboBackup7z.exe
- RoboBackup7z.ini
Edit RoboBackup7z.ini
Add a folder to backup. This can also be a drive letter, in our example we'll use D:. This backs up all folders and files found on drive D:.
Run
Double click RoboBackup7z.exe
The first time it runs the following will be created:
- A folder called Backup where a copy of the folders and files will be made.
- A log file named yyyymmdd_hhmmss_LOG.txt (the RoboCopy log showing what was backed up).
The second time it runs the following happens:
- A folder called Archive is created
- 7-Zip runs, it adds all the folders/files from the Backup folder to a 7z archive file in the Archive folder.
- It names the 7z with the name of the last backup log file (it gets this from the ini).
- It splits the 7z file into 300MB separate files.
- The log file is moved to the Archive folder.
- RoboCopy runs again, the Backup folder is updated with new files, a new log file appears in the script folder.
The third time it runs:
- The same as the second time, a new 7z is created and stored in the Archive folder with the log file. RoboCopy runs again to update the Backup folder and create a new log file.
In the above example I have run the script more than twice. The Backup folder contains the files of the latest copy and the Archive folder contains the 7z files. The log is the latest log file.
Here's what it looks like inside the Archive folder:
In the above screen shot you can see the txt files are the logs. You can open these in Notepad. They are the original log files produced by Robocopy when it originally ran.
The other files are the 7z files. They are in 300 MB chunks. To view and extract files open the first one (001) with 7-Zip for Windows.
IMPORTANT: There is no control over how much disk space is used or how many backups can be made. You must manage the disk space yourself. Move files from the Archive folder to another location as needed.
Source Code
In the following screen shot you'll see the code as I see it when I writing it. On the left hand side you can see the line numbers. Below the image I'll explain line by line what the script does but first a few basics, here's how the colour coding works:
Green - comments, text to explain what the script is doing. Therefore lines 1 to 11 is just for information.
Black - variables, they also start with a dollar sign $
Blue - functions like IF.
Orange - operators like <>=
Red - text
--- Line 15
This defines the $ini variable that identifies the RoboBackup7z.ini file. It's a configuration file containing the drive/folder to backup and the name of the last log file.
--- Line 17 to 25
These are the variables and what their initial values are. This includes reading data from the ini file.
--- Line 27 to 39
First this checks if the 7za.exe exists. If it does not, it'll skip this part.
If 7za.exe does exist then it'll check for the destination folder (Backup), if this exists then it'll run the 7za program to archive the files from the destination (Backup) folder.
--- Line 32
This is the 7za line. It compresses the files from the Backup folder into one or more 7z compressed (zipped) files. The 7za.exe has a number of parameters:
-r = recurse, meaning to archive sub-folders/files
-v300m = this splits the 7z archive file into smaller files (volumes). In this case we're splitting to 300MB files. You can change this to any size that fits your needs. If you do not use this parameter then the archive will be one large 7z file. Such a file might be very big, depending on what files you have to backup, larger than 2GB, therefore it makes sense to split to smaller files to make them easier to copy. Many services do not support files larger than 2GB.
--- Line 36 and 37
These copy the log file to the Archive folder. This is so the log is stored along with the backup it was made from.
--- Line 42
Save the date (name of the log file) to the ini file. This so it can be used the next time the backup runs to identify the 'last' backup.
--- Line 45
Runs ROBOCOPY to synchronise files from the source to the backup (destination) folder.
--- Line 57
Open Notepad and display the log file. This will happen at the end and shows you what has happened during this backup. It's the usual output from Robocopy.
--- Line 59
Exit just closes the script. You don't really need this but usually put it just to show where it ends.
My RoboBackup7z Script Source Code for Copy/Paste
Here's the same script as shown above in the screen shot. But this time it's text so you can copy/paste it and use it. Feel free you change it :-)
#cs ----------------------------------------------------------------------------
RoboBackup using 7-Zip
AutoIt Version: 3.3.10.2
Author: Michael Gerrard / http://mgxp.blogspot.com
Script Function:
Create a backup using Robocopy and archives using 7-zip
#ce ----------------------------------------------------------------------------
; Set variables
$ini = @ScriptDir & "\RoboBackup7z.ini"
$FolderToBackup = IniRead($ini, "Settings", "FolderToBackup", @MyDocumentsDir)
$source = '"' & $FolderToBackup & '"'
$dest = @ScriptDir & "\Backup"
$archive = @ScriptDir & "\Archive"
$today = @YEAR & @MON & @MDAY & "_" & @HOUR & @MIN & @SEC
$log = @ScriptDir & "\" & $today & "_LOG.txt"
$7za = @ScriptDir & "\7za.exe"
$last = IniRead($ini, "Settings", "LastBackup", "00000000_000000")
$7zBackup = $archive & "\" & $last
; Check if a backup was done before, if so, archive it to a 7z file
If FileExists($7za) Then
If FileExists($dest) Then
DirCreate($archive) ;make sure the archive folder exists
RunWait(@ComSpec & " /c " & $7za & " a -r -v300m -w -y " & $7zBackup & " " & _
$dest & "\*.*", @ScriptDir, @SW_MINIMIZE) ; add to 7z archive file
; Move the log to the archive folder
FileCopy(@ScriptDir & "\" & $last & "_LOG.txt", $archive)
FileDelete(@ScriptDir & "\" & $last & "_LOG.txt")
EndIf
EndIf
; Write today's date and time to the ini
IniWrite($ini, "Settings", "LastBackup", $today)
; Run RoboCopy
RunWait(@ComSpec & " /c ROBOCOPY " & $source & " " & $dest & _
" /MIR /R:2 /LOG:" & $log & " /TEE" & " /XF desktop.ini ", @ScriptDir, @SW_MINIMIZE)
; /MIR = mirror
; /R:2 = retry twice
; /LOG: = record to a log file
; /TEE = output to the screen too
; /XF desktop.ini = don't copy the desktop.ini file (avoids getting the My Documents folder name)
; Display the log file
If FileExists($log) Then
Run("notepad " & $log)
EndIf
Exit
Other required files
For the script to work you'll need Robocopy, this command is included with Windows as standard. Also you'll need the 7za.exe file, you can download it free of charge from http://www.7-zip.org/
For more detailed information please see the following article:
https://mgxp.blogspot.ch/2016/04/7-zip-7za-command-line-zip-tool.html
Conclusion
It's amazing what you can do with free tools! AutoIt is of course the easiest way to make a script that's compact and simple. Just look at how few lines of code I used. Robocopy and 7za too, excellent. This script works very nicely and I've been using it for several months now. But it has some shortcomings. For instance, I have to manage the disk space myself. Also it only backs up from one location (drive or folder location and sub-folders). But this is fine, it means I have more work to do in the future!
Disclaimer
The script here is just an example. I do not provide it with any guarantee. I'm not advocating this script be used for anything specific, this article is just to demonstrate what can be done. Use at your own risk!
Reference
AutoIt
https://www.autoitscript.com
Robocopy at Wikipedia
https://en.wikipedia.org/wiki/Robocopy
Various articles about Robocopy
https://mgxp.blogspot.ch/search/label/Robocopy
7za, the 7-zip command line program
If you are looking for some help with creating self-extracting archives, please see my previous articles specifically on this more advanced subject:
PART 1 - http://mgxp.blogspot.com/2010/07/create-self-extracting-archive-exe.html
PART 2 - http://mgxp.blogspot.com/2013/01/create-self-extracting-archive-exe-part.html
PART 1 - http://mgxp.blogspot.com/2010/07/create-self-extracting-archive-exe.html
PART 2 - http://mgxp.blogspot.com/2013/01/create-self-extracting-archive-exe-part.html
No comments:
Post a Comment