-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathrclone-backup.sh
More file actions
111 lines (92 loc) · 3.91 KB
/
rclone-backup.sh
File metadata and controls
111 lines (92 loc) · 3.91 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Backup important stuff using Rclone
#
# If Entware is available it will try to install Rclone on first backup execution
# If Entware was installed to tmpfs it will uninstall Rclone after execution
#
# Based on:
# https://github.com/jacklul/rclone-backup
#
#jas-update=rclone-backup.sh
#shellcheck shell=ash
#shellcheck disable=SC2155
#shellcheck source=./common.sh
readonly common_script="$(dirname "$0")/common.sh"
if [ -f "$common_script" ]; then . "$common_script"; else { echo "$common_script not found" >&2; exit 1; } fi
REMOTE="remote:" # remote to use
PARAMETERS="--buffer-size 1M --progress --stats 1s --verbose --log-file=/tmp/rclone-backup.log" # optional parameters
CRON="0 6 * * 7" # schedule as cron string
CONFIG_FILE="/jffs/rclone-backup/rclone.conf" # Rclone configuration file
FILTER_FILE="/jffs/rclone-backup/filter.list" # Rclone filter/list file
SCRIPT_PRE="/jffs/rclone-backup/script-pre.sh" # execute a command before running rclone command
SCRIPT_POST="/jffs/rclone-backup/script-post.sh" # execute a command after running rclone command
RCLONE_PATH="" # path to Rclone binary
load_script_config
case "$1" in
"run")
# Detect when installed through Entware
if [ -z "$RCLONE_PATH" ] && [ -f /opt/bin/rclone ]; then
RCLONE_PATH="/opt/bin/rclone"
fi
# Install it through Entware then remove it after we are done
if [ -z "$RCLONE_PATH" ] && [ -f /opt/bin/opkg ]; then
logecho "Installing Rclone..."
# Required to setup execution env for OPKG
PATH=/opt/bin:/opt/sbin:$PATH
if opkg update && opkg install rclone; then
RCLONE_PATH="/opt/bin/rclone"
if df "$(readlink -f /opt)" | tail -n +2 | grep -Fq "tmpfs"; then
entware_on_tmpfs=1
fi
else
logecho "Failed to install Rclone!" error
fi
fi
{ [ "$(nvram get wan0_state_t)" != "2" ] && [ "$(nvram get wan1_state_t)" != "2" ] ; } && { logecho "Error: WAN network is not connected" error; return 1; }
[ ! -f "$RCLONE_PATH" ] && { logecho "Error: Could not find Rclone binary: $RCLONE_PATH" error; exit 1; }
[ ! -f "$CONFIG_FILE" ] && { logecho "Error: Could not find Rclone configuration file: $CONFIG_FILE" error; exit 1; }
[ ! -f "$FILTER_FILE" ] && { logecho "Error: Could not find filter file: $FILTER_FILE" error; exit 1; }
if [ -n "$SCRIPT_PRE" ] && [ -x "$SCRIPT_PRE" ]; then
logecho "Executing script '$SCRIPT_PRE'..." alert
. "$SCRIPT_PRE"
fi
logecho "Backing up now..." alert
#shellcheck disable=SC2086
"$RCLONE_PATH" sync --config="$CONFIG_FILE" --filter-from="$FILTER_FILE" / "$REMOTE" $PARAMETERS
status="$?"
if [ -n "$SCRIPT_POST" ] && [ -x "$SCRIPT_POST" ]; then
logecho "Executing script '$SCRIPT_POST'..." alert
. "$SCRIPT_POST"
fi
if [ -n "$entware_on_tmpfs" ]; then
logecho "Uninstalling Rclone..."
if ! /opt/bin/opkg remove rclone --autoremove; then
logecho "Failed to uninstall Rclone!" error
fi
fi
if [ "$status" -eq 0 ]; then
logecho "Finished successfully" alert
else
logecho "Finished with error code $status" alert
exit 1
fi
;;
"start")
[ ! -f "$CONFIG_FILE" ] && { logecho "Error: Rclone configuration file '$CONFIG_FILE' not found" error; exit 1; }
type rclone > /dev/null 2>&1 || { echo "Warning: Command 'rclone' not found" >&2; }
[ -n "$CRON" ] && crontab_entry add "$CRON $script_path run"
;;
"stop")
crontab_entry delete
;;
"restart")
sh "$script_path" stop
sh "$script_path" start
;;
*)
echo "Usage: $0 run|start|stop|restart"
exit 1
;;
esac