Última atividade 1746685718

Revisão 27dedc47c1435618276db06cd8a792254dfc5478

overusage_fixer.sh Bruto
1#!/bin/bash
2
3# Function to display the dashboard
4show_dashboard() {
5 clear
6 echo "Overspace Management Script"
7 echo "1. Scan for overspace files"
8 echo "2. Copy overspace files to backup server"
9 echo "3. Remove overspace files"
10 echo "4. File validator / Checker"
11 echo "5. Exit"
12 echo
13 read -p "Enter your choice (1-4): " choice
14 echo
15}
16
17# Function to scan for overspace files
18scan_overspace() {
19 echo "Scanning for files larger than 5GB..."
20 read -p "Enter the directory to scan (default: /home): " scan_dir
21 scan_dir=${scan_dir:-/home}
22 read -p "Enter the output file path (default: overspace_users.txt): " output_file
23 output_file=${output_file:-overspace_users.txt}
24
25 echo "Scanning $scan_dir for files larger than 5GB. Results will be saved in $output_file"
26 find "$scan_dir" -type f -size +5G > "$output_file"
27 echo "Scan complete. Results saved in $output_file"
28 echo "Found $(wc -l < "$output_file") files."
29 read -p "Press Enter to continue..."
30}
31
32# Function to copy overspace files
33copy_overspace() {
34 read -p "Enter the path to the overspace_users.txt file: " input_file
35 if [ ! -f "$input_file" ]; then
36 echo "File $input_file not found. Please run the scan first or provide a valid file."
37 read -p "Press Enter to continue..."
38 return
39 fi
40
41 read -p "Enter the destination server (e.g., user@server): " dest_server
42 read -p "Enter the SSH port number (default: 22): " ssh_port
43 ssh_port=${ssh_port:-22}
44 read -p "Enter the base backup directory on the destination server: " backup_dir
45
46 read -p "Do you want to use an identity file for SSH authentication? (y/n): " use_identity
47 if [ "$use_identity" = "y" ] || [ "$use_identity" = "Y" ]; then
48 read -p "Enter the path to the identity file: " identity_file
49 if [ ! -f "$identity_file" ]; then
50 echo "Identity file not found. Proceeding without it."
51 ssh_options="-p $ssh_port"
52 else
53 ssh_options="-p $ssh_port -i $identity_file"
54 fi
55 else
56 ssh_options="-p $ssh_port"
57 fi
58
59 echo "Copying files to $dest_server:$backup_dir using port $ssh_port"
60 if [ -n "$identity_file" ] && [ -f "$identity_file" ]; then
61 echo "Using identity file: $identity_file"
62 fi
63
64 while IFS= read -r file; do
65 echo "Copying $file..."
66 # Extract the relative path
67 relative_path=${file#/home/}
68 # Create the directory structure on the remote server
69 ssh $ssh_options $dest_server "mkdir -p $backup_dir/$(dirname $relative_path)"
70 # Copy the file preserving the relative path
71 rsync -avz --progress -e "ssh $ssh_options" "$file" "${dest_server}:${backup_dir}/${relative_path}"
72 done < "$input_file"
73
74 echo "Copy complete."
75 read -p "Press Enter to continue..."
76}
77
78# Function to remove overspace files
79remove_overspace() {
80 read -p "Enter the path to the overspace_users.txt file (default: overspace_users.txt): " input_file
81 input_file=${input_file:-overspace_users.txt}
82
83 if [ ! -f "$input_file" ]; then
84 echo "File $input_file not found. Please run the scan first or provide a valid file."
85 read -p "Press Enter to continue..."
86 return
87 fi
88
89 echo "The following files will be moved to trash:"
90 cat "$input_file"
91 echo
92
93 read -p "Are you sure you want to move these files to trash? (y/n): " confirm
94
95 if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
96 echo "Moving overspace files to trash..."
97 while IFS= read -r file; do
98 echo "Trashing $file"
99 trash "$file"
100 done < "$input_file"
101 echo "Removal complete. Files have been moved to trash."
102 else
103 echo "Operation cancelled."
104 fi
105
106 read -p "Press Enter to continue..."
107}
108
109
110# Function to check file size and location
111check_file() {
112 read -p "Enter the path to the overspace_users.txt file (default: overspace_users.txt): " input_file
113 input_file=${input_file:-overspace_users.txt}
114
115 if [ ! -f "$input_file" ]; then
116 echo "File $input_file not found. Please run the scan first or provide a valid file."
117 read -p "Press Enter to continue..."
118 return
119 fi
120
121 echo "Checking files listed in $input_file:"
122 echo "--------------------------------------"
123
124 while IFS= read -r file_path; do
125 if [ -f "$file_path" ]; then
126 size=$(du -sh "$file_path" | cut -f1)
127 location=$(dirname "$file_path")
128 echo "File: $file_path"
129 echo "Size: $size"
130 echo "Location: $location"
131 else
132 echo "File not found: $file_path"
133 fi
134 echo "--------------------------------------"
135 done < "$input_file"
136
137 read -p "Press Enter to continue..."
138}
139
140# Main loop
141while true; do
142 show_dashboard
143
144 case $choice in
145 1) scan_overspace ;;
146 2) copy_overspace ;;
147 3) remove_overspace ;;
148 4) check_file ;;
149 5) echo "Exiting..."; exit 0 ;;
150 *) echo "Invalid choice. Please try again."; read -p "Press Enter to continue..." ;;
151 esac
152done