Skip to content

Commit aeaf0f6

Browse files
Add service timer
1 parent b61b2ae commit aeaf0f6

File tree

9 files changed

+158
-100
lines changed

9 files changed

+158
-100
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
3+
## 1.1.0
4+
5+
* Removed endless loop for scheduled backups
6+
* Added systemd timer for scheduled backups
7+
* Fixed `backup` fuction not recognizing errors
8+
9+
## 1.0.1
10+
11+
* Fixed variable name in `backup_config_folders` function
12+
13+
## 1.0.0
14+
15+
* Initial release

backup.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ else
1313
exit 1
1414
fi
1515

16-
init_schedule
17-
backup
18-
19-
exit 0
16+
if backup
17+
then
18+
exit 0
19+
else
20+
exit 1
21+
fi
File renamed without changes.

kgb.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/bin/bash
22

3-
VERSION="v1.0.0"
3+
VERSION="v1.1.0"
44
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit; pwd -P )"
55
UNSAVED_CHANGES=0
66

77
### Load all functions
8-
for SCRIPT in "${SCRIPTPATH}/lib/functions/"*.sh
8+
for FN_SCRIPT in "${SCRIPTPATH}/lib/functions/"*.sh
99
do
10-
source "${SCRIPT}"
10+
source "${FN_SCRIPT}"
1111
done
12-
for SCRIPT in "${SCRIPTPATH}/lib/ui/"*.sh
12+
for UI_SCRIPT in "${SCRIPTPATH}/lib/ui/"*.sh
1313
do
14-
source "${SCRIPT}"
14+
source "${UI_SCRIPT}"
1515
done
1616

1717
### Load config

lib/functions/dialogs.sh

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ backup_dialog() {
66
while true
77
do
88
warning_msg "You have config changes pending!"
9+
warning_msg "Not saving will cause the pending changes to be lost!"
910
read -p "$(echo -e "${CYAN}Save changes now? ${NC}")" SAVE_CHANGES
1011
case $SAVE_CHANGES in
1112
y|Y)
@@ -22,44 +23,12 @@ backup_dialog() {
2223
esac
2324
done
2425
fi
25-
if [[ $SCHEDULED_BACKUPS -eq 1 ]]
26-
then
27-
warning_msg "You have scheduled backups enabled!"
28-
info_msg "This action will turn off scheduled backups temporarily"
29-
info_msg "This causes all your pending changes to be saved"
30-
while true
31-
do
32-
read -p "$(echo -e "${CYAN}Save changes now? ${NC}")" SAVE_CHANGES
33-
case $SAVE_CHANGES in
34-
y|Y)
35-
SCHEDULED_BACKUPS=0
36-
REVERT_SCHEDULE=1
37-
save_config
38-
break
39-
;;
40-
n|N)
41-
info_msg "This would lead to an infinite loop"
42-
error_msg "Aborting"
43-
return 1
44-
;;
45-
*)
46-
deny_action
47-
;;
48-
esac
49-
done
50-
fi
5126
if "${SCRIPTPATH}/backup.sh"
5227
then
5328
success_msg "Backup succeeded"
5429
else
5530
error_msg "Backup failed! Please check the log"
5631
fi
57-
if [[ $REVERT_SCHEDULE -eq 1 ]]
58-
then
59-
SCHEDULED_BACKUPS=1
60-
REVERT_SCHEDULE=0
61-
save_config
62-
fi
6332
}
6433

6534
update_dialog() {
@@ -90,7 +59,7 @@ install_dialog() {
9059
n|N)
9160
while true
9261
do
93-
read -p "$(echo -e "${CYAN}Ignore config changes? ${NC}")" IGNORE
62+
read -p "$(echo -e "${CYAN}Ignore config changes and cancel install? ${NC}")" IGNORE
9463
case $IGNORE in
9564
y|Y)
9665
success_msg "Ignoring config changes"
@@ -134,6 +103,9 @@ uninstall_dialog() {
134103
info_msg "Removing backup service"
135104
sudo systemctl disable kgb.service
136105
sudo rm /etc/systemd/system/kgb.service
106+
info_msg "Removing backup schedule"
107+
sudo systemctl disable kgb.timer
108+
sudo rm /etc/systemd/system/kgb.timer
137109
info_msg "Deleting config"
138110
rm -r "$HOME/.config/kgb.cfg"
139111
info_msg "Deleting scripts"

lib/functions/github_functions.sh

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
11
#!/bin/bash
22

3-
init_schedule() {
4-
if [[ -z ${TIME_UNIT+x} ]]
5-
then
6-
log_msg "Something went wrong during schedule initialization"
7-
exit 1
8-
else
9-
case $TIME_UNIT in
10-
h)
11-
MULTIPLIER=3600
12-
;;
13-
d)
14-
MULTIPLIER=86400
15-
;;
16-
m)
17-
MULTIPLIER=2592000
18-
;;
19-
*)
20-
log_msg "Misconfiguration in backup interval"
21-
log_msg "Please specify a valid timespan"
22-
log_msg "Available are h(ours), d(ays) and m(onths)"
23-
log_msg "Falling back to daily backup"
24-
MULTIPLIEER=86400
25-
;;
26-
esac
27-
PAUSE=$(( BACKUP_INTERVAL * MULTIPLIER ))
28-
fi
29-
}
30-
313
log_rotation() {
32-
DEL=$(( ( $(date '+%s') - $(date -d "${LOG_RETENTION} months ago" '+%s') ) / 86400 ))
4+
local DEL=$(( ( $(date '+%s') - $(date -d "${LOG_RETENTION} months ago" '+%s') ) / 86400 ))
335
case $LOG_ROTATION in
346
0)
357
log_msg "Log rotation is disabled"
@@ -47,6 +19,7 @@ log_rotation() {
4719
}
4820

