I have an Oracle archives folder on windows, which I need to take an incremental backup everyday at 6:00AM.
I need to copy all the files generated during the previous day, and place them in a folder with today's date.
What is needed is that, the files generated after the last backup was taken, only should be copied [i.e, the file names with the sequence after yesterday's backup's last file].
I tried xcopy, but it doesn't provide any facility for copying files based on modified time.
I need to write a batch script for this, please help me out!
xcopy provides methods for copying files based on their Archive attribute. The option you would likely want is /M, which copies only files with the Archive attribute set, and resets that attribute. It kind of relies upon the Archive attribute being set, but Windows does this by default (I think) when creating or modifying a file.
For example (a rubbish example, but an example nonetheless):
C:\tmp>echo hello > out.txt
C:\tmp>xcopy /M *.* ..
C:out.txt
1 File(s) copied
C:\tmp>xcopy /M *.* ..
0 File(s) copied
C:\tmp>echo hello > out2.txt
C:\tmp>xcopy /M *.* ..
C:out2.txt
1 File(s) copied
Only files that are new/modified since the last copy are copied.
Alternatively, depending on your Windows version, you could look into the much more powerful (and hence more confusing) robocopy.
Unless the database is shutdown, you can have data inconsistency problems if you attempt to backup the raw files on the disk.
A really great reference for all things backup is an O'Reilly title: Backup & Recovery
Each database will have it's own methods of running backups while the database is live. Here's Oracle's page.
Related
I am new to this , Actually my requirement is to take backup of some XML file from one machine to another machine .So my senario would be when i click on the batch file it will create a new folder on the second server (name of the folder should be date and time )and under this folder it paste all the copied files .
I need to create a new folder every time as i am doing version control.
Thanks
VG
The batch file is very simple
#echo off
setlocal
set "copydest=%date%_%time%"
for %%i in (/ - : .) do call set "copydest=%%copydest:%%~s=%%
xcopy "c:\path\to\your\datafiles\*.xml" "c:\parent\%copydest%\"
which should copy the .xml files from the directory c:\path\to\your\datafiles\ to a new directory under c:\parent\which has the date/time as its name.
The precise structure of that date/time depends on your settings. If you don't tell us what your settings are, we'd have to write a book about all of the possible combinations - not going to do that.
The for %%i... line does the removal of the common date/time separators; of these : and / are the important ones since they are invalid in a filename.
I want to create a backup Batch file that meets these requirements:
Requirement:
1-Only copy the source files if the source file got modified.
2-If the destination contains the files/folders that do not exist in the source, then the destination files/folders will be deleted.
3-Copies all subdirectories, even if they are empty.
4-If something happened that make the coping process uncompleted (ie copied unsuccessfully), the program will revert & keep the old version of the destination folder.
5-The batch file should run at the time i turn off my PC or at 7pm Daily.
So, i tried this
xcopy C:\MyProject N:\backup\MyProject /V /Y /E /D
Note: /v=Verify, /y=No prompting /e=subdirs /d=Copy only changed
However, after running the batch, it did not delete the files/folders mentioned in requirement 2. Also, i don't know how to do the requirement 4 & 5.
I searched many questions but seem they didn't have the code to meet requirement 2 & 4 & 5.
I don't want to use backup tool in Win7 since it is very heavy & it requires a big backup disk space. I prefer something simple (why didn't Window make things simple?? ).
Backup is very important when u do the project, I hope my question will help a lot of other programmers.
Can anyone know a good solution for this?
If you want a mirror backup, which only copies new and changed files, removes files that no longer exist in the source - then Robocopy is built in and can do that. A batch file that is scheduled at 7pm daily will work.
Your requirement #4 is something that I haven't seen in any backup programs, including Robocopy.
I recently wrote a batch file like this for an old XP machine, since I didn't trust the format produced by NTbackup to be readable on future machines. E.g. Windows 7 and 8 require a download of some sort of converter: forget that. My batch file lives in the root C:\ directory, and I have a shortcut in my "tools" program list.
Write the file in Notepad, and save as e.g. c:\backup.bat. Run it from the "Run" command. The first time it runs, all the directories you specify will be copied exactly to your backup drive, including all subdirectories (I use a 4 Gbyte thumb drive: you may well need larger). The first time, my small 3 GB or so of stuff took about 4 minutes on an old XP machine.
Interestingly, the total uncompressed backup produced this way is no larger than the backup produced by NTbackup in Windows XP in that program's unreadable format.
Here's a batch file, with comments (REM)...
REM backup.bat
The xcopy line below is for stuff in directory c:\anydir and its subdirectories, being copied to a directory \anydir on an e: drive (e.g. USB drive).
REM The actual command:
xcopy c:\anydir e:\anydir /i /s /y /d
For other directories, just change the c:\directory and e:\directory names in the xcopy command. Watch out, however, for names like My Pictures, with a space: you must rename them (just delete the space), so that DOS recognizes them.
REM finish
exit
The /i means make any (sub)directories that don't exist on the backup drive. This therefore copies the whole branch from the c: directory where you start.
The /s copies source directories and subdirectories unless they are empty.
The /y says don't ask about over-writing existing files.
The /d says only copy files of a later date: perfect for backup, and very fast.
The files are exactly as original: no weird backup format.
With regard to the questioner's question #4, I think just running the backup again would overwrite any failure. But if that fails, wipe your backup drive and start over. And question #5...XP (and I think 7) allow scheduled tasks from the Startup menu: just insert c:\backup.bat in the list. But I haven't tried that.
I am trying to copy all .txt files that I have scattered throughout several subdirectories of one main directory into another directory using a batch file. I have research this site and found lots of answers at this link: batch file Copy files with certain extensions from multiple directories into one directory. Like the code below from Jay:
set dSource=C:\Main directory\sub directory
set dTarget=D:\Documents
set fType=*.doc
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
copy /V "%%f" "%dTarget%\" 2>nul
)
My question is how to modify this code or other codes on this link to batch copy the files with time stamps, like I only want to copy .txt files created from Jan 1, 2012 to Nov 1, 2012.
My suggestion for finding and moving *.txt files in a directory tree of a drive, or the entire drive, or even multiple drives with last modification date in a definite time period is:
Start Windows Explorer.
Click on button Search.
Open advanced search options for finding files and folders.
Select/enter to search for files by last modification date.
Enter the two dates to specify time period or select the time period.
Run the search.
Select all found files in search result, for example with Ctrl+A.
Press Ctrl+X to mark the found files for being cut (moved).
Open the folder into which the files should be moved.
Press Ctrl+V to paste the files (move them).
That's it.
Nobody needs to code a batch job for this task if this find + move files job should not be done periodically using a scheduling task.
The exact steps for doing such an advanced find for files in a definite time period with Windows Explorer depends on version of Windows. See for example the computer tips
Find files by date modified in Windows for Windows 8/7/Vista, or
for Windows XP: How do I search for a file on my computer?
And of course there are many freeware and shareware tools which support also finding files according to various search criterias like last modification date within a specified time period and move them.
Well, that does not really answer the question as it does not contain the batch code for doing the job. So I answer this question with another question:
Why thinking about coding a batch file for such a task hard to adapt for varying dates if dozens of GUI applications including Windows Explorer exist doing the same by simple user input with no need on coding skills and therefore very easy to use, and the find + move must be done only once or from time to time with changed criterias?
Is there a way to move files in a directory (we have 1.7 million) to folders based upon a date?
WOuld like to move all files created between 1-1-2010 and 2-1-2010 to a specific folder
You can use the Robocopy feature. It comes as default in Windows Vista and Windows 7 and you can download it in Windows XP in the microsoft website.
If your Windows is 64 bit it even move files that have a path longer than 256 characters, unlike the CTRL+C, CTRL+V on Windows Explorer (I can't understand why). To see the program help you can write the following in the DOS Prompt (example, usually you can't write to the root):
robocopy /? > c:\robocopyhelp.txt
Use the switches "/MINAGE" for setting the minimum age of the file to be copied/moved and "/MAXAGE" for setting the maximum age.
I've never moved files before and never tried to filter them by age, but I think the syntax should be (from drive F to G, for example, and only 2011 files):
robocopy F:\ G:\ /MOVE /MAXAGE:20110101 /MINAGE:20111231
Plus other parameters described on the "robocopy /?". Usually I add "/R:0 /W:0", for it not try to access systems files (can help if you run the batch file with administrator privileges) 1 million times with a wait time of 2 seconds for each system file it can't copy/move (2 million seconds or 23 days for just pagefile.sys and hiberfil.sys). And the "/A-:H" switch to un-hide the hidden files.
Keep in mind also the existence of NTFS Junctions (infinite loop in the C:\users directory) and encrypted directories and use the according switches.
This won't be very nice, but you could use forfiles twice. Once to move all files with a date greater than 2010-01-01 to the folder and a second time to move all files with a date greater than 2010-??-?? (can't parse your date format reliably) back to the original folder.
Not pretty, definitely.
By default, copying from the command prompt will prompt you to overwrite files that already exist in the target location.
You can add "/Y" to say "Yes to all" replacements.
But how can you say "No to all" ?
In other words, I want to copy everything from one directory that does not already exist in the target.
The closest thing I see is the XCOPY argument to only copy things after a specific mod-datetime.
Unless there's a scenario where you'd not want to copy existing files in the source that have changed since the last copy, why not use XCOPY with /D without specifying a date?
echo "No" | copy/-Y c:\source c:\Dest\
You can make a text file with a single long line of "n" then run your command and put < nc.txt after it. I did this to copy over 145,000 instances where "No overwrite" was what I wanted and it worked fine this way.
Or you can just hold the n key down with something, but that takes longer than using the < to pipe it in.
Here's a workaround. If you want to copy everything from A that does not already exist in B:
Copy A to a new directory C.
Copy B to C, overwriting anything that overlaps with A.
Copy C to B.
I use XCOPY with the following parameters for copying .NET assemblies:
/D /Y /R /H
/D:m-d-y - Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.
/Y - Suppresses prompting to confirm you want to overwrite an existing destination file.
/R - Overwrites read-only files.
/H - Copies hidden and system files also.
I know you all think /D: date is going to use date stuff, but just /D without the: does exactly what we want so...
xcopy {Source} {Destination} /E /D
Will copy without overwriting to pickup those files that are new or maybe failed before for some reason.
Just try it, it works.
I expect xxcopy has an option for that.
Bingo:
http://www.xxcopy.com/xxcopy27.htm#tag_231
2.3 By comparison with the file in destination
The switches in this group select files based on the
comparison between the files in the source and those in
the destination. They are often used for periodic backup
and directory synchronization purposes. These switches
were originally created as variations of directory backup.
They are also convenient for selecting files for deletion.
2.3.1 by Presence/Absence
The /BB and /U switches are the two switches which select
files by the pure presence or absence as the criteria.
Other switches in the this group (Group 2.3) are also
affected by the file in the destination, but for a
particular characteristics for comparison's sake.
/BB Selects files that are present in source but not in destination.
/U Selects files that are present in both source and destination.
-Adam
this works fine
no | cp -rf c:\source c:\Dest\
echo N | copy /-y $(SolutionDir)SomeDir $(OutDir)
Try this:
robocopy "source" "destination" /e /b /copyall /xo /it
Copy that line into notepad and save as a .bat file. Run the file and it will copy everything from the source to the destination. When you run it again it will not replace files that are identical. when you change or a file changes it will replace the file at the destination.
test it out. I created a .txt file with a few works, ran the script, change the wording on the .txt file and ran the script again, it replace only the change file from the source.
/e=Copies subdirectories. Note that this option includes empty directories
/b=Copies files in Backup mode
/copyall=Copies all file information
/xo=Excludes older files. (this is what prevents it from copy the same file over and over)
/it=Includes "tweaked" files. (this will allow the copy and replace of modified files)
Thanks for this. I am using the command line utility AzCopy (v 3.1.0.93) to move ~1 million files from my local PC to my Azure blob storage. I've got some duplicates and cannot babysit the copy to answer each prompt and don't want to re-upload the same file.
The AzCopy utility offers a /Y command to suppress the confirmation prompts but ends up telling it to overwrite the destination file. Going this route I was able to get it to NOT re-upload the file. However, it does seem like a bit of a hack since it is not actually answering the prompt with "No", instead I get the error "No input is received when user needed to make a choice among several given options." but does not upload the file.
Here is the command I used: echo n | AzCopy /Source:"{file path}" /Dest:"{blob storage URL}" /DestKey:{key}
Hope this helps the next guy.
Depending on the size and number of files being copied, you could copy the destination directory over the source first with "yes to all", then do the original copy you were doing, also with "yes to all" set. That should give you the same results.
We used "robocopy" through "invoke-command" to copy a huge amount of VMs in our environment. We've discovered that "robocopy" unexpectedly exits sometimes and the whole proccess goes to down. So we've decided to use "xcopy". Now we're checking it's work and to "create" "Not for all" option we use that function (powershell):
function gen_long_no([string]$path) {
$result = ""; Get-ChildItem $path -Recurse | ? { if ($_.PSIsContainer -eq $false) { $result += "n" } };
return $result
}
Maybe helps somebody.
Adding the switches for subdirectories and verification work just fine.
echo n | xcopy/-Y/s/e/v c:\source*.* c:\Dest\