-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_script.py
More file actions
executable file
·27 lines (22 loc) · 1021 Bytes
/
Copy pathbackup_script.py
File metadata and controls
executable file
·27 lines (22 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import shutil
dirs = input("Enter the directories you want to backup separated by space: ").split()
final_archivename = input("Enter the filename for final .tar archive: ")
output_dir = os.path.expanduser("~/backups/")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Create archive for each selected directory
for dir in dirs:
dir_path = os.path.expanduser(dir)
if os.path.isdir(dir_path):
archive_path = os.path.join(output_dir, dir.replace("/", "_") + ".tar")
# creates the .tar archive with the following name:
# "archive_path.tar" but then [:-4] removes ".tar" from the name
# shutil.make_archive(base_name, format, base_dir)
shutil.make_archive(archive_path[:-4], "tar", dir_path)
else:
print(f"Error: {dir_path} does not exist or is not a directory")
# TODO
# all directories from the user input are in separate archive
# combine them together using the final_archivename variable
print(f"Backup created in {output_dir}")