4921
backup_config_folders() {
22+
local ERROR=0
5023
for FOLDER in "${CONFIG_FOLDER_LIST[@]}"
5124
do
5225
log_msg "Backing ${FOLDER} up to GitHub"
@@ -55,32 +28,46 @@ backup_config_folders() {
5528
log_msg "Committing to GitHub repository"
5629
git -C "${FOLDER}" commit -m "backup $(date +%F)" | tee -a "${HOME}/kgb-log/$(date +%F).log"
5730
log_msg "Pushing to GitHub repository"
58-
git -C "${FOLDER}" push -u origin "${GITHUB_BRANCH}" | tee -a "${HOME}/kgb-log/$(date +%F).log"
31+
if git -C "${FOLDER}" push -u origin "${GITHUB_BRANCH}" | tee -a "${HOME}/kgb-log/$(date +%F).log"
32+
then
33+
log_msg "${FOLDER} backed up"
34+
else
35+
log_msg "${FOLDER} backup failed"
36+
ERROR=$(( ERROR + 1 ))
37+
fi
5938
done
39+
if [[ $ERROR -ne 0 ]]
40+
then
41+
return 1
42+
else
43+
return 0
44+
fi
6045
}
6146

6247
backup() {
63-
while true
64-
do
65-
case $GIT in
66-
0)
67-
log_msg "Backups are disabled"
68-
;;
69-
1)
70-
backup_config_folders
71-
;;
72-
*)
73-
log_msg "No valid backup configuration"
74-
log_msg "Please check the config file!"
75-
exit 1
76-
;;
77-
esac
78-
log_rotation
79-
if [[ $SCHEDULED_BACKUPS -eq 0 ]]
80-
then
81-
break
82-
else
83-
sleep $PAUSE
84-
fi
85-
done
48+
case $GIT in
49+
0)
50+
log_msg "Backups are disabled"
51+
;;
52+
1)
53+
if backup_config_folders
54+
then
55+
local ERROR=0
56+
else
57+
local ERROR=1
58+
fi
59+
;;
60+
*)
61+
log_msg "No valid backup configuration"
62+
log_msg "Please check the config file!"
63+
exit 1
64+
;;
65+
esac
66+
log_rotation
67+
if [[ $ERROR -ne 0 ]]
68+
then
69+
return 1
70+
else
71+
return 0
72+
fi
8673
}

lib/functions/install_functions.sh

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,43 @@ setup_ssh() {
108108
return 0
109109
}
110110

