GitRepositorySynchronizerV1/grs.sh

61 lines
1.6 KiB
Bash
Raw Normal View History

2024-03-13 11:51:49 -04:00
#!/bin/bash
2024-03-23 16:33:40 -04:00
logPath=log.txt
2024-03-13 11:51:49 -04:00
# 1/10 = 0.1 = 10%
thresholdFactor=10
sleepTime=10m
#while true; do
if [ ! -d repositories ]; then mkdir repositories; fi
if [ ! -d snapshots ]; then mkdir snapshots; fi
cd repositories
for repository in *; do
if [ ! -d "$repository" ]; then continue; fi
if [ ! -d "../snapshots/$repository" ]; then cp -rf "$repository" ../snapshots; fi
log=$(echo [$(date +"%Y-%m-%d %H:%M:%S")] Checking \"$repository\")
echo "$log"
cd "$repository"
gitOutput=$(git pull --recurse-submodules=on-demand)
echo "$gitOutput"
cd ..
2024-03-23 16:33:40 -04:00
if [ "$gitOutput" == "Already up to date." ] || [ "$gitOutput" == "" ]; then continue; fi
2024-03-13 11:51:49 -04:00
2024-03-23 16:33:40 -04:00
cd ..
echo "$log" >> "$logPath"
echo "$gitOutput" >> "$logPath"
2024-03-13 11:51:49 -04:00
2024-03-23 16:33:40 -04:00
snapshotSize=$(($(du -sb "snapshots/$repository" | cut -f1) - $(du -sb "snapshots/$repository/.git" | cut -f1)))
if [ $(($snapshotSize - ($snapshotSize / $thresholdFactor))) -lt $(($(du -sb "repositories/$repository" | cut -f1) - $(du -sb "repositories/$repository/.git" | cut -f1))) ]; then
2024-03-13 11:51:49 -04:00
log=$(echo [$(date +"%Y-%m-%d %H:%M:%S")] Updating \"$repository\")
echo "$log"
2024-03-23 16:33:40 -04:00
echo "$log" >> "$logPath"
echo >> "$logPath"
2024-03-13 11:51:49 -04:00
2024-03-23 16:33:40 -04:00
rm -rf "snapshots/$repository"
cp -rf "repositories/$repository" snapshots
cd repositories
2024-03-13 11:51:49 -04:00
continue
fi
snapshotTime=$(echo [$(date +"%Y-%m-%d %H'%M'%S")])
log=$(echo $snapshotTime Snapshotting \"$repository\")
echo "$log"
2024-03-23 16:33:40 -04:00
echo "$log" >> "$logPath"
echo >> "$logPath"
tar -czf "snapshots/$snapshotTime $repository.tar.gz" "snapshots/$repository"
rm -rf "snapshots/$repository"
cp -rf "repositories/$repository" snapshots
2024-03-13 11:51:49 -04:00
2024-03-23 16:33:40 -04:00
cd repositories
2024-03-13 11:51:49 -04:00
done
cd ..
# sleep $sleepTime
#done