-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage-replication-jobs.sh
executable file
·65 lines (51 loc) · 1.64 KB
/
manage-replication-jobs.sh
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
#!/bin/bash
# --- PARAMETERS ---
# - first parameter can be either 'read' or 'update'
# - update will update the jobs and read will save the current job configuration
# --- CONSTANTS ---
SETTINGS_FILE=replication-job-settings.csv;
# --- FUNCTIONS ---
read_jobs () {
# Delete the existing settings
if test -f $SETTINGS_FILE; then
rm $SETTINGS_FILE;
fi
# Create header line in CSV
echo "jobID,schedule,comment" | tee -a $SETTINGS_FILE;
# Save all the current job settings into the settings file
jobIDs=$(pvesr list | awk '{print $1}' | grep -Eo '[1-9][0-9]{2,8}-[0-9]{1,9}');
for jobID in $jobIDs; do
curJobSchedule=$(pvesr read $jobID | jq -r '.schedule');
comment=$(pvesr read $jobID | jq -r '.comment');
echo "$jobID,$curJobSchedule,$comment" | tee -a $SETTINGS_FILE;
done;
}
update_jobs () {
# Read in the CSV file
while IFS="," read -r jobID schedule comment
do
if [ ! -z "$jobID" ]; then
curJobSchedule=$(pvesr read $jobID | jq -r '.schedule');
if [[ "$curJobSchedule" != "$schedule" ]]; then
echo "Updating job ID '$jobID' to schedule of '$schedule'";
pvesr update $jobID --schedule "$schedule" --comment "$comment";
else
echo "Job ID '$jobID' already has the schedule '$schedule'";
fi
fi
done < <(tail -n +2 $SETTINGS_FILE)
}
# -- END FUNCTIONS ---
# Install jq if not detected
if [ -z $(which jq) ]; then
apt install jq -y;
fi
# Determine how script should run
if [[ "$1" == "read" ]]; then
read_jobs
elif [[ "$1" == "update" ]]; then
update_jobs
else
echo "Script parameter was not valid";
echo "Parameter must either be 'read' or 'update'";
fi