111+
init_schedule() {
112+
if [[ -z ${TIME_UNIT+x} ]]
113+
then
114+
log_msg "Something went wrong during schedule initialization"
115+
exit 1
116+
else
117+
if [[ $SCHEDULED_BACKUPS -eq 1 ]]
118+
then
119+
case $TIME_UNIT in
120+
h)
121+
INTERVAL="OnCalendar=*-*-* 01/${BACKUP_INTERVAL}:00:00"
122+
PERSISTENT="true"
123+
;;
124+
d)
125+
INTERVAL="OnCalendar=*-*-01/${BACKUP_INTERVAL} 00:00:00"
126+
PERSISTENT="true"
127+
;;
128+
m)
129+
INTERVAL="OnCalendar=*-01/${BACKUP_INTERVAL}-* 00:00:00"
130+
PERSISTENT="true"
131+
;;
132+
*)
133+
log_msg "Misconfiguration in backup interval"
134+
log_msg "Please specify a valid timespan"
135+
log_msg "Available are h(ours), d(ays) and m(onths)"
136+
log_msg "Falling back to daily backup"
137+
INTERVAL="OnCalendar=*-*-*/${BACKUP_INTERVAL} 00:00:00"
138+
PERSISTENT="true"
139+
;;
140+
esac
141+
else
142+
INTERVAL="OnBootSec=3min"
143+
PERSISTENT="false"
144+
fi
145+
fi
146+
}
147+
111148
install() {
112149
success_msg "Installing"
113150
chmod +x "${SCRIPTPATH}/"*.sh
@@ -116,7 +153,7 @@ install() {
116153
then
117154
error_msg "Git is not installed"
118155
info_msg "Installing..."
119-
"${SCRIPTPATH}/install-git.sh"
156+
install_git
120157
else
121158
GIT_VERSION=$(git --version | cut -b 13- | sed -e 's/\.//g')
122159
if [[ $GIT_VERSION -lt 2280 ]]
@@ -138,6 +175,7 @@ install() {
138175
mkdir -p "$HOME/kgb-log"
139176
git config --global user.email "$GITHUB_MAIL"
140177
git config --global user.name "$GITHUB_USER"
178+
git config --global init.defaultBranch "$GITHUB_BRANCH"
141179
for i in ${!REPO_LIST[@]}
142180
do
143181
if [[ -d "${CONFIG_FOLDER_LIST[$i]}/.git" ]]
@@ -164,9 +202,32 @@ install() {
164202
echo "$SERVICE_FILE" >> "${SCRIPTPATH}/kgb.service"
165203
sudo mv "${SCRIPTPATH}/kgb.service" /etc/systemd/system/kgb.service
166204
sudo chown root:root /etc/systemd/system/kgb.service
205+
sudo chmod 644 /etc/systemd/system/kgb.service
167206
sudo systemctl enable kgb.service
168-
sudo systemctl start kgb.service
207+
# sudo systemctl start kgb.service
208+
fi
209+
if [[ -f /etc/systemd/system/kgb.timer ]]
210+
then
211+
info_msg "Schedule was already set up"
212+
info_msg "Disabling the schedule temporarily"
213+
sudo systemctl stop kgb.timer
214+
sudo systemctl disable kgb.timer
215+
info_msg "Updating the schedule"
216+
else
217+
info_msg "Setting up the schedule"
169218
fi
219+
init_schedule
220+
echo "$SERVICE_TIMER" >> "${SCRIPTPATH}/kgb.timer"
221+
sleep 1
222+
sed -i "s|replace_interval|${INTERVAL}|g" "${SCRIPTPATH}/kgb.timer"
223+
sed -i "s|replace_persist|${PERSISTENT}|g" "${SCRIPTPATH}/kgb.timer"
224+
sudo mv "${SCRIPTPATH}/kgb.timer" /etc/systemd/system/kgb.timer
225+
sudo chown root:root /etc/systemd/system/kgb.timer
226+
sudo chmod 644 /etc/systemd/system/kgb.timer
227+
info_msg "Enabling the schedule"
228+
sudo systemctl daemon-reload
229+
sudo systemctl enable kgb.timer
230+
sudo systemctl start kgb.timer
170231
success_msg "Installation complete"
171232
read -p "$(echo -e "${CYAN}Press enter to continue ${NC}")" CONTINUE
172233
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,27 @@ SERVICE_FILE=$(cat <<- EOF
44
[Unit]
55
Description=Klipper config backup service
66
Documentation="https://github.com/Low-Frequency/klipper_backup_script"
7-
After=network-online.target
8-
Requires=network-online.target
97
108
[Service]
119
Type=simple
1210
User=$(echo $USER)
1311
ExecStart=$(echo $SCRIPTPATH)/backup.sh
12+
EOF
13+
)
14+
15+
SERVICE_TIMER=$(cat <<- EOF
16+
[Unit]
17+
Description=Timer for kgb.service
18+
Documentation="https://github.com/Low-Frequency/klipper_backup_script"
19+
After=network-online.target
20+
Requires=network-online.target
21+
22+
[Timer]
23+
replace_interval
24+
Unit=kgb.service
25+
Persistent=replace_persist
1426
1527
[Install]
16-
WantedBy=multi-user.target
28+
WantedBy=multi-user.target timers.target
1729
EOF
1830
)

lib/ui/backup_schedule_ui.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ backup_schedule_ui() {
2020
if [[ $BACKUP_INTERVAL -eq 1 ]]
2121
then
2222
SCHEDULE_STATUS="${BACKUP_INTERVAL} hour "
23+
elif [[ $BACKUP_INTERVAL -lt 10 ]]
24+
then
25+
SCHEDULE_STATUS="${BACKUP_INTERVAL} hours "
2326
else
2427
SCHEDULE_STATUS="${BACKUP_INTERVAL} hours "
2528
fi
@@ -32,6 +35,9 @@ backup_schedule_ui() {
3235
if [[ $BACKUP_INTERVAL -eq 1 ]]
3336
then
3437
SCHEDULE_STATUS="${BACKUP_INTERVAL} day "
38+
elif [[ $BACKUP_INTERVAL -lt 10 ]]
39+
then
40+
SCHEDULE_STATUS="${BACKUP_INTERVAL} days "
3541
else
3642
SCHEDULE_STATUS="${BACKUP_INTERVAL} days "
3743
fi
@@ -44,6 +50,9 @@ backup_schedule_ui() {
4450
if [[ $BACKUP_INTERVAL -eq 1 ]]
4551
then
4652
SCHEDULE_STATUS="${BACKUP_INTERVAL} month "
53+
elif [[ $BACKUP_INTERVAL -lt 10 ]]
54+
then
55+
SCHEDULE_STATUS="${BACKUP_INTERVAL} months "
4756
else
4857
SCHEDULE_STATUS="${BACKUP_INTERVAL} months "
4958
fi

0 commit comments

Comments
 (0)