Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python backup script If file is already in backup folder then do nothing.

Hello, guys. how can I compare all files in every dir and subdirs.
If any file from source dir is already in destination dir then don't backup it again just pass
I have made it now just to copy and paste every file from src to dest dirs every time when scripts is started
As example lets take these directories:
Source directory ->        /root/$USER/Desktop
Destination directory ->  /root/$USER/backup/Desktop
If you are checking by simple filename you can raise exception if file exists by passing os.O_EXCL in your open() funtion.
See this for more info.
https://stackoverflow.com/questions/3044...-in-python
(Nov 30, 2018 02:00 AM)Dcayy Wrote:  Hello, guys. how can I compare all files in every dir and subdirs.
If any file from source dir is already in destination dir then don't backup it again just pass
I have made it now just to copy and paste every file from src to dest dirs every time when scripts is started
As example lets take these directories:
Source directory ->        /root/$USER/Desktop
Destination directory ->  /root/$USER/backup/Desktop

Code
from os import listdir,getcwd,chdir,mkdir
from os.path import isdir,join
from shutil import copy2
from sys import platform
def get_first_root():
   return getcwd()

def create_folder(directory):
   try:
       mkdir(directory)
   except:
       pass

def get_name(directory):
   if platform =='win32':
       return directory.split('\\')[-1]
   else:
       return directory.split('/')[-1]

def _directory_destiny(start,name):
   return join(start,name)

def backup(start,directory_to_copy):
   name=get_name(directory_to_copy)
   root_with_directory=_directory_destiny(start,name)
   create_folder(root_with_directory)
   for file in listdir(directory_to_copy):
       chdir(root_with_directory)
       if isdir(join(directory_to_copy,file)):
           backup(root_with_directory,join(directory_to_copy,file))
       else:
           if file not in listdir(root_with_directory):
               copy2(join(directory_to_copy,file),root_with_directory)
           else:
               print('{} alredy exists'.format(join(directory_to_copy,file)))

I Hope that this work for you
Also, depending on how you are wanting to implement it, you can use someone similar to this which is pretty simple
Code
import os
import shutil
# /root/$USER/Desktop
# /root/$USER/backup/Desktop

source_files = "/root/$USER/Desktop"
destination_files = "/root/$USER/backup/Desktop"
source_files_array = os.listdir(source_files)
destination_files_array = os.listdir(destination_files)

for item in source_files_array:
    if not item in destination_files_array:
        if os.path.isfile(source_files + "/" + item):
            shutil.copy((source_files + "/" + item), destination_files + "/" + item)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 631 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by ZamperJan 28, 2020 09:30 PM 3 179 Jan 28, 2020 09:30 PM
Last Post: Shisho
[C] by Tr0jandogSep 11, 2019 11:44 AM 0 134 Sep 11, 2019 11:44 AM
Last Post: Tr0jandog