创建工作目录
import os
base_dir = '~/sync_task'
cache_dir = os.path.join(base_dir, 'cache')
tmp_dir = os.path.join(base_dir, 'tmp')
# 确保目录存在
for dir_path in [base_dir, cache_dir, tmp_dir]:
os.makedirs(os.path.expanduser(dir_path), exist_ok=True)
print("目录已设置。")
安装rclone,并配置环境
!curl https://rclone.org/install.sh | sudo bash
from google.colab import files
# Upload the rclone config file
uploaded = files.upload()
# Check if the correct file was uploaded and move it to the sync_task directory
for fn in uploaded.keys():
if "rclone.conf" in fn:
print('User uploaded rclone config file.')
!mv rclone.conf ~/sync_task/rclone.conf
print("rclone configuration moved to ~/sync_task/")
else:
print(f'Unexpected file "{fn}". Please upload rclone.conf.')
测试连通性
!rclone lsd olde5: --config ~/sync_task/rclone.conf && rclone lsd new: --config ~/sync_task/rclone.conf
配置sync和日志监视
import subprocess
# Define rclone command with logging
command = "rclone sync olde5: new: --dry-run --config ~/sync_task/rclone.conf -v --log-file ~/sync_task/rclone.log --transfers 15 --checkers 64 --fast-list --cache-dir ~/sync_task/cache --temp-dir ~/sync_task/tmp"
rclone_process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
import time
from IPython.display import clear_output
# Define monitoring period and duration
log_check_interval = 10 # seconds
keep_alive_message_interval = 60 # seconds
last_message_time = 0
while True:
# Check if the rclone process is still running
if rclone_process.poll() is not None:
print("rclone process has finished.")
break
clear_output(wait=True) # Clear the previous output
# Print last 30 lines of the log file
log_content = !tail -n 30 ~/sync_task/rclone.log
for line in log_content:
print(line)
# Print a keep-alive message
current_time = time.time()
if current_time - last_message_time > keep_alive_message_interval:
print("Still syncing...")
last_message_time = current_time
# Sleep before checking the log again
time.sleep(log_check_interval)
# Check if the process is still running
if rclone_process.poll() is None:
rclone_process.terminate()
print("rclone process terminated.")
else:
print("rclone process is not running.")