Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
added systemLog save with date, removed function with dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
bstnbuck committed Jun 27, 2020
1 parent 591eeb9 commit ee3f256
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[![Build Status](https://travis-ci.org/bstnbuck/MST.svg?branch=master)](https://travis-ci.org/bstnbuck/MST)
# MST (Move & Symlink Tool)

MST searches for large or old files and moves them to another drive (e.g. external hard disk) to avoid filling up your own

>**Not all functions are implemented yet! Right now MST is in an alpha stage.
Attention! It is strongly discouraged to run with sudo rights or run with system / program files**

Expand Down Expand Up @@ -39,10 +41,11 @@
- -src (required) -> Specify source path (like: "/var/www/newPath/")
- -help -h -> see this help
- -log -> turn logging on (default = false)
- -save -> save system log file with actual date(default = false)
- -a -> analyze all files that could be archived (default = false)
- -depth -> only in combination with -m 2, depth to search dir's (-1 = all) (default = 3)
- reset -> reset all changes of last run, optional with -log
- remove -> remove all changes of last run, optional with -log
- -reset -> reset all changes of last run, optional with -log
- -remove -> remove all changes of last run, optional with -log
- -filename -> choose other systemLog file for -reset or -remove
***
##### Status-Codes
0=Success; 1=Failure; 2=Info; 3=Modified; 4=User Interrupt; 9=Not implemented
Expand All @@ -52,12 +55,11 @@
- -m 1 -days 360 -src "/test/testdrive/" -log -a (MST analyze within elapsed 360 days and search in directory and log all commands)
- (-m 0) -size 2 -src "/test/testDrive/" -dest "/test/testPaste/" -log (MST with filesize = 2 MB and logging)
- -h (or -help) (prints help)
- -reset -log
- -reset -filename "systemLog2020-6-27-12-11-19.log" -log


### Information
- MST only runs with Linux OS

### The following is still being implemented
* Move whole directory to another place and make optional tar or zip file.
* Maybe Windows support.
37 changes: 23 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@ import (
"log"
"os"
"runtime"
"time"
)

var logVar bool //log variable is used in message-function, dir for archive whole dirs
var logVar, saveLog bool //log variable is used in message-function, dir for archive whole dirs
var logname = "mstLog.log"

var systemLog = "systemLog.log"
var whichOS = runtime.GOOS

func main() {
fmt.Println("\nWelcome to MST (Move & Symlink Tool)")
fmt.Println("")

var m, depth int
var m int
var size, days int64
var dest, src string
var dest, src, sysLogFileName string
var analyze bool

help := flag.Bool("help", false, "help")
h := flag.Bool("h", false, "help")
flag.IntVar(&m, "m", 0, "mode 0 = biggest file; 1 = days, 2 = dir size")
flag.IntVar(&m, "m", 0, "mode 0 = biggest file; 1 = days")
flag.Int64Var(&size, "size", 0, "file size in megabyte")
flag.Int64Var(&days, "days", 0, "date in days")
flag.StringVar(&dest, "dest", "", "destination (/which/folder/)")
flag.StringVar(&src, "src", "", "source (/which/folder/)")
flag.BoolVar(&logVar, "log", false, "logging (bool)")
flag.IntVar(&depth, "depth", 3, "archive depth")
flag.BoolVar(&saveLog, "save", false, "logging with current date (bool)")
flag.BoolVar(&analyze, "a", false, "analyze all files or dir's and make output")
flag.StringVar(&sysLogFileName, "filename", "systemLog.log", "other systemLog name for reset and remove")

//new flags to reset last execution and remove all moved files and symlinks
reset := flag.Bool("reset", false, "reset last move & symlink execution")
Expand All @@ -65,13 +68,13 @@ func main() {
printHelp()
return
case *reset:
err := runReset()
err := runReset(sysLogFileName)
if err != nil {
log.Fatal(err)
}
return
case *remove:
err := runRemove()
err := runRemove(sysLogFileName)
if err != nil {
log.Fatal(err)
}
Expand All @@ -87,6 +90,19 @@ func main() {
log.Fatal(err)
}

//rename logFile to filename + actual date
if saveLog {
defer func() {
y, m, d := time.Now().Date()
h, min, s := time.Now().Clock()
saveLogName := fmt.Sprintf("systemLog%d-%d-%d-%d-%d-%d.log", y, m, d, h, min, s)
err := os.Rename(systemLog, saveLogName)
if err != nil {
log.Fatal(err)
}
}()
}

if whichOS == "linux" || whichOS == "windows" {

//#####################################################
Expand Down Expand Up @@ -115,9 +131,6 @@ func main() {
}
message(0, "Files successfully analyzed\n")
return
} else if analyze && m == 2 && src[len(src)-1:] == "/" && depth > 0 {
message(9, "During now, only files are allowed")
return
} else if analyze {
printHelp()
fmt.Println("[ERROR] Bad arguments!")
Expand Down Expand Up @@ -177,10 +190,6 @@ func main() {
fmt.Println(err)
return
}
} else if m == 2 && dest[len(dest)-1:] == "/" && src[len(src)-1:] == "/" && dest[0] == '/' && src[0] == '/' && size == 0 && analyze == false && (depth >= 0 || depth == -1) {
message(9, "During now, only files are allowed")
return

//if arguments check failed, print to user
} else {
printHelp()
Expand Down
8 changes: 4 additions & 4 deletions removeAndReset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
)

// reset last program execution
func runReset() error {
func runReset(sysLogFileName string) error {
if whichOS == "linux" {
var accept string
fmt.Printf("This will reset all made changes from last run. Run cannot be cancelled!\nconfirm (y/n)\n\n")
_, err := fmt.Scanln(&accept)
if accept == "y" && err == nil {
sysLogFile, err := os.OpenFile(systemLog, os.O_RDWR, 0755)
sysLogFile, err := os.OpenFile(sysLogFileName, os.O_RDWR, 0755)
if err != nil {
return err
}
Expand Down Expand Up @@ -58,14 +58,14 @@ func runReset() error {
}

// remove all made changes
func runRemove() error {
func runRemove(sysLogFileName string) error {

if whichOS == "linux" {
var accept string
fmt.Printf("Remove will now start, cannot be cancelled and undone!\nconfirm (y/n)\n\n")
_, err := fmt.Scanln(&accept)
if accept == "y" && err == nil {
sysLogFile, err := os.OpenFile(systemLog, os.O_RDWR, 0755)
sysLogFile, err := os.OpenFile(sysLogFileName, os.O_RDWR, 0755)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions support_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@ func printHelp() {
" or run with system / program files\n" +
"#**************************************#\n" +
"Arguments: -m -size -days -dest -src -help -h -log -a -depth\n" +
"-m Select running mode 0 = file size (default); 1 = days; 2 = dir size\n" +
"-m Select running mode 0 = file size (default); 1 = days\n" +
"-size Archive files by file size (default 20MB)\n" +
"-days (beta) Archive files by days (last modified) (default 60 days)\n" +
"-dest (required) Specify destination path (like: \"/var/www/newPath/\")\n" +
"-src (required) Specify source path (like: \"/var/www/newPath/\")\n" +
"-help -h see this help\n" +
"-log turn logging on (default = false)\n" +
"-save save system log file with actual date(default = false)\n" +
"-a analyze all files that could be archived (default = false)\n" +
"-depth only in combination with -m 2, depth to search dir's (-1 = all) (default = 3)\n" +
"-reset reset all changes of last run, optional with -log\n" +
"-remove remove all changes of last run, optional with -log\n" +
"-filename choose other systemLog file for -reset or -remove\n" +
"#**************************************#\n" +
"Status-Codes \n" +
"0=Success; 1=Failure; 2=Info; 3=Modified; 4=User Interrupt; 9=Not implemented\n" +
Expand Down

0 comments on commit ee3f256

Please sign in to comment.