LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-31-2018, 04:49 AM   #1
coderlen
Member
 
Registered: May 2018
Location: Greenwood, IN
Distribution: Ubuntu
Posts: 74

Rep: Reputation: Disabled
Convert Windows xcopy script to Linux for Backup


I'm trying to convert a Windows script which I used for backups on my Windows computer. It worked very well in Windows, and now I would like to use the same idea on my Ubuntu files.

The Windows script just used a series of xcopy commands and named the directories I wanted backed up. It would go through and see if any of the files in each directory had changed, and then it would copy only those files to my flash drive. It worked like a charm. Very simple, very effective.

The flash drive ended up being a composite copy of all my important directories from the hard drive of my laptop. It was a life saver, and it saved me several times when my hard drive crashed.

I would like a Linux command line script to do the same. I did some research on the cp command, and it seems as if the "cp" command would be the answer, but I had some problems getting it to work correctly. I did get as far as getting the script to be executable, but it kept getting errors and never performed for me.

Here is an extract of the Windows bat file:
xcopy "C:\Bills" "D:\Bills" /s /m /y
xcopy "C:\Securities" "D:\Securities" /s /m /y
xcopy "C:\Taxes" "D:\Taxes" /s /m /y

Here is an extract of my Linux script, which is not working:
cp -ur /home/len/len/Bills /media/len/UUI/Bills
cp -ur /home/len/len/Securities /media/len/UUI/Securities
cp -ur /home/len/len/Taxes /media/len/UUI/Taxes

A little help, please. Thanks.
 
Old 05-31-2018, 05:36 AM   #2
pierre2
Member
 
Registered: May 2009
Location: Perth, AU
Distribution: LinuxMint
Posts: 386
Blog Entries: 8

Rep: Reputation: 88
that xcopy is an advanced version of copy,
- whereas cp in Linux is just a plain copy command.

and that UUI is also a variable, as well, and does that match the UUI of the destination ?.
 
Old 05-31-2018, 05:47 AM   #3
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,909

Rep: Reputation: 793Reputation: 793Reputation: 793Reputation: 793Reputation: 793Reputation: 793Reputation: 793
Code:
cp -ur /home/len/len/Bills/* /media/len/UUI/Bills/
 
1 members found this post helpful.
Old 05-31-2018, 06:33 AM   #4
xamaco
Member
 
Registered: Sep 2009
Location: Bastelicaccia, Corsica
Distribution: Crux
Posts: 47

Rep: Reputation: 7
I use rsync for my backups. It's fast and very versatile. It has a lot of options and needs a careful reading of its man page (which is good and contains examples).
 
1 members found this post helpful.
Old 05-31-2018, 06:56 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,064

Rep: Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294
Quote:
Originally Posted by xamaco View Post
I use rsync for my backups. It's fast and very versatile. It has a lot of options and needs a careful reading of its man page (which is good and contains examples).
And the advantage or rsync is that it won't copy files that are the same at the destination, making it much better and faster if there's a lot of files.
 
Old 05-31-2018, 06:57 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,913

Rep: Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249
Yes, you need to look at rsync if you want to only copy the changes, not all the files all the time as "cp" will do.

Note that your strategy exposes you as you only have one copy of your backup data. Backup devices also fail.
 
Old 05-31-2018, 07:41 AM   #7
coderlen
Member
 
Registered: May 2018
Location: Greenwood, IN
Distribution: Ubuntu
Posts: 74

Original Poster
Rep: Reputation: Disabled
Thanks teckk. I tried what you suggested. Here is the result on my Linux command line:

~/len/Backup$ ./cpToUSBOnlyChangedFilesBackupDriveE.sh
cp: target '/media/len/UUI/Bills/'$'\r' is not a directory
cp: target '/media/len/UUI/Securities/'$'\r' is not a directory
cp: target '/media/len/UUI/Taxes/'$'\r' is not a directory

What do I do to correct this?

And the others who responded with the rsync suggestions, I tried rsync after reading the man page, but I was unable to get it to work. Please suggest an actual example with the rsync command, with options, so that I can give it a try. Thanks.
 
Old 05-31-2018, 07:46 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,674

Rep: Reputation: 4793Reputation: 4793Reputation: 4793Reputation: 4793Reputation: 4793Reputation: 4793Reputation: 4793Reputation: 4793Reputation: 4793Reputation: 4793Reputation: 4793
you wrote your script in windows and all the lines contain a \r. You need to remove it first. see the tool dos2unix.
how did you try rsync?
 
Old 05-31-2018, 07:54 AM   #9
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
You really don't need to write a script. There are programs that will do exactly what you want. xamaco mentioned that he uses rsync. But he also said to completely read the man page. You don't really need to do that. For any ordinary copying this will do
Code:
$ rsync -av source destination
That will transfer only new or newer (more recent modify time) files. And, rsync does integrity checking, so you know the back up is sound.
 
Old 05-31-2018, 06:57 PM   #10
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 4,845

Rep: Reputation: 1768Reputation: 1768Reputation: 1768Reputation: 1768Reputation: 1768Reputation: 1768Reputation: 1768Reputation: 1768Reputation: 1768Reputation: 1768Reputation: 1768
Install, configure and use rsnapshot , which uses rsync.
Its configuration file will set up and automate multiple generations of backups. I use it to perform daily off-site backups.
 
Old 06-07-2018, 05:24 AM   #11
coderlen
Member
 
Registered: May 2018
Location: Greenwood, IN
Distribution: Ubuntu
Posts: 74

Original Poster
Rep: Reputation: Disabled
I had my doubts about rsync. The first time I used it, it took a long time to back up all the files, I guess since it was the first time doing it.

rsync -av /home/len/len/* /media/len/56E9-5630/

But later, when I had made some modifications to some of the files, and I ran that same command again, magically, it backed up ONLY the files which were changed, and it did it in a hurry. Wow! That's just exactly what I needed.

When I used to use Windows and did my backup script with xcopy, it took forever, even if there was only one file changed. rsync is super fast.

I did try rshapshot, as rcasey recommended. It was a bit intimidating for me, so I didn't try it. It looks OK though. I just can't speak to it, as I didn't try it.

teckk and AwesomeMachine, your responses helped me the most, although it took me awhile to get my arms around it. The command is so simple, even a child can execute it.

The only real problem I had was identifying exactly what my flash drive was called for the Destination, and exactly how to code the Source. I hard-coded the reference to the Destination as "56E9-5630". There's probably a better way of doing it, but this works for me, for now.

Thanks everyone, for your responses. I am astounded as to how fast rsync executes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I backup my C Drive with Windows and convert the entire filesyst as ISO file d9esco Linux - Newbie 3 09-03-2015 08:53 PM
Convert python script to windows executable ankit_mcgill Linux - Newbie 4 10-05-2008 01:21 AM
verify successful cp like xcopy does in windows? carl0ski Linux - General 2 11-03-2006 03:14 PM
xcopy functionality with linux? syssyphus Linux - General 12 11-27-2005 01:24 PM
dos xcopy to backup changes to samba susewho Linux - Software 1 02-01-2005 07:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration