76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
import os
|
|
from datetime import datetime, timedelta
|
|
import subprocess
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
def sync_monthly(base_src: str, base_dest: str, date: datetime) -> None:
|
|
"""
|
|
Sync files for a specific year/month from base_src to base_dest
|
|
|
|
Args:
|
|
base_src: Base source directory
|
|
base_dest: Base destination directory
|
|
date: Date to process
|
|
"""
|
|
# Format year and month
|
|
year = date.strftime("%Y")
|
|
month = date.strftime("%m")
|
|
|
|
# Construct full paths
|
|
src_path = Path(base_src) / year / month
|
|
dest_path = Path(base_dest) / year / month
|
|
|
|
# Skip if source doesn't exist
|
|
if not src_path.exists():
|
|
print(f"Source path does not exist: {src_path}")
|
|
return
|
|
|
|
print(f"Syncing {src_path} -> {dest_path}")
|
|
|
|
# Call sync.py
|
|
# Assuming sync.py is in the same directory as this script
|
|
sync_script = Path(__file__).parent / "sync.py"
|
|
try:
|
|
subprocess.run([
|
|
"python",
|
|
str(sync_script),
|
|
str(src_path),
|
|
str(dest_path)
|
|
], check=True)
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Sync failed for {year}/{month}: {e}")
|
|
|
|
# Sync OneDrive to Local(Backup)
|
|
# python sync_task.py d:\OneDrive\Photos\Auto-saved\ D:\Photos\Auto-saved --months 1
|
|
# Sync Photo to NAS
|
|
# python sync_task.py d:\OneDrive\Photos\Auto-saved\ \\tiger-nas\Multimedia\Photos\ --months 1
|
|
# Sync Local Motion to NAS
|
|
# python sync_task.py D:\Photos\OneDrive-Photo\ \\tiger-nas\Multimedia\Photos\ --months 1
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description='Sync files based on year/month structure')
|
|
parser.add_argument('base_src', help='Base source directory')
|
|
parser.add_argument('base_dest', help='Base destination directory')
|
|
parser.add_argument('--months', type=int, default=0,
|
|
help='Optional: specific month to sync (1: current month, 2: last month, etc.). '
|
|
'If not set: syncs current month, plus last month if today is first day of month')
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Get current date
|
|
now = datetime.now()
|
|
|
|
if args.months > 0:
|
|
# Specific month was requested
|
|
months_ago = args.months - 1
|
|
target_date = now.replace(day=1) - timedelta(days=months_ago * 28)
|
|
sync_monthly(args.base_src, args.base_dest, target_date)
|
|
else:
|
|
# Default behavior
|
|
# Always sync current month
|
|
sync_monthly(args.base_src, args.base_dest, now)
|
|
|
|
# If it's the first day of the month, also sync last month
|
|
if now.day == 1:
|
|
last_month = now.replace(day=1) - timedelta(days=1)
|
|
sync_monthly(args.base_src, args.base_dest, last_month) |