Restic Python Program
Table of Contents
Hello, today in this blog you will learn how to make a python program that backups your documents using restic. But you may be thinking why do you need to do this? Can’t you just write:
restic backup C:\\Users\\koderT\\Documents blah blah blah
Thats what I see in all the blogs! Thats correct however if your lazy writing these long sentences all the time why don’t you make a mini python program to speed the process. Heres how you make it: (It depends on which program base your using im using shell)
Step 1.) Make a list to backup #
In order to make this program we have to make a list of what document we want to backup (you can edit and add more things you want to backup afterwards but do the most important ones first), heres what your list should look like.
backup = ["C:\\Users\\koderT\\Documents",
"C:\\Users\\koderT\\dev",
"C:\\Users\\koderT\\ibisPaint",
"C:\\Users\\koderT\Pictures"]
It may look a bit different, thats because when you make a list of many things it becomes a long list and it will be hard to edit things so we can turn it into a list but downwards.
Step 2.) Use a for-loop #
For our python program, it would be nice to show what were going to backup its kinda like a display. Were also gonna use enumerate so its numbered and it looks a lot neater. Heres what your code should look like now:
backup = ["C:\\Users\\koderT\\Documents",
"C:\\Users\\koderT\\dev",
"C:\\Users\\koderT\\ibisPaint",
"C:\\Users\\koderT\Pictures"]
print("-----------------------------------------\n")
for b, number in enumerate(backup, start =1):
print(f" {b}. {number}")
print("\n-----------------------------------------")
Like I did before I edited to be different to what I described to make it a lot more nicer. Btw if your wondering why I put four space in the print for the for-loop because when you print it it will look nice
Step 3.) Adding the Restic #
Now here comes the sort of challenging part so basically were going to make another list called restic_command because its a restic commmand, were gonna add restic and backup into the list. And then after that were going to make another list called final_command or whatever you want and were gonna add backup.
backup = ["C:\\Users\\koderT\\Documents",
"C:\\Users\\koderT\\dev",
"C:\\Users\\koderT\\ibisPaint",
"C:\\Users\\koderT\Pictures"]
print("-----------------------------------------\n")
for b, number in enumerate(backup, start =1):
print(f" {b}. {number}")
print("\n-----------------------------------------")
restic_command = ["restic", "backup"]
final_command = restic_command + backup
Step 4.) The final step adding subprocess #
The last and final step we have to add subprocess ‘what is subprocess?’ you may be thinking, its a child process. To do that we need to import it into our blog (make sure to put it at the very top of your code). After that you would need to make a final final list called whatever you want and use the command subprocess.run() and add the final_command. And this is the last thing to do so this thing depend on which program your using but because im using shell, I split the final_command using a comma and write shell=True and there you are done heres what your code should look like now.
backup = ["C:\\Users\\koderT\\Documents",
"C:\\Users\\koderT\\dev",
"C:\\Users\\koderT\\ibisPaint",
"C:\\Users\\koderT\Pictures"]
print("\nList of directories to be backed up:")
print("-----------------------------------------\n")
for b, number in enumerate(backup, start =1):
print(f" {b}. {number}")
print("\n-----------------------------------------")
print("Backup process started...\n")
restic_command = ["restic", "backup"]
final_command = restic_command + backup
restic = subprocess.run(final_command, shell=True)
Thanks for reading bye :3333333!!!!!!!