要使用Python命令进行Linux备份,您可以使用subprocess
模块来执行shell命令
python3 --version
如果未安装Python,请使用以下命令安装:
sudo apt-get install python3
backup.py
的Python脚本文件,并在其中添加以下内容:import subprocess
import datetime
def backup(source_directory, destination_directory):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
backup_directory = f"{destination_directory}/backup_{timestamp}"
try:
subprocess.run(["tar", "-czvf", f"{backup_directory}.tar.gz", source_directory], check=True)
print(f"Backup of {source_directory} completed successfully! Backup file is {backup_directory}.tar.gz")
except subprocess.CalledProcessError as e:
print(f"Backup failed: {e}")
if __name__ == "__main__":
source_directory = "/path/to/source/directory"
destination_directory = "/path/to/destination/directory"
backup(source_directory, destination_directory)
请确保将/path/to/source/directory
和/path/to/destination/directory
替换为您要备份的源目录和目标备份目录的实际路径。
backup.py
文件的目录,然后运行以下命令:python3 backup.py
这将创建一个包含所选源目录中所有文件的tar.gz备份文件,并将其保存在指定的目标备份目录中。备份文件名将包含当前时间戳,以便轻松识别。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。