Skip to content

Commit 24ad712

Browse files
committed
install and src scripts added
1 parent 042427f commit 24ad712

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

install.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# ensure the script is run as root
4+
if [[ "$EUID" -ne 0 ]]; then
5+
echo "Error: This script must be run as root!"
6+
exit 1
7+
fi
8+
9+
# ensure systemd is available
10+
if ! command -v systemctl &> /dev/null; then
11+
echo "Error: systemd is not available on this system."
12+
exit 1
13+
fi
14+
15+
echo "Starting system-backup installation..."
16+
17+
# creating configuration file
18+
echo "Copying config file to /etc/system-backup/config.conf..."
19+
mkdir -p /etc/system-backup
20+
cp src/config.conf /etc/system-backup/config.conf
21+
chmod 600 /etc/system-backup/config.conf
22+
23+
# copy main script
24+
echo "Copying main script to /usr/local/bin/system-backup.sh..."
25+
cp src/system-backup.sh /usr/local/bin/system-backup.sh
26+
chmod +x /usr/local/bin/system-backup.sh
27+
28+
# setting up systemd service
29+
echo "Copying systemd service and timer files and setting up deamon..."
30+
cp src/openvpn-notify.service /etc/systemd/system/system-backup.service
31+
cp src/openvpn-notify.timer /etc/systemd/system/system-backup.timer
32+
systemctl daemon-reload
33+
systemctl enable system-backup
34+
systemctl start system-backup
35+
36+
# successful exit
37+
echo "system-backup installation finished!"
38+
exit 0

src/config.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BOT_TOKEN="your_bot_token_here"
2+
CHAT_ID="your_chat_id_here"

src/system-backup.service

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=System Backup Process
3+
4+
[Service]
5+
Type=oneshot
6+
ExecStart=/usr/local/bin/system-backup.sh
7+
User=root
8+
Group=root
9+
StandardOutput=journal
10+
StandardError=journal
11+
12+
[Install]
13+
WantedBy=multi-user.target

src/system-backup.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
BACKUP_DIR="/mnt/backup"
4+
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M")
5+
DEST="$BACKUP_DIR/sys_backup_$TIMESTAMP"
6+
CONFIG_FILE="/etc/system-backup/config.conf"
7+
8+
# load config file
9+
if [[ ! -f "$CONFIG_FILE" ]]; then
10+
echo "Config file $CONFIG_FILE not found! Retrying in 30 seconds..." | systemd-cat -t system-backup -p err
11+
exit 1
12+
fi
13+
14+
# load telegram credentials
15+
source "$CONFIG_FILE"
16+
17+
# sending telegram notification
18+
telegram_message() {
19+
local MESSAGE="$1"
20+
local URL="https://api.telegram.org/bot${BOT_TOKEN}/sendMessage"
21+
22+
RESPONSE=$(curl -s -X POST "$URL" -d chat_id="$CHAT_ID" -d text="$MESSAGE")
23+
if [[ "$RESPONSE" != *"\"ok\":true"* ]]; then
24+
echo "Failed to send Telegram message: $RESPONSE" | systemd-cat -t system-backup -p err
25+
fi
26+
}
27+
28+
# send telegram notification
29+
telegram_message "Starting system backup at $DEST"
30+
31+
# rotate backups: keep only the most recent
32+
cd "$BACKUP_DIR"
33+
ls -dt sys_backup_* | tail -n +2 | xargs -d '\n' rm -rf --
34+
35+
# new backup folder
36+
mkdir -p "$DEST"
37+
38+
# rsync backup
39+
rsync -aAXv / \
40+
--exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/swapfile"} \
41+
"$DEST" > /dev/null
42+
43+
if [ $? -ne 0 ]; then
44+
echo "System backup FAILED" | systemd-cat -t system-backup -p err
45+
telegram_message "System backup FAILED"
46+
exit 1
47+
fi
48+
49+
# send telegram notification
50+
telegram_message "System backup completed"
51+
52+
exit 0

src/system-backup.timer

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=System backup every week on Sunday at 4:00 AM
3+
4+
[Timer]
5+
OnCalendar=Sun 04:00
6+
Persistent=true
7+
8+
[Install]
9+
WantedBy=timers.target

0 commit comments

Comments
 (0)