Project

General

Profile

unused.sh

Emmanuel GARETTE, 01/07/2019 11:06 AM

Download (4.68 KB)

 
1
#!/bin/bash
2

    
3
print_usage() {
4
        echo "  -n";
5
        echo "     update all volumes to \"actiononpurge=None\"";
6
        echo "  -t";
7
        echo "     update all volumes to \"actiononpurge=Truncate\"";
8
        echo "  -p";
9
        echo "     prune all volumes ";
10
        echo "  -Dp";
11
        echo "     delete all purged volumes from Bacula catalog"
12
        echo "  -Do";
13
        echo "     delete obsolete volume files from the disk (those not listed in catalog)"
14
        echo "       NOTE: this operation can take several minutes to complete"
15
        echo "  -h";
16
        echo "     print this screen";
17
        echo ""
18
        exit 0
19

    
20
}
21

    
22
if [ $# -lt 1 ]; then
23
        print_usage
24
        exit 3
25
fi
26

    
27
update_volume() {
28

    
29
        BACULA_BATCH="$(mktemp)"
30

    
31
        echo ""
32
        echo "updating all volumes to \"actiononpurge=$1\"..."
33
        echo ""
34

    
35
        cd /mnt/sauvegardes/
36
        ls -1 | while read pool; do
37
                cd /mnt/sauvegardes/$pool
38
                echo "update volume=$pool ActionOnPurge=$1" >> $BACULA_BATCH;
39
        done
40
        bconsole < $BACULA_BATCH | grep $1
41
        rm -f $BACULA_BATCH
42

    
43
}
44

    
45
prune_all() {
46

    
47
        BACULA_BATCH="$(mktemp)"
48

    
49
        echo ""
50
        echo "pruning all volumes and let Bacula mark them as purged once the retention periods are expired..."
51
        echo ""
52

    
53
        cd /mnt/sauvegardes/
54
        ls -1 | while read pool; do
55
                echo "prune volume=$pool" >> $BACULA_BATCH;
56
                echo "yes" >> $BACULA_BATCH;
57
        done
58
        bconsole < $BACULA_BATCH | grep -i "marking it purged"
59
        rm -f $BACULA_BATCH
60
}
61

    
62
delete_obsolete_volumes() {
63

    
64
        VOLUMES_TO_DELETE="$(mktemp)"
65

    
66
        echo ""
67
        echo "searching for obsolete files on disk. These could be some old volumes deleted from catalog manually..."
68
        echo ""
69

    
70
        cd /mnt/sauvegardes/
71
        ls -1 | while read pool; do
72
                if [ "$pool" = "lost+found" ]; then
73
                    continue
74
                fi
75
                echo "list volume=$pool" | bconsole | if grep --quiet "No results to list"; then
76
                        echo "$pool is ready to be deleted"
77
                        echo "/mnt/sauvegardes/$pool" >> $VOLUMES_TO_DELETE
78
                fi
79
        done
80

    
81
        # Check whether we have found some volumes(files) to delete
82
        if [ `wc -l $VOLUMES_TO_DELETE | awk {'print $1'}` -gt 0 ]; then
83

    
84
                echo ""
85
                echo -n "Are you sure you want to delete these volumes(files) from the disk ? (yes|no): "
86
                read response
87

    
88
                if [ "$response" = "yes" ]; then
89
                        cat $VOLUMES_TO_DELETE | while read I; do
90
                                rm -f "$I"
91
                        done
92

    
93
                        echo ""
94
                        echo "DONE: following files were deleted: "
95
                        cat $VOLUMES_TO_DELETE
96
                fi
97

    
98
        else
99
                echo "No volumes to delete found on disk"
100

    
101
        fi
102

    
103
        rm -f $VOLUMES_TO_DELETE
104

    
105
}
106

    
107
delete_purged_volumes() {
108

    
109
        echo ""
110
        echo "searching for all purged volumes to be deleted..."
111
        echo ""
112

    
113
        PURGED_VOLUMES=`echo "list volumes" | bconsole | grep "Purged" | awk {'print $4'}`
114
        echo "$PURGED_VOLUMES"
115

    
116
        echo ""
117
        echo -n "Are you sure you want to delete all these purged volumes from Bacula catalog ? (yes|no): "
118
        read response
119

    
120
        if [ $response = "yes" ]; then
121
                BACULA_BATCH="$(mktemp)"
122
                rm -f /tmp/bacula.log
123
                echo "@output /tmp/bacula.log" > $BACULA_BATCH
124
                echo "$PURGED_VOLUMES" | while read I; do
125
                        echo "delete volume=$I" >> $BACULA_BATCH
126
                        echo "yes" >> $BACULA_BATCH
127
                done
128
                bconsole < $BACULA_BATCH
129
                rm -f $BACULA_BATCH
130
                echo "Done. See the log file /tmp/bacula.log"
131
        fi
132

    
133
}
134

    
135
# Parse parameters
136
while [ $# -gt 0 ]; do
137
    case "$1" in
138
        -h | --help)
139
                print_usage
140
                exit 0
141
                ;;
142
        -p)
143
                shift
144
                prune_all
145
                exit 0
146
                ;;
147
        -t)
148
                shift
149
                update_volume Truncate
150
                exit 0
151
                ;;
152
        -n)
153
                shift
154
                update_volume None
155
                exit 0
156
                ;;
157
        -Do)
158
                shift
159
                delete_obsolete_volumes
160
                exit 0
161
                ;;
162
        -Dp)
163
                shift
164
                delete_purged_volumes
165
                exit 0
166
                ;;
167
        *)  echo "Unknown argument: $1"
168
            print_usage
169
            exit 3
170
            ;;
171
        esac
172
done
173
exit 1
174