Find Unused mdf and ldf file locations - sql-server-2008

We can find mdf and ldf file locations either by using sp_helpdb or using the below command.
SELECT * FROM sys.master_files
But how can we find ldf and mdf files which are not being used at present i.e Let say you restored one database with new mdf and ldf files and not deleted old mdf and ldf files .

There isn't a straight forward easy way. You can manually do this by searching the server or there is a more complex way spelled out here:
Finding un-used data files

You can try the below way:
IF OBJECT_ID(N'tempdb.dbo.#AllMDFfiles', N'U') IS NOT NULL
BEGIN DROP TABLE #AllMDFfiles END
CREATE TABLE #AllMDFfiles (mdfFileName VARCHAR(max))
-- Will return all the .mdf and .ldf in C:\ drive, you can change it in ... /d C:\ && dir /b....
INSERT INTO #AllMDFfiles EXEC xp_cmdshell 'cmd /c "cd /d C:\ && dir /b /s | sort | findstr /c:".mdf" /c:".ldf""'
--Will return the mdfs and ldf paths which are not in sys.master_files
select mdfFileName from #AllMDFfiles
Where mdfFileName IS NOT NULL
EXCEPT
SELECT physical_name FROM sys.master_files

Related

How to make general_log(mysql) file into daily copy?

Need help on this.
I would like to make my general_log file for mysql from single huge file into a daily log file.
I created a batch file to rename and create anew general log file,
but it still write into the renamed file.
Are there a config in mysql or am I missing a command here, so that the general log will not write on the renamed file / or just create a new general log / or will just write to the new empty file? thanks in advance
btw, batch file is below:
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set
dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
ren C:\wamp\logs\generalmysql.log "generalmysql - %stamp%.log"
#echo off
echo.>"C:\wamp\logs\generalmysql.log"
Screenshot
solved my issue by flushing the logs
call "C:\wamp\bin\mysql\mysql5.7.14\bin\mysqladmin.exe" -u'root' -h localhost flush-logs

How to seperate out ERROR FILE from Correct file using SSIS and archive them

I have a scenario where there are two source file ABC.txt and DEF.txt. i want to make sure that if any files are present in that folder with naming convention as ABC.txt, it has to be moved to the different folder prior to loading to the SQL server, and the other (incorrect file) should be moved to a error log folder (separate folder). Can someone advice what task would i use to do that. i tried it using File system task but it just allow you to move or copy files from one location to other prior to loading the file.can someone advice how to i copy that incorrect file to the different folder ( error log folder)?
try to use a bat file.
move ABC.txt /directory
and after
xcopy "source" "destination_err\" /exclude:%temp%\ABC.txt /y
or you can use robocopy command in bat file, this is the command with parameters http://itsvista.com/2007/03/robocopy/

Windows Batch Script for 2 Cases

I use the following .bat script
set varSearch="C:\Users\User1\Desktop\Test-folder\*.crypt8"
for /f %%i in ('dir %varSearch% /B ') do set varSearch= %%i
WhatsAppViewer.exe -decrypt8 %myName% key exp.db
sqlite3.exe exp.db<command.txt
cd C:\xampp\mysql\bin
mysql -u admin -p1234 < query.txt
The basic function is to find a file thats ending with .crypt8, decrypt it, save as csv and import to mysql. Its working correctly
But i need some extra features
Case1
The folder contains more than 1 file, and every file has to be processed, but only once
Case 2
Everyday at least one file gets added. It would be superb if the .bat could be scheduled as a task, and run every night and just process the new added files.
Does anybody has a solution for this?
Case 2
The forfiles command processes groups of files based on date. This does files made today only.
forfiles /d 0 /m *.crypt8 /c "cmd /c echo #fname in #path"
Case 1
Your code has errors, it may work but not under all conditions.
The easist way is to put the sequence of commands in a batchfile for a file (%1) which is passed on command line, and use forfiles to call it.

Bash script - splitting .TAR file in smaller parts

this a part of a .sh script I need to edit to make some backups and upload them on Dropbox but I need to split that backup in smaller parts.
NOW=$(date +"%Y.%m.%d")
DESTFILE="$BACKUP_DST/$NOW.tgz"
# Backup mysql.
mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS --all-databases > "$NOW-Databases.sql"
tar cfz "$DESTFILE" "$NOW-Databases.sql"
And then the function to upload the backup on DropBox....
dropboxUpload "$DESTFILE"
How can I split the .tar file in smaller parts (for example of 100 or 200mb size) and get the name and the number of those files to upload them with the dropboxUpload function?
You could use split. For example, this:
split -b500k $DESTFILE ${DESTFILE}-
will split $DESTFILE into 500 KB pieces called:
${DESTFILE}-aa
${DESTFILE}-ab
${DESTFILE}-ac
...
Then you could loop through them with something like:
for x in ${DESTFILE}-*
do
dropboxUpload $x
end
To join binary files in windows, use
copy /b parts.. dest
/a is for ASCII text files.

MySQL load command: is there a wildcard available for the datafile?

I have the following file dumped daily into one of our online directories:
dat-part2-489359-43535-toward.txt
The numbers change each day randomly.
I have the following code to try and LOAD the file:
mysql_query("LOAD DATA LOCAL INFILE 'dat-part2-%-toward.txt'
REPLACE INTO TABLE my_table
FIELDS TERMINATED BY ',' ENCLOSED BY ''
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES") or die(mysql_error());
And of course no luck. What's the best way to do this?
assuming this is a scheduled job, why not check the directory for the most recent file that matches your filename template. Store the name of said file in a variable and then sub the variable into your query. Check out glob()
I would do it via a shell script or windows cmd file. This assumes you created the file with mysqldump or some program that creates a valid sql script.
You can run dir /B in windows commend to get a directory of files. So do a dir /B > input.txt then use Windows scripting to read the input file and for each line, pipe the file in using the mysql client.
# echo off set infile= %%1
for /f "eol= tokens=*
delims= usebackq" %%i in (%infile%)
do ( mysql -u userName --password=pwd < %%i )
It's been a long time since I wrote any windows scripts, but that should give you an idea of an approach.