Junior — Middle
What measures should be taken to ensure data safety in case of hard drive failure, especially in the context of backups?
sobes.tech AI
Answer from AI
To ensure data safety in case of a hard drive failure, it is important to implement a reliable backup strategy:
- Regular backups: Automate the backup process at a set frequency (daily, weekly).
- Store copies in different locations: Use remote servers, cloud storage, or physically separated media to avoid data loss during local failures.
- Versioning: Keep multiple versions of files to restore data to a specific point in time.
- Recovery testing: Periodically verify that backups can be successfully restored.
Example in Go for creating a file backup:
package main
import (
"io"
"os"
)
func backupFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
return err
}
func main() {
err := backupFile("data.txt", "backup/data_backup.txt")
if err != nil {
panic(err)
}
}
This example demonstrates copying a file to create a backup. In real projects, additional considerations include scheduling, encryption, and backup storage.