Skip to content

Instantly share code, notes, and snippets.

@Onurtag
Last active August 31, 2020 23:15
Show Gist options
  • Save Onurtag/6e99802131a1995f5946c310643d331e to your computer and use it in GitHub Desktop.
Save Onurtag/6e99802131a1995f5946c310643d331e to your computer and use it in GitHub Desktop.
Backup a folder, a game save, a file or whatever you want every x seconds with the help of 7-zip.
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
FileEncoding, UTF-8
SetFormat, float, 0.3
;7Backup: https://gist.github.com/Onurtag/6e99802131a1995f5946c310643d331e
;For this script, YOU NEED 7z.exe (https://www.7-zip.org/) . Copy that exe from your 7-zip installation to the script's folder or add your 7z folder to path (environment variables).
;TO DELETE DUPLICATE FILES, You need SwissFileKnife (https://sourceforge.net/projects/swissfileknife/).
;!!! RENAME IT TO sfk.exe !!! and copy to the script's folder or add your sfk folder to path (environment variables).
BackupSource := ""
OutputFolder := A_Desktop . "\7Backups"
Timer := 60
Backupname := "My7Backup"
Backingup := 0
savetoini := 1
UseListFile := 0
DeleteDuplicates := 0
Menu, Tray, Icon,,, 1
Menu, Tray, NoStandard
Menu, Tray, Add, 7Backup Wizard, theWizard
Menu, Tray, Add, Backup Folder Info, InfoWindow
Menu, Tray, Add, Open Backup Folder, BrowseS
Menu, Tray, Add, Start Backup Timer, ToggleBackups
Menu, Tray, Add, Exit, Exited
Menu, Tray, Tip, 7Backup
Menu, Tray, Default, 7Backup Wizard
IfExist, 7Backup.ini
{
IniRead, BackupSource, 7Backup.ini, 7Backup, BackupSource
IniRead, OutputFolder, 7Backup.ini, 7Backup, OutputFolder
IniRead, Backupname, 7Backup.ini, 7Backup, Backupname
IniRead, Timer, 7Backup.ini, 7Backup, Timer
IniRead, UseListFile, 7Backup.ini, 7Backup, UseListFile
IniRead, DeleteDuplicates, 7Backup.ini, 7Backup, DeleteDuplicates
}
WizardGui()
Return
theWizard:
WizardGui()
Return
/*
~F10::
ListVars
Return
*/
WizardGui() {
Global
Gui, Wizard: Destroy
Gui, Wizard: Color, 292929
Gui, Wizard: Font, s14 c836DFF bold, Segoe UI
Gui, Wizard: Add, Text, x242 y12, 7Backup Wizard
Gui, Wizard: Font, s10 norm
Gui, Wizard: Add, Text, x30 y56 h13, Backup Name:
Gui, Wizard: Add, Text, x30 y101 h13, Folder to Backup:
Gui, Wizard: Add, Text, x30 y146 h13, Destination Folder:
Gui, Wizard: Add, Text, x30 y191 h13, Timer (Seconds):
Gui, Wizard: Font, s10 bold
Gui, Wizard: Add, Edit, x157 y55 w315 h20 vBackupname, %Backupname%
Gui, Wizard: Add, Checkbox, x480 y56 Checked%UseListFile% vUseListFile gListChecked, Use a list file
Gui, Wizard: Add, Edit, x157 y100 w315 h20 Disabled%UseListFile% vBackupSource, %BackupSource%
Gui, Wizard: Add, Edit, x157 y145 w315 h20 vOutputFolder, %OutputFolder%
Gui, Wizard: Add, Edit, x157 y190 w84 h20 vTimer, %Timer%
Gui, Wizard: Font, s9 cDCDCCC norm
Gui, Wizard: Add, Button, x480 y101 w60 h18 Disabled%UseListFile% vSeS gSelectS, Browse
Gui, Wizard: Add, Button, x480 y146 w60 h18 vSeB gSelectB, Browse
Gui, Wizard: Add, Button, x548 y101 w60 h18 Disabled%UseListFile% vBrS gBrowseS, Open
Gui, Wizard: Add, Button, x548 y146 w60 h18 vBrB gBrowseB, Open
Gui, Wizard: Add, Checkbox, x430 y250 Checked%DeleteDuplicates% vDeleteDuplicates, Delete Duplicates
Gui, Wizard: Add, Checkbox, x430 y271 Checked%savetoini% vsavetoini, Save to .ini
Gui, Wizard: Font, s10 cDCDCCC norm bold
if (Backingup)
{
Gui, Wizard: Add, Button, x237 y236 w164 h66 vToggleButton gToggleBackups, Stop Backup Timer
} else {
Gui, Wizard: Add, Button, x237 y236 w164 h66 vToggleButton gToggleBackups, Start Backup Timer
}
Gui, Wizard: Show, w640 h330, 7Backup Wizard
}
BrowseS:
Gui, Submit, NoHide
Run, %BackupSource%
Return
BrowseB:
Gui, Submit, NoHide
Run, %OutputFolder%
Return
SelectS:
FileSelectFolder, BackupSource, , , Select a folder to backup
GuiControl, Wizard:, BackupSource, %BackupSource%
Return
SelectB:
FileSelectFolder, OutputFolder, , , Select a destination folder
GuiControl, Wizard:, OutputFolder, %OutputFolder%
Return
ToggleBackups:
Gui, Submit, NoHide
if (savetoini)
{
IniWrite, %BackupSource%, 7Backup.ini, 7Backup, BackupSource
IniWrite, %OutputFolder%, 7Backup.ini, 7Backup, OutputFolder
IniWrite, %Backupname%, 7Backup.ini, 7Backup, Backupname
IniWrite, %Timer%, 7Backup.ini, 7Backup, Timer
IniWrite, %UseListFile%, 7Backup.ini, 7Backup, UseListFile
IniWrite, %DeleteDuplicates%, 7Backup.ini, 7Backup, DeleteDuplicates
}
if (Backingup)
{
KillBackups()
GuiControl, , ToggleButton, Start Backup Timer
Menu, Tray, Rename, Stop Backup Timer, Start Backup Timer
} else {
Backingup := 1
GuiControl, , ToggleButton, Stop Backup Timer
Menu, Tray, Rename, Start Backup Timer, Stop Backup Timer
Backup()
}
Return
Backup() {
Global
FileCreateDir, %OutputFolder%
IfNotExist, %OutputFolder%\7Backup.lnk
{
FileCreateShortcut, %A_ScriptFullPath%, %OutputFolder%\7Backup.lnk,,,,
}
if (UseListFile) {
Runargs := "/c 7z a -t7z """ . OutputFolder . "\" . BackupName . "_" . A_YYYY . "." . A_MM . "." . A_DD . "_" . A_Hour . "." . A_Min . "." . A_Sec . ".7z""" . " -ssw @7Backup_Filelist.txt >>""" . OutputFolder . "\7Backup_" . BackupName . "_" . A_YYYY . "." . A_MM . "." . A_DD . ".log"""
} else {
Runargs := "/c 7z a -t7z """ . OutputFolder . "\" . BackupName . "_" . A_YYYY . "." . A_MM . "." . A_DD . "_" . A_Hour . "." . A_Min . "." . A_Sec . ".7z""" . " -ssw """ . BackupSource . """ >>""" . OutputFolder . "\7Backup_" . BackupName . "_" . A_YYYY . "." . A_MM . "." . A_DD . ".log"""
}
FSize := 0
Loop, %OutputFolder%\*.*, , 1
FSize += %A_LoopFileSize%
FSize := FSize / 1048576
FCount := ComObjCreate("Shell.Application").NameSpace(OutputFolder).Items.Count
FileAppend, `n`n`n------ Backup Started (File count: %FCount%) ------`n, % OutputFolder . "\7Backup_" . BackupName . "_" . A_YYYY . "." . A_MM . "." . A_DD . ".log"
Menu, Tray, Tip, Backup Name: %BackupName%`nFolder Item Count: %FCount%`nFolder Size: %FSize% MB
Run, cmd %Runargs%,, Hide
Process, Wait, 7z.exe, 3
if (ErrorLevel) {
Process, Priority, 7z.exe, Low
}
if (Backingup) {
MSTimer := Timer*1000
if (MSTimer < 10000) {
MSTimer := 10000
}
SetTimer, Backup, %MSTimer%
}
if (DeleteDuplicates) {
SetTimer, DelDupes, 5000
}
Return
}
WizardGuiEscape:
Gui, Wizard: Destroy
Return
ListChecked:
Gui, Submit, NoHide
if (UseListFile) {
GuiControl, Disable, BackupSource
GuiControl, Disable, SeS
GuiControl, Disable, BrS
IfNotExist, 7Backup_Filelist.txt
{
txtisnew := 1
}
FileOpen("7Backup_Filelist.txt", "rw")
if (txtisnew) {
FileAppend, For Example: C:\Games\Game\Saves\*.sav`n, 7Backup_Filelist.txt
FileAppend, Multiple lines and different drives are usable. * means any number of characters and ? means one character.`n, 7Backup_Filelist.txt
FileAppend, Complete Example below:`n, 7Backup_Filelist.txt
FileAppend, C:\Games\Game\League of Legends\Config\*.cfg`n, 7Backup_Filelist.txt
FileAppend, C:\Games\Game\Config\*.ini`n, 7Backup_Filelist.txt
FileAppend, C:\Users\USERNAME\Documents\Game\Saves\Profile1`n, 7Backup_Filelist.txt
FileAppend, YOU MUST DELETE ALL THESE LINES AND LEAVE ONLY YOUR FILELIST, 7Backup_Filelist.txt
}
Run, "7Backup_Filelist.txt"
} else {
GuiControl, Enable, BackupSource
GuiControl, Enable, SeS
GuiControl, Enable, BrS
}
Return
KillBackups() {
Global
SetTimer, Backup, Off
Backingup := 0
GuiControl, , ToggleButton, Start Backup Timer
Return
}
DelDupes:
SetTimer, DelDupes, Off
Process, WaitClose, 7z.exe, 30
if (ErrorLevel == 0) {
FileAppend, `n---`n`nTrying to delete duplicate files...`n, % OutputFolder . "\7Backup_" . BackupName . "_" . A_YYYY . "." . A_MM . "." . A_DD . ".log"
;sfk dupfind -dir "%OutputFolder%" +run "recycle -f $file" -yes
Dupeargs := "/c sfk dupfind -dir """ . OutputFolder . """ +del -yes >>""" . OutputFolder . "\7Backup_" . BackupName . "_" . A_YYYY . "." . A_MM . "." . A_DD . ".log"""
Run, cmd %Dupeargs%,, Hide
}
Return
InfoWindow:
FSize = 0
Loop, %OutputFolder%\*.*, , 1
FSize += %A_LoopFileSize%
FSize := FSize / 1048576
FCount := ComObjCreate("Shell.Application").NameSpace(OutputFolder).Items.Count
MsgBox, 0, 7Backup Folder Info, Backup Name: %BackupName%`nFolder Item Count: %FCount%`nFolder Size: %FSize% MB
Return
Exited:
ExitApp
@Onurtag
Copy link
Author

Onurtag commented Nov 23, 2016

For listfiles '?' implies single character, '*' implies many characters and ending with a '\' means that its a folder.
https://sevenzip.osdn.jp/chm/cmdline/syntax.htm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment