-
Notifications
You must be signed in to change notification settings - Fork 14
/
rclone-backup.sh
107 lines (89 loc) · 3.91 KB
/
rclone-backup.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
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
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Backup important stuff using Rclone
#
# Note that automatic download of Rclone binary stores it in /tmp directory - make sure you have enough free RAM!
# This script will detect if Rclone was installed through Entware and set RCLONE_PATH automatically when left empty
#
#jacklul-asuswrt-scripts-update
#shellcheck disable=SC2155
readonly SCRIPT_PATH="$(readlink -f "$0")"
readonly SCRIPT_NAME="$(basename "$SCRIPT_PATH" .sh)"
readonly SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
readonly SCRIPT_CONFIG="$SCRIPT_DIR/$SCRIPT_NAME.conf"
readonly SCRIPT_TAG="$(basename "$SCRIPT_PATH")"
REMOTE="remote:" # remote to use
PARAMETERS="--buffer-size 1M --progress --stats 1s --verbose" # optional parameters
CONFIG_FILE="/jffs/rclone.conf" # Rclone configuration file
FILTER_FILE="/jffs/rclone.list" # Rclone filter/list file
RCLONE_PATH="" # path to Rclone binary
LOG_FILE="/tmp/rclone.log" # log file
NVRAMTXT_FILE="/tmp/nvram.txt" # file to dump 'nvram show' result to, empty means don't dump
NVRAMCFG_FILE="/tmp/nvram.cfg" # file to 'nvram save' to, empty means don't save
CRON="0 6 * * 7"
if [ -f "$SCRIPT_CONFIG" ]; then
#shellcheck disable=SC1090
. "$SCRIPT_CONFIG"
fi
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
logger -st "$SCRIPT_TAG" "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 mount | grep "on /opt " | grep -q "tmpfs"; then
ENTWARE_ON_TMPFS=1
fi
else
logger -st "$SCRIPT_TAG" "Failed to install Rclone!"
fi
fi
{ [ "$(nvram get wan0_state_t)" != "2" ] && [ "$(nvram get wan1_state_t)" != "2" ]; } && { echo "WAN network is not connected"; exit 1; }
[ ! -f "$CONFIG_FILE" ] && { logger -st "$SCRIPT_TAG" "Could not find Rclone configuration file: $CONFIG_FILE"; exit 1; }
[ ! -f "$FILTER_FILE" ] && { logger -st "$SCRIPT_TAG" "Could not find filter file: $FILTER_FILE"; exit 1; }
[ ! -f "$RCLONE_PATH" ] && { logger -st "$SCRIPT_TAG" "Could not find Rclone binary: $RCLONE_PATH"; exit 1; }
logger -st "$SCRIPT_TAG" "Creating backup..."
echo "" > "$LOG_FILE"
[ -n "$NVRAMTXT_FILE" ] && nvram show > "$NVRAMTXT_FILE" 2> /dev/null
[ -n "$NVRAMCFG_FILE" ] && nvram save "$NVRAMCFG_FILE" 2> /dev/null
#shellcheck disable=SC2086
"$RCLONE_PATH" sync --config "$CONFIG_FILE" --filter-from="$FILTER_FILE" / "$REMOTE" --log-file="$LOG_FILE" $PARAMETERS
STATUS="$?"
rm -f "$NVRAMTXT_FILE" "$NVRAMCFG_FILE"
if [ -n "$ENTWARE_ON_TMPFS" ]; then
logger -st "$SCRIPT_TAG" "Uninstalling Rclone..."
if ! /opt/bin/opkg remove rclone --autoremove; then
logger -st "$SCRIPT_TAG" "Failed to uninstall Rclone!"
fi
fi
if [ "$STATUS" = "0" ]; then
logger -st "$SCRIPT_TAG" "Backup completed successfully"
else
logger -st "$SCRIPT_TAG" "There was an error while running backup, check $LOG_FILE for details"
exit 1
fi
;;
"start")
[ ! -f "$CONFIG_FILE" ] && { logger -st "$SCRIPT_TAG" "Unable to start - Rclone configuration file ($CONFIG_FILE) not found"; exit 1; }
cru a "$SCRIPT_NAME" "$CRON $SCRIPT_PATH run"
;;
"stop")
cru d "$SCRIPT_NAME"
;;
"restart")
sh "$SCRIPT_PATH" stop
sh "$SCRIPT_PATH" start
;;
*)
echo "Usage: $0 run|start|stop|restart"
exit 1
;;
esac