112 lines
4.5 KiB
Markdown
112 lines
4.5 KiB
Markdown
# Go Backup Utility
|
|
|
|
A simple yet powerful command-line utility written in Go for creating and restoring backups of files, directories, and block devices (like disks). It supports standard `gzip` compression for files/disks and `tar.gz` for directories.
|
|
|
|
The key feature of this tool is the ability to stream backups directly over the network, allowing you to create a backup on one machine and restore it on another without storing intermediate files.
|
|
|
|
## Features
|
|
|
|
- **File, Disk, and Directory Backup**: Create backups of individual files, entire directories, or full block devices (e.g., `/dev/sda`).
|
|
- **Network Streaming**: Directly stream backups between two machines over a TCP connection.
|
|
- `create` acts as a network client, sending data.
|
|
- `restore` acts as a network server, receiving data.
|
|
- **Compression**: Uses `gzip` for efficient compression. Directories are archived using `tar` before compression.
|
|
- **Interactive Progress Bars**: Displays a real-time progress bar for both creating and restoring, so you always know the status of the operation.
|
|
- **Cross-Platform**: Written in Go, it can be compiled for Linux, macOS, and Windows.
|
|
|
|
## Installation
|
|
|
|
To build the utility from source, you need to have Go installed on your system.
|
|
|
|
1. Clone the repository (or download the source code).
|
|
2. Navigate to the project directory.
|
|
3. Build the binary:
|
|
```sh
|
|
go build
|
|
```
|
|
4. This will create an executable file named `backup` (or `backup.exe` on Windows) in the current directory. You can move this file to a directory in your system's `PATH` (e.g., `/usr/local/bin`) to make it accessible from anywhere.
|
|
|
|
## Usage
|
|
|
|
The utility has two main commands: `create` and `restore`.
|
|
|
|
### Local Backup and Restore
|
|
|
|
#### 1. Backup a Directory
|
|
|
|
This will create a `tar.gz` archive of the specified source directory.
|
|
|
|
```sh
|
|
./backup create -s /path/to/source/directory -t /path/to/backup.tar.gz
|
|
```
|
|
|
|
#### 2. Restore a Directory
|
|
|
|
This will extract the contents of a `tar.gz` archive into the specified target directory. If the target directory is omitted, it defaults to the current working directory.
|
|
|
|
```sh
|
|
./backup restore -s /path/to/backup.tar.gz -t /path/to/restore/location
|
|
```
|
|
|
|
#### 3. Backup a File or Disk
|
|
|
|
This will create a compressed `gz` file from a source file or a block device.
|
|
|
|
```sh
|
|
# Backup a file
|
|
./backup create -s /path/to/largefile.log -t /path/to/largefile.log.gz
|
|
|
|
# Backup a disk partition (use with caution!)
|
|
./backup create -s /dev/sdb1 -t /path/to/sdb1_backup.img.gz
|
|
```
|
|
|
|
#### 4. Restore a File or Disk
|
|
|
|
This will decompress a `.gz` file to a target file or write the uncompressed data to a block device.
|
|
|
|
```sh
|
|
# Restore a file
|
|
./backup restore -s /path/to/largefile.log.gz -t /path/to/restored_largefile.log
|
|
|
|
# Restore a disk partition (use with extreme caution!)
|
|
./backup restore -s /path/to/sdb1_backup.img.gz -t /dev/sdb1
|
|
```
|
|
|
|
### Network Backup and Restore
|
|
|
|
This is the most powerful feature. It allows you to pipe a backup from a source machine (client) to a target machine (server).
|
|
|
|
**Step 1: On the receiving machine (Server)**
|
|
|
|
First, run the `restore` command. It will start listening on the specified IP address and port, waiting for a connection. Use `0.0.0.0` to listen on all available network interfaces.
|
|
|
|
```sh
|
|
# The server waits for data on port 8080 and will restore it to /mnt/data
|
|
./backup restore -s 0.0.0.0:8080 -t /mnt/data
|
|
```
|
|
> The command will print `Waiting for connection on 0.0.0.0:8080...` and will pause until the client connects.
|
|
|
|
**Step 2: On the sending machine (Client)**
|
|
|
|
Now, run the `create` command, pointing the target (`-t`) to the IP address and port of the server machine.
|
|
|
|
```sh
|
|
# This will back up the /home/user/documents directory and send it directly
|
|
# to the server at 192.168.1.100 on port 8080.
|
|
./backup create -s /home/user/documents -t 192.168.1.100:8080
|
|
```
|
|
|
|
The backup process will start, and you will see a progress bar on both the client and server terminals.
|
|
|
|
## Command-line Flags
|
|
|
|
- `create`:
|
|
- `-s, --source`: The source file, directory, or disk to back up (required).
|
|
- `-t, --target`: The target backup file or network address (`ip:port`) (required).
|
|
- `restore`:
|
|
- `-s, --source`: The source archive file or network address (`ip:port`) to restore from (required).
|
|
- `-t, --target`: The target file, disk, or directory. Required for network sources and file/disk restores. Defaults to the current directory for local archive restores.
|
|
|
|
## License
|
|
|
|
This project is licensed under the **MIT License**. See the `LICENSE` file for more details. |