import os
import shutil
import time
from sh import rsync
#必须要在linux才有用sh,这个库要先pip安装
def check_dir(os_dir):
if not os.path.exists(os_dir):
print(os_dir, "does not exist")
def ask_for_confirm():
ans = input("do you want to continue? yes/no\n")
global con_exit
if ans == "yes":
con_exit = 0
return con_exit
elif ans == "no":
con_exit = 1
return con_exit
else:
print("answer with yes or no.")
ask_for_confirm()
backup_dir = input("enter directory to backup\n")
check_dir(backup_dir)
print(backup_dir,"saved.")
time.sleep(3)
backup_to_dir = input("where to backup?\n")
check_dir(backup_to_dir)
print("doing the bakup now!")
ask_for_confirm()
if con_exit == 1:
print("aborting the backup process!")
exit(1)
rsync("-auhv", "--delete", "--exclude=lost+found", "--exclude=/sys", "--exclude=/tmp","--exclude=/proc","--exclude=/mnt","--exclude=/dev",backup_dir,backup_to_dir)
#exclude 排除规则,delete删除无关文件,-a存档,-u更新,-h显示帮助,-v详情
python在linux下备份文件
于 2022-03-27 21:02:37 首次发布