Python Backup script not working on windows - python

I have created a backup script that creates zip of folders. This script is working on linux but not on Windows. Please help.
import os
import time
source = 'D:\\backup_original'
target_dir = 'E:\\backup_copied'
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
zip_command = "zip {0} {1}".format(target, ''.join(source))
os.system(zip_command)

On windows, zip is not installed by default. You need to install 7 zip, command line. I have modified your script.
import os
import time
source = 'D:\\backup_original'
target_dir = 'E:\\backup_copied'
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
zip_command = "7za a {0} {1}".format(target, ''.join(source))
if os.system(zip_command) == 0:
print('Success')
else:
print('Backup Failed')
Are you sure this script was working on linux? u have used "zip a"? You must have copied it from somewhere.

Related

after run python script to run recursive rename my files has disappeared

I've just run this script to rename my files, adding the 15 first chars of files, but now all the files are disappearead, and i can't find them. i've just run this on a mac
import os
def replace(folder_path, old, new):
for path, subdirs, files in os.walk(folder_path):
for name in files:
file_path = os.path.join(path,name)
new_name = os.path.basename(path)[:15] + " - " + name
print path + "##" + new_name
os.rename(file_path, new_name)
replace('/Users/myuser/mp3/', '', '')
#DDave,
In the function call os.rename(src, dst): src is the fully qualified filepath of the source file, and dst is fully qualified filepath of the target file.
In your above program, new_name is just the new name of the file which does not have the directory information (and hence it is considering the directory as your current working directory). That is why you are not seeing the renamed files where you were looking to see them.
The program is rather trying to create the files under your current working directory. Run the following from your console of your IDE or from your program to figure out the current working directory:
print(os.getcwd())
Providing the full solution as requested in the comment below:
import os
def replace(folder_path, old, new):
for path, subdirs, files in os.walk(folder_path):
for name in files:
file_path = os.path.join(path,name)
# new_name = os.path.basename(path)[:15] + " - " + name
new_filepath = os.path.join(path, (name[:15] if len(name)>=15 else name) + " - " + name)
print ("new path: " + new_filepath)
os.rename(file_path, new_filepath)

Why do I get zip error while creating a file copy program?

I'm creating a program for copying files to directories but I'm getting an error:
Unable to find threadStateIndex for the current thread. curPyThread:
0x7f8090c03050
zip error: Nothing to do! (try: zip -qr
Users/ketiaslanishvili/Desktop/20190811154406.zip . -i
Users/ketiaslanishvili/Desktop/Video/1.mp4)
My code :
import os
import time
source = ['"Users/ketiaslanishvili/Desktop/Video"' ]
target_dir = 'Users/ketiaslanishvili/Desktop'
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S')+'.zip'
zip_command = "zip -qr {0} {1}" .format(target, '' .join(source))
if os.system(zip_command) == 0:
print('Completed', target)
else:
print('Unsucsesful:((((((')

Python 3 Backup Failed?

I just followed totorial《A Byte of Python》,there is a problem to back up all important files.
Here is my code:
import os
import time
source = ['"C:\\My Documents"', 'C:\\Code']
target_dir = 'D:\\Backup'
target = target_dir + os.sep + \
time.strftime('%Y%m%d%H%M%S') + '.Zip'
if not os.path.exists(target_dir):
os.mkdir(target_dir)
zip_command = 'zip -r {0} {1}'.format(target,
' '.join(source))
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup Failed')
Although I am on windows system, I think I have added C:\Windows\GnuWin32\bin to system PATH environment variable.Is it right?
When I type zip in terminal it shows like this
But when I run the program, the result is:
Zip command is:
zip -r D:\Backup\20170310193946.Zip "C:\My Documents" C:\Code
Running:
Backup Failed
I have tried again and again, still don't know why.
Any help would be greatly appreciated. Thank you
Make sure you installed zip 3.0 from the GnuWin32 website.
Maybe you need to change system path environment variable from C:\Windows\GnuWin32\bin to C:\Program Files (x86)\GnuWin32\bin.
Restart IDE (in my case it was PyCharm) after changing the path.

Creating a backup and getting error only concenate list and not (str)

I'm trying to write a code to create backup of files or directory using Python but there is an error : can only concenate list not "str"
Here's my code :
import os
import time
# The files or directory which has to be backed up
source = ['"F:\\College Stuffs"']
#The backup must be stored in a target directory
target_dir = ['"E:\\Backup"']
#File will be backed up in a zip file and name will be set to current date
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + target.append('.zip')
# Create the directory if it's not present
if not os.path.exists(target_dir):
os.mkdir(target_dir) #Make the directory
#Use zip command to put files in a zip archive
zip_command = "zip -r {} {}".format(target,''.join(source))
#run the backuo
print "Zip command is : "
print zip_command
print "Running:"
if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED'
You've defined a list
#The backup must be stored in a target directory
target_dir = ['"E:\\Backup"']
while your usage indicates you'd intended to use a str there:
#The backup must be stored in a target directory
target_dir = '"E:\\Backup"'

copying logs in python using the command line function

I had a doubt with my code which i think I can verify here . My requirement is to copy the apache log and error log from two different servers . Iv written down a python program, using a for loop.
My code:
def copylogs(Appache,Errorlog, folder_prefix) :
root_path = '/home/tza/Desktop/LOGS/'
folders = ['Appache','Errorlog']
for folder in folders:
folder_name = folder_prefix + "_" + folder + str(int(time.time()))
mkdircmd = "mkdir -p " + root_path + "/" + folder_name
os.system(mkdircmd)
filePath = root_path + folder_name
serverPath = "/var/log/apache/*"
cmd = "scp " + "symentic#60.62.1.164:" + serverPath + " " + filePath
cmd = cmd.replace("60.62.1.164" ,myip1)
cmd = os.system(cmd)
print "Logs are at:",root_path+folder_name
time.sleep(10)
filePath = root_path + folder
serverPath = "/var/log/errorlog/*"
cmd = "scp " + "symentic#10.95.21.129:" + serverPath + " " + filePath
cmd = cmd.replace("10.95.21.129" ,myip2)
cmd = os.system(cmd)
print "Logs are at:",root_path+folder_name
now Im calling the function at the end of my program :
folder_prefix = "Fail Case-1"
copylogs(Appache,Errorlog, folder_prefix)
I have an issue here . Programm executes successfully but the logs get overwritten .what i mean is first Appache folder gets created ,logs are copied and then once again it gets overwritten .
What i require is : create a folder Appachelogs[with the timestamp as defined ] ,copy the logs from machine one , next copy error logs from machine2 , and continue the program
How can this be achieved?
scp by default overwrites if a same file name exists in the target computer.
I would suggest using a combination of a error file name + the timestamp for naming the error logs. It's always a good convention for logs to have a timestamp in the name and they also prevent the overwriting problem you are experiencing.
Consider using rsync instead of scp
Do your logs have the same filenames on both machines? scp will overwrite them if they do.
Personally I would have two directories, one for each machine. Or use a timestamp as suggested by Sylar in his answer.

Categories

Resources