mars

package module
v0.0.0-...-02f65b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

README

Mars

Overview

Mars is a tool for backing up multiple MySQL databases with multiples options. The backups are outputted as a .tar.gz and are stored locally, there is also support for retention in days/weeks/months

Usage

As a Libarary

See main.go in cmd

As a command

$ go run mars.go -help
  -hostname string
    	Hostname of the mysql server to connect to (default "localhost")
  -bind string
    	Port of the mysql server to connect to (default "3306")
  -password string
    	password of the mysql server to connect to (default "1234")
  -username string
    	username of the mysql server to connect to (default "root")
  -additionals string
    	Additional parameters that will be appended to mysqldump command
  -tablethreshold int
    	Do not split mysqldumps, if rowcount of table is less than dbthreshold value for table (default 5000000)      
  -batchsize int
    	Split mysqldumps in order to get each file contains batchsize number of records (default 1000000)
  -databases string
    	List of databases as comma seperated values to dump. OBS: If not specified, --all-databases is the default (default "--all-databases")
  -dbthreshold int
    	Do not split mysqldumps, if total rowcount of tables in database is less than dbthreshold value for whole database (default 10000000)
  -excluded-databases string
    	List of databases excluded to be excluded. OBS: Only valid if -databases is not specified
  -forcesplit
    	Split schema and data dumps even if total rowcount of tables in database is less than dbthreshold value. if false one dump file will be created
  -mysqldump-path string
    	Absolute path for mysqldump executable. (default "/usr/bin/mysqldump")
  -output-dir string
    	Default is the value of os.Getwd(). The backup files will be placed to output-dir {DATE/{DATABASE_NAME}/{DATABASE_NAME}_{TABLENAME|SCHEMA|DATA|ALL}_{TIMESTAMP}.sql
  -daily-rotation int
    	Number of days of retention (default 5)   	
  -weekly-rotation int
    	Number of weeks of retention (default 2)
  -monthly-rotation int
    	Number of months of retention (default 1) 
  -verbosity int
    	0 = only errors, 1 = important things, 2 = all (default 2)      
  -test
    	test
Rotation folders structure

mysqldump-path / daily|weekly|monthly / XXXX-XX-XX / {DATABASE_NAME}-XXXX-XX-XX / {DATABASE_NAME}{TABLENAME|SCHEMA|DATA|ALL}{TIMESTAMP}.tar.gz

Example

Running a backup of only one database:

$go run mars.go -username "root" -password "123456" -databases "mysql"

Running with parameters
{
	"HostName": "localhost",
	"Bind": "3306",
	"UserName": "root",
	"Password": "123456",
	"Databases": [
		"mysql"
	],
	"ExcludedDatabases": [],
	"DatabaseRowCountTreshold": 10000000,
	"TableRowCountTreshold": 5000000,
	"BatchSize": 1000000,
	"ForceSplit": false,
	"AdditionalMySQLDumpArgs": "",
	"Verbosity": 2,
	"MySQLDumpPath": "/usr/bin/mysqldump",
	"OutputDirectory": "/home/mauro/Downloads/mysql-dump-goland",
	"DefaultsProvidedByUser": true,
	"ExecutionStartDate": "2017-08-05T22:39:26.473773337-04:00",
	"DailyRotation": 5,
	"WeeklyRotation": 2,
	"MonthlyRotation": 1,
}
Running on operating system : linux
Processing Database : mysql
Getting tables for database : mysql
30 tables retrived : mysql
options.ForceSplit (false) && totalRowCount (2102) <= options.DatabaseRowCountTreshold (10000000)
Generating single file backup : mysql
mysqldump is being executed with parameters : -hlocalhost -uroot -p1234 -r/home/mauro/Downloads/mysql-dump-goland/daily/2017-08-05/mysql-2017-08-05/mysql_ALL_20170805.sql mysql
mysqldump output is : 
Compressing table file : /home/mauro/Downloads/mysql-dump-goland/daily/2017-08-05/mysql-2017-08-05/mysql_ALL_20170805.sql
Single file backup successfull : mysql
Processing done for database : mysql

Documentation

Index

Constants

View Source
const (
	// Info messages
	Info = 1 << iota // a == 1 (iota has been reset)

	// Warning Messages
	Warning = 1 << iota // b == 2

	// Error Messages
	Error = 1 << iota // c == 4
)

Variables

This section is empty.

Functions

func BackupRotation

func BackupRotation(options Options)

BackupRotation execute a rotation of file, daily,weekly and monthly

func Compress

func Compress(tw *tar.Writer, path string) error

Compress compresses files into tar.gz file

func CopyDir

func CopyDir(src string, dst string) (err error)

CopyDir recursively copies a directory tree, attempting to preserve permissions. Source directory must exist, destination directory must *not* exist. Symlinks are ignored and skipped.

func CopyFile

func CopyFile(src, dst string) (err error)

CopyFile copies the contents of the file named src to the file named by dst. The file will be created if it does not already exist. If the destination file exists, all it's contents will be replaced by the contents of the source file. The file mode will be copied from the source and the copied data is synced/flushed to stable storage.

func GenerateSchemaBackup

func GenerateSchemaBackup(options Options, db string) error

func GenerateSingleFileBackup

func GenerateSingleFileBackup(options Options, db string) error

func GenerateSingleFileDataBackup

func GenerateSingleFileDataBackup(options Options, db string) error

func GenerateTableBackup

func GenerateTableBackup(options Options, db string, table Table) error

func GetDatabaseList

func GetDatabaseList(hostname string, bind string, username string, password string, verbosity int) []string

GetDatabaseList retrives list of databases on mysql

func GetTotalRowCount

func GetTotalRowCount(tables []Table) int

func ListDirs

func ListDirs(rootpath string) []string

ListDirs give a Array of folders in a given path

func PrintMessage

func PrintMessage(message string, verbosity int, messageType int)

func WriteToFile

func WriteToFile(filePath string, msg string)

WriteToFile create a file and writes a specified msg to it

Types

type Options

type Options struct {
	HostName                 string
	Bind                     string
	UserName                 string
	Password                 string
	Databases                []string
	ExcludedDatabases        []string
	DatabaseRowCountTreshold int
	TableRowCountTreshold    int
	BatchSize                int
	ForceSplit               bool
	AdditionalMySQLDumpArgs  string
	Verbosity                int
	MySQLDumpPath            string
	OutputDirectory          string
	DefaultsProvidedByUser   bool
	ExecutionStartDate       time.Time
	DailyRotation            int
	WeeklyRotation           int
	MonthlyRotation          int
}

Options model for commandline arguments

func GetOptions

func GetOptions() *Options

GetOptions creates Options type from Commandline arguments

func NewOptions

func NewOptions(hostname string, bind string, username string, password string, databases string, excludeddatabases string, databasetreshold int, tablethreshold int, batchsize int, forcesplit bool, additionals string, verbosity int, mysqldumppath string, outputDirectory string, defaultsProvidedByUser bool, dailyrotation int, weeklyrotation int, monthlyrotation int) *Options

NewOptions returns a new Options instance.

type Table

type Table struct {
	TableName string
	RowCount  int
}

Table model struct for table metadata

func GetTables

func GetTables(hostname string, bind string, username string, password string, database string, verbosity int) []Table

GetTables retrives list of tables with rowcounts

func NewTable

func NewTable(tableName string, rowCount int) *Table

NewTable returns a new Table instance.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL