Repository navigation
Expand file tree
/
Copy pathscript.sh
More file actions
158 lines (131 loc) Β· 5.96 KB
/
Copy pathscript.sh
File metadata and controls
158 lines (131 loc) Β· 5.96 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# === CONFIGURATION ===
SOURCE="/root/immich-app/library/library" # Path to the local Immich library
DEST="proton:Immich" # rclone remote destination (must be configured)
# Define user folders (replace with your actual subfolders)
USER1_NAME="User1" # Display name for first user
USER2_NAME="User2" # Display name for second user
USER1_PATH="$SOURCE/user1_folder" # Local path to first user's subfolder
USER2_PATH="$SOURCE/user2_folder" # Local path to second user's subfolder
LOGDIR="/root/log"
mkdir -p "$LOGDIR" # Ensure log directory exists
# Delete logs older than 20 days
find "$LOGDIR" -type f -name 'log_immich_*.txt' -mtime +20 -delete # Remove old logs (older than 20 days)
# Create log files
MAINLOGFILE="$LOGDIR/log_immich_$(date +%F).txt" # Daily log file
TEMPLOGFILE=$(mktemp) # Temporary log file for current run
# === TELEGRAM NOTIFICATION CONFIGURATION ===
BOT_TOKEN="INSERT_YOUR_TELEGRAM_BOT_TOKEN" # Telegram bot token (insert your own)
CHAT_ID="INSERT_YOUR_CHAT_ID" # Telegram chat ID (insert your own)
# === EXECUTE BACKUP ===
# Start rclone backup with parameters
rclone sync "$SOURCE" "$DEST" \
# --backup-dir="proton:Immich/archive/$(date +%F_%H-%M-%S)" \ # Optional: move deleted/modified files here instead of losing them. Useful for archival. Use only if you have enough space on your remote!
--drive-chunk-size 128M \
--fast-list \
--tpslimit 4 \
--transfers 2 \
--checkers 2 \
--retries 3 \
--low-level-retries 5 \
--retries-sleep 15s \
--timeout 10m \
--contimeout 30s \
--expect-continue-timeout 10s \
--log-file="$TEMPLOGFILE" \
--log-level INFO
STATUS=$? # Save rclone exit status
if [ $STATUS -eq 0 ]; then
TOTAL=$(grep 'Copied (new)' "$TEMPLOGFILE" | grep -v '/\.' | wc -l)
# Count number of new files copied (total)
U1_COUNT=$(grep 'Copied (new)' "$TEMPLOGFILE" | grep ' user1_folder/' | wc -l)
# Count number of new files copied for user 1
U2_COUNT=$(grep 'Copied (new)' "$TEMPLOGFILE" | grep ' user2_folder/' | wc -l)
# Count number of new files copied for user 2
CHECKS=$(grep 'Checks:' "$TEMPLOGFILE" | tail -n1 | awk '{print $2}')
# Extract number of file checks from log
ELAPSED=$(grep 'Elapsed time' "$TEMPLOGFILE" | tail -n1 | awk -F': ' '{print $2}')
# Extract elapsed time from log
DISK_USAGE=$(df -h / | awk 'NR==2 {print "π¦ Disk usage: "$3" / "$2" ("$5" used)"}')
# Get overall disk usage
U1_NEW=$(grep 'Copied (new)' "$TEMPLOGFILE" | grep ' user1_folder/' | awk -F': ' '{print $2}' | xargs -I{} stat --format="%s" "$SOURCE/{}" 2>/dev/null | awk '{sum+=$1} END {printf "%.2f MB", sum/1024/1024}')
# Calculate size of new files copied for each user
U2_NEW=$(grep 'Copied (new)' "$TEMPLOGFILE" | grep ' user2_folder/' | awk -F': ' '{print $2}' | xargs -I{} stat --format="%s" "$SOURCE/{}" 2>/dev/null | awk '{sum+=$1} END {printf "%.2f MB", sum/1024/1024}')
# Calculate size of new files copied for each user
U1_TOTAL=$(rclone size "$DEST/user1_folder" --json | jq '.bytes' | awk '{printf "%.2f GB", $1/1024/1024/1024}')
# Get total storage used by each user on remote
U2_TOTAL=$(rclone size "$DEST/user2_folder" --json | jq '.bytes' | awk '{printf "%.2f GB", $1/1024/1024/1024}')
# Get total storage used by each user on remote
if [ "$TOTAL" -eq 0 ]; then
# If no new files were copied, prepare minimal message
MESSAGE="π¦ Immich backup completed on $(hostname) at $(date)
No new files to backup.
π Checked files: $CHECKS
β±οΈ Duration: $ELAPSED
$DISK_USAGE"
else
MESSAGE="β
Immich backup completed successfully on $(hostname) at $(date)
π Files copied:
$USER1_NAME: $U1_COUNT
$USER2_NAME: $U2_COUNT
Total: $TOTAL
π Checked files: $CHECKS
β±οΈ Duration: $ELAPSED
$DISK_USAGE
π Per-user disk usage:
- $USER1_NAME: $U1_TOTAL (new: $U1_NEW)
- $USER2_NAME: $U2_TOTAL (new: $U2_NEW)"
fi
else
# If backup failed, prepare error message
if grep -qE "ERROR|401|503|504" "$TEMPLOGFILE"; then
# Detect API-related errors in the log
MESSAGE="β Immich backup failed (API errors) on $(hostname) at $(date)
Check the log file: $MAINLOGFILE"
else
MESSAGE="β Immich backup failed with unknown error on $(hostname). Manual check recommended."
fi
fi
# === OPTIONAL: DELETE OLD ARCHIVE FOLDERS ===
DELETE_OLD_ARCHIVES=false # Set to true to enable archive cleanup
ARCHIVE_PATH="proton:Immich/archive" # Path where --backup-dir saves deleted files
KEEP_DAYS=30 # Number of days to keep
if [ "$DELETE_OLD_ARCHIVES" = true ]; then
# Get date threshold in seconds
THRESHOLD=$(date -d "$KEEP_DAYS days ago" +%s)
# List remote archive directories
rclone lsf "$ARCHIVE_PATH" --dirs-only | while read -r dir; do
# Remove trailing slash and extract timestamp from directory name
clean_dir=$(echo "$dir" | sed 's:/*$::')
dir_date=$(echo "$clean_dir" | cut -d_ -f1) # Extract YYYY-MM-DD
dir_epoch=$(date -d "$dir_date" +%s 2>/dev/null)
if [ -n "$dir_epoch" ] && [ "$dir_epoch" -lt "$THRESHOLD" ]; then
rclone purge "$ARCHIVE_PATH/$clean_dir" || logger "Failed to delete $clean_dir from archive"
fi
done
fi
# === COMPACT LOG CREATION ===
{
grep 'Copied (new)' "$TEMPLOGFILE"
echo ""
tail -100 "$TEMPLOGFILE" | grep -E 'Transferred:|Checks:|Elapsed time:'
} > "$MAINLOGFILE"
COMPACTLOGFILE="$LOGDIR/compact_log_immich.txt"
{
grep 'Copied (new)' "$TEMPLOGFILE"
echo ""
grep -E 'Transferred:|Checks:|Elapsed time:' "$TEMPLOGFILE" | tail -n 3
} > "$COMPACTLOGFILE"
rm "$TEMPLOGFILE"
# Clean up temporary log
# === SEND TELEGRAM NOTIFICATION ===
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
# Send Telegram text message
-d chat_id="$CHAT_ID" \
-d "text=$MESSAGE"
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendDocument" \
# Send Telegram log file as document
-F chat_id="$CHAT_ID" \
-F caption="π Immich backup log - $(date)" \
-F document=@"$COMPACTLOGFILE"
rm "$COMPACTLOGFILE"