Last active
August 29, 2015 14:05
-
-
Save mmstick/01134c72ed972f31329e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "os/exec" | |
import "time" | |
func errorChecker(output *[]byte, err *error, entry *string, elapsedTime time.Duration) { | |
if *err != nil { | |
fmt.Println(fmt.Sprint(*err) + ": " + string(*output)) | |
} else { | |
fmt.Printf("Synchronized %s in %s\n", *entry, elapsedTime) | |
} | |
} | |
// Executes commands then returns done when finished | |
func runBackup(backup *exec.Cmd, entry string, done chan bool) { | |
fmt.Printf("Synchronizing %s...\n", entry) | |
startTime := time.Now() | |
output, err := backup.CombinedOutput() | |
errorChecker(&output, &err, &entry, time.Since(startTime)) | |
done <- true | |
} | |
func main() { | |
rsyncJobs := uint(6) | |
animeArchive := exec.Command("rsync", "-av", "--delete", "/home/mmstick/Videos/Archive", "/home/mmstick/Backup/") | |
animeMovies := exec.Command("rsync", "-av", "--delete", "/home/mmstick/Videos/Movies", "/home/mmstick/Backup/") | |
animeCurrent := exec.Command("rsync", "-av", "--delete", "/home/mmstick/Videos/Current", "/home/mmstick/Backup/") | |
visualNovels := exec.Command("rsync", "-av", "--delete", "/home/mmstick/Games/Visual Novels", "/home/mmstick/Backup/Games/") | |
romArchive := exec.Command("rsync", "-av", "--delete", "/home/mmstick/Games/ROMs", "/home/mmstick/Backup/Games/") | |
musicArchive := exec.Command("rsync", "-av", "--delete", "/home/mmstick/Music", "/home/mmstick/Backup/") | |
// Execute commands in parallel | |
synchronize := make(chan bool, rsyncJobs) | |
go runBackup(animeArchive, "anime archive", synchronize) | |
go runBackup(animeMovies, "anime movies", synchronize) | |
go runBackup(animeCurrent, "anime currently airing", synchronize) | |
go runBackup(visualNovels, "visual novels", synchronize) | |
go runBackup(romArchive, "ROMs", synchronize) | |
go runBackup(musicArchive, "music archive", synchronize) | |
// Wait for commands to finish before exiting the program | |
for jobCount := uint(0); jobCount < rsyncJobs; jobCount++ { | |
<-synchronize | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment