Beberapa Script untuk Rclone

Bagi kalian yang mainan VPS dengan backup di letakkan di sistem Cloud, pasti tidak asing dengan tool Rclone, Berikut ada beberapa script untuk Rclone, semoga bermanfaat :

compare-clouds.sh
#!/bin/bash
# Requirements: sudo apt install tree
if pidof -o %PPID -x "compare-acd-gdrive"; then
echo "Already running, exit"
exit 1
fi
scriptstart=$(date +'%s')
TSTAMP=`date +%d.%m.%Y/%H:%M`
LOGFILE="/home/plex/logs/compare-acd-gdrive.log"
ACDREMOTE="acd:"
GDRIVEREMOTE="gdrive:"
ACDMOUNT="/mnt/acdcrypt/"
GDRIVEMOUNT="/mnt/gdrivecrypt/"

echo "------------------------------------------- " | tee -a "$LOGFILE"
echo "$TSTAMP START" | tee -a "$LOGFILE"
echo "ACD FILES & SIZE (rclone size $ACDREMOTE)" | tee -a "$LOGFILE"
lastupload=`tail -n5 $LOGFILE | grep "Total size:" | cut -c 13- | cut -c -5`
acdstart=$(date +'%s')
rclone size $ACDREMOTE | tee -a "$LOGFILE"
echo "$TSTAMP ACD: It took $(($(date +'%s') - $acdstart)) seconds" | tee -a "$LOGFILE"
echo " " | tee -a "$LOGFILE"
echo "GDRIVE FILE & SIZE (rclone size $GDRIVEREMOTE)" | tee -a "$LOGFILE"
gdrivestart=$(date +'%s')
rclone size $GDRIVEREMOTE | tee -a "$LOGFILE"
echo "$TSTAMP GDRIVE: It took $(($(date +'%s') - $gdrivestart)) seconds" | tee -a "$LOGFILE"
echo " " | tee -a "$LOGFILE"
echo "$TSTAMP ACD&GDRIVE SIZE: TOTAL TIME $(($(date +'%s') - $scriptstart)) seconds" | tee -a "$LOGFILE"
echo " " | tee -a "$LOGFILE"
acdstart=$(date +'%s')
echo "ACD FILE/FOLDER COUNT (tree $ACDMOUNT)" | tee -a "$LOGFILE"
tree $ACDMOUNT | tail -1 | tee -a "$LOGFILE"
echo "$TSTAMP ACD COUNT: It took $(($(date +'%s') - $acdstart)) seconds" | tee -a "$LOGFILE"
echo " " | tee -a "$LOGFILE"
gdrivestart=$(date +'%s')
echo "GDRIVE FILE/FOLDER COUNT (tree $GDRIVEMOUNT) " | tee -a "$LOGFILE"
tree $GDRIVEMOUNT | tail -1 | tee -a "$LOGFILE"
echo "$TSTAMP GDRIVE COUNT: It took $(($(date +'%s') - $gdrivestart)) seconds" | tee -a "$LOGFILE"
echo " " | tee -a "$LOGFILE"
echo "$TSTAMP ACD&GDRIVE SIZE & COUNT: TOTAL TIME $(($(date +'%s') - $scriptstart)) seconds" | tee -a "$LOGFILE"
echo " " | tee -a "$LOGFILE"
exit

rclone-mount-check.sh
#!/bin/bash
# 1. Change paths
# 2. for mount and log file & create mountchek file.
# 3. Add to crontab -e (paste the line bellow, without # in front)
# * * * * * /home/plex/scripts/rclone-mount-check.sh >/dev/null 2>&1
# Make script executable with: chmod a+x /home/plex/scripts/rclone-mount-check.sh

LOGFILE="/home/plex/logs/rclone-mount-check.log"
RCLONEREMOTE="acdcrypt:"
MPOINT="/home/plex/acdcrypt"
CHECKFILEPATH="mountcheck"

if pidof -o %PPID -x "$0"; then
echo "$(date "+%d.%m.%Y %T") EXIT: Already running." | tee -a "$LOGFILE"
exit 1
fi

if [[ -f "$MPOINT/$CHECKFILE" ]]; then
echo "$(date "+%d.%m.%Y %T") INFO: Check successful, $MPOINT mounted." | tee -a "$LOGFILE"
exit
else
echo "$(date "+%d.%m.%Y %T") ERROR: $MPOINT not mounted, remount in progress." | tee -a "$LOGFILE"
# Unmount before remounting
while mount | grep "on ${MPOINT} type" > /dev/null
do
echo "($wi) Unmounting $mount"
fusermount -uz $MPOINT | tee -a "$LOGFILE"
cu=$(($cu + 1))
if [ "$cu" -ge 5 ];then
echo "$(date "+%d.%m.%Y %T") ERROR: Folder could not be unmounted exit" | tee -a "$LOGFILE"
exit 1
break
fi
sleep 1
done
rclone mount \
--read-only \
--allow-non-empty \
--allow-other \
--max-read-ahead 14G \
--acd-templink-threshold 0 \
--checkers 16 \
--quiet \
--stats 0 \
$RCLONEREMOTE $MPOINT &

