52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
|
package database
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"log"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type Backup struct {
|
||
|
Hostname string
|
||
|
ReceivedOn time.Time
|
||
|
}
|
||
|
|
||
|
func ListBackups(dbConn *sql.DB) ([]Backup, error) {
|
||
|
log.Println("listing backups")
|
||
|
|
||
|
rows, err := dbConn.Query(`SELECT hostname, received_on FROM backups;`)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
|
backups := []Backup{}
|
||
|
for rows.Next() {
|
||
|
var backup Backup
|
||
|
err := rows.Scan(&backup.Hostname, &backup.ReceivedOn)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
backups = append(backups, backup)
|
||
|
}
|
||
|
|
||
|
return backups, nil
|
||
|
}
|
||
|
|
||
|
func DeleteOldBackups(dbConn *sql.DB, days int) error {
|
||
|
log.Println("deleting old backups")
|
||
|
|
||
|
duration := time.Duration(days) * 24 * time.Hour
|
||
|
_, err := dbConn.Exec(`DELETE FROM backups WHERE received_on < ?;`, time.Now().Add(-duration))
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func ReceivedBackup(dbConn *sql.DB, hostname string) error {
|
||
|
log.Println("received backup for", hostname)
|
||
|
|
||
|
_, err := dbConn.Exec(`INSERT INTO backups (hostname) VALUES (?);`, hostname)
|
||
|
|
||
|
return err
|
||
|
}
|