-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzfsonusb
executable file
·171 lines (164 loc) · 5.94 KB
/
zfsonusb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# License: MIT
# Copyright (c) 2024 Salih Emin - SynergOps
# Origin: https://github.com/synergops/.zfsonusb
# Load configuration from external file
# shellcheck source=/dev/null
CONFIG_FILE=~/.zfsonusb.conf
if [[ -f "$CONFIG_FILE" ]]; then
source "$CONFIG_FILE"
else
echo "Configuration file $CONFIG_FILE not found!"
exit 1
fi
VERSION="24.09.21"
# Function to list available disks in /dev/disk/by-id
list_disks() {
echo -e "Listing available disks in /dev/disk/by-id:"
for n in /dev/disk/by-id/*; do
if [[ "$n" == *usb* ]]; then
# Get the actual device (follow the symlink)
DEVICE=$(readlink -f "$n")
echo "$n ... points to -> $DEVICE"
fi
done
}
# Function to import the ZFS pool
import_pool() {
IMPORT_CMD="sudo zpool import"
for DISK in "${DISKS_ARRAY[@]}"; do
IMPORT_CMD="$IMPORT_CMD -d $DISK"
done
IMPORT_CMD="$IMPORT_CMD $ZFS_POOL_NAME -N"
echo "Importing ZFS pool $ZFS_POOL_NAME"
eval "$IMPORT_CMD"
sleep 1
echo "Mounting non-encrypted ZFS datasets:"
sudo zfs mount -a
sleep 1
# If you don't use NFS shares and you get an error about
# "failed to lock /etc/exports.d/zfs.exports"
# run "systemctl disable --now zfs-share.service"
echo "----------------------------------------"
echo "ZFS datasets mounted:"
echo "----------------------------------------"
sudo zfs get mounted
echo "----------------------------------------"
sleep 1
echo "ZFS pool status:"
echo "----------------------------------------"
sudo zpool status
sleep 1
echo "----------------------------------------"
echo "ZFS pool capacity status:"
echo "----------------------------------------"
sudo zpool list
}
# Function to export the ZFS pool and power off external disks
export_pool() {
echo "Syncing filesystems..."
sudo sync
sleep 1
echo "Unmounting ZFS datasets..."
sudo zfs unmount -a
sleep 1
echo "Exporting ZFS pool $ZFS_POOL_NAME ..."
sudo zpool export "$ZFS_POOL_NAME"
sleep 1
# Prepare the udisksctl power-off command
POWER_OFF_CMD="sudo udisksctl power-off"
if command -v udisksctl &> /dev/null; then
for DISK in "${DISKS_ARRAY[@]}"; do
POWER_OFF_CMD="$POWER_OFF_CMD -b $DISK"
done
# Execute the udisksctl power-off command
echo "Powering off external disks..."
eval "$POWER_OFF_CMD"
sleep 1
else
echo "udisksctl was not found in your system. Please install it or you have to manually poweroff your external disks"
fi
}
backup_to_pool() {
if command -v rsync &> /dev/null; then
echo "Backing up $BACKUP_SOURCE to $BACKUP_DESTINATION"
echo "Backup will log in file: /tmp/zfsonusb_backup.log"
echo "Backup will exclude files in file: $BACKUP_EXCLUDE"
sleep 2
rsync --archive -hh --log-file=/tmp/zfsonusb_backup.log --delete-excluded --partial --info=stats1,progress2 --modify-window=1 --exclude-from="$BACKUP_EXCLUDE" "$BACKUP_SOURCE" "$BACKUP_DESTINATION"
else
echo "rsync was not found in your system. Please install it to use backup feature"
fi
}
# Function to display help with detailed steps
show_help() {
echo "zfsonusb v$VERSION"
echo "Usage:"
echo " zfsonusb {import|export|list|backup|help}"
echo ""
echo "Steps to use the script:"
echo " 1. List available disks in /dev/disk/by-id:"
echo " zfsonusb list"
echo ""
echo " 2. After identifying the disks, edit the 'zfsonusb.conf' file"
echo " to include the disk paths under the DISKS array."
echo ""
echo " Example 'zfsonusb.conf':"
echo ""
echo " ZFS_POOL_NAME=\"zones\""
echo " DISKS=("
echo " \"/dev/disk/by-id/usb-ST950042_0AS_00A1234567C4-0:0-part1\""
echo " \"/dev/disk/by-id/usb-TOSHIBA_MQ01ABF050_00A1234567C4-0:1-part1\""
echo " )"
echo ""
echo " Set the \"BACKUP_SOURCE\" and \"BACKUP_DESTINATION\" in the \".zfsonusb.conf\" file to the desired values."
echo " Edit the \".rsync-home-excludes.txt\" file to exclude the files and directories that you don't want to backup."
echo " All lines starting with a # are ignored by rsync, i.e. those directories will be backed up."
echo ""
echo " 3. After configuring the 'zfsonusb.conf' file, you can:"
echo " - Import the ZFS pool: zfsonusb import"
echo " - Export the ZFS pool and power off disks: zfsonusb export"
echo ""
echo "Options:"
echo " import : Import the ZFS pool and mount datasets"
echo " export : Unmount the ZFS pool and power off external disks"
echo " list : List available disks in /dev/disk/by-id"
echo " backup : Backup data to the ZFS pool"
echo " help : Display this help message"
echo ""
echo "Backup Usage:"
echo " The backup feature uses rsync to backup data to the ZFS pool."
echo " If you need this feature, configure the following variables in $CONFIG_FILE:"
echo " BACKUP_SOURCE: The source directory to backup"
echo " BACKUP_DESTINATION: The destination directory in the ZFS pool"
echo " BACKUP_EXCLUDE: Path to a file containing rsync exclude patterns"
echo ""
echo " The backup will log messages in a temporary file at: /tmp/zfsonusb_backup.log"
echo " CAUTION: Make sure you understand the rsync backup process and the configuration before running the backup."
echo " The backup will exclude files in $BACKUP_EXCLUDE"
echo " and delete files in $BACKUP_DESTINATION that no longer exist in $BACKUP_SOURCE"
echo ""
}
# Command-line argument parsing
case "$1" in
import)
import_pool
;;
export)
export_pool
;;
list)
list_disks
;;
backup)
backup_to_pool
;;
help|--help|-h)
show_help
;;
*)
echo "Error: Invalid option"
show_help
exit 1
;;
esac