while ! mount | grep "on ${MPOINT} type" > /dev/null
do
echo "($wi) Waiting for mount $mount"
c=$(($c + 1))
if [ "$c" -ge 4 ] ; then break ; fi
sleep 1
done
if [[ -f "$MPOINT/$CHECKFILE" ]]; then
echo "$(date "+%d.%m.%Y %T") INFO: Remount successful." | tee -a "$LOGFILE"
else
echo "$(date "+%d.%m.%Y %T") CRITICAL: Remount failed." | tee -a "$LOGFILE"
fi
fi
exit

rclone-mount.service
# Rclone mount on boot ( service file template - add aditional rclone parameters )
# Copy file to: /etc/systemd/system
# Set remote name (current acdcrypt), path to mount and User,Group (current plex)
# Register new service by typing:
# sudo systemctl daemon-reload
# sudo systemctl enable rclone-mount.service
# systemctl start rclone-mount.service
# systemctl enable NetworkManager-wait-online.service
# Usage:
# To unmount drive use
# systemctl stop rclone-mount.service
# To mount use:
# systemctl start rclone-mount.service

[Unit]
Description=rclone Amazon Cloud Drive FUSE mount
Documentation=http://rclone.org/docs/
After=network-online.target

[Service]
Type=simple
User=plex
Group=plex
ExecStart=/usr/bin/rclone mount --allow-non-empty --allow-other acdcrypt:/ /path/
ExecStop=/usr/bin/fusermount -uz /path/

[Install]
Wants=network-online.target

rclone-upload.sh
#!/bin/bash
# RCLONE UPLOAD CRON TAB SCRIPT
# chmod a+x /home/plex/scripts/rclone-upload.sh
# Type crontab -e and add line below (without #) and with correct path to the script
# * * * * * /home/plex/scripts/rclone-upload.sh >/dev/null 2>&1
# if you use custom config path add line bellow in line 20 after --log-file=$LOGFILE
# --config=/path/rclone.conf (config file location)

if pidof -o %PPID -x "$0"; then
exit 1
fi

LOGFILE="/home/plex/logs/rclone-upload.log"
FROM="/storage/local/"
TO="gdrivecrypt:/"

# CHECK FOR FILES IN FROM FOLDER THAT ARE OLDER THAN 15 MINUTES
if find $FROM* -type f -mmin +15 | read
then
start=$(date +'%s')
echo "$(date "+%d.%m.%Y %T") RCLONE UPLOAD STARTED" | tee -a $LOGFILE
# MOVE FILES OLDER THAN 15 MINUTES
rclone move "$FROM" "$TO" --transfers=20 --checkers=20 --delete-after --min-age 15m --log-file=$LOGFILE
echo "$(date "+%d.%m.%Y %T") RCLONE UPLOAD FINISHED IN $(($(date +'%s') - $start)) SECONDS" | tee -a $LOGFILE
fi
exit

rcquota.sh
#!/bin/bash
## Show gdrive used quota
LogFile=/dev/shm/rcquota.log

if [ -z $1 ]; then
echo "Use rcquota remote:"
fi
echo "Checking gdrive quota used for $Remote"
rclone lsd $1 -vv --dump-bodies --log-file=$LogFile >/dev/null 2>&1
QuotaUsed=$(grep -e 'quotaBytesUsed"' $LogFile | awk -F'[^0-9]*' '{print $2}')
QuotaUsed=$(($QuotaUsed / (1024*1024*1024)))
echo "$QuotaUsed GB"
rm $LogFile
exit

rcuploadcheck.sh
#!/bin/bash
# RCLONE UPLOAD CHECK SCRIPT
# chmod a+x /home/plex/scripts/rcuploadcheck.sh
# Set rclone remote and path to LogFile
# You can add script in crontab to check every 10 minutes and log gdrive locks/unlocks
# Type crontab -e and add line below (without #) and with correct path to the script
# */10 * * * * /home/plex/scripts/rcuploadcheck.sh >/dev/null 2>&1

if pidof -o %PPID -x "$0"; then
echo "$(date "+%d.%m.%Y %T") Already running, exiting."
exit 1
fi

LogFile="/home/plex/logs/rcuploadcheck.log"
UploadFile="/dev/shm/upload-file"
Remote="gdajki:"

if [[ ! -f $LogFile ]]; then
touch $LogFile
fi
LastState=$(tail -1 $LogFile)

start=$(date +'%s')
echo "INFO: Checking upload for $Remote"
dd if=/dev/zero of=$UploadFile count=1024 bs=100 >/dev/null 2>&1
/usr/bin/rclone move "$UploadFile" $Remote --delete-after --log-level ERROR
if [ $? -eq 0 ]; then
echo "INFO: Upload successful"
if [[ $LastState == *"upload locked"* ]]; then
echo "$(date "+%d.%m.%Y %T") $Remote upload unlocked" | tee -a $LogFile
fi
else
echo "INFO: Upload error, drive locked"
if [[ $LastState == *"upload unlocked"* ]]; then
echo "$(date "+%d.%m.%Y %T") $Remote upload locked" | tee -a $LogFile
fi
fi
echo "INFO: Finished in $(($(date +'%s') - $start)) seconds"
exit

 

Komentar