Friday, June 15, 2012

Remotely copy hosts file to Windows 7


I have users that use laptops to work remote.  Unfortunately, when they VPN into to our server they can route, but they have problems with DNS.   So for these users I have a hosts file setup with the servers that they need.   Normally, I would just go to their C drive \\computername\c$  and drill down and manually replace it.   That works fine, but since I started using PDQ Deploy - From Admin Arsenal (This is a super Great application - I strongly recommend it) I wanted to see if I could deploy the hosts file to my Windows 7 Professional laptops using a batch file. 

I ran into all kinds of access denied problems.  Everywhere I searched on the Internet for help I ran into you can't it is a security feature in Windows 7.   I tried many things that I found online, but with no success.  That was until I ran into this post: http://stackoverflow.com/questions/10724591/how-to-remote-execute-an-elevated-remote-script-in-powershell.

To be honest, I didn't know what "start-process" was, but since I have googled it.  After putting together my script, I know there more efficient ways to write this script, but for a quick batch file that is easy to follow this is what I wrote.


start-process cmd.exe -verb runas /env /user:username@domainname.com
echo y |copy "\\servername\apps\misc\Windows 7 hosts file\hosts" C:\windows\temp
echo y |copy "\\servername\apps\misc\Windows 7 hosts file\hosts.orig" C:\windows\temp
echo y |copy C:\windows\temp\hosts C:\windows\system32\drivers\etc\hosts
echo y |copy C:\windows\temp\hosts.orig C:\windows\system32\drivers\etc\hosts.orig

Let me explain the script a little bit.   I didn't test, but maybe I could have copied from the network share to C:\windows\system32\drivers\etc directly, but I remember getting an error about copying from a network location, so I decided to copy to the temp directory on the computer and then copy over the original hosts file under the etc folder.   Please experiment or write better code or show me how you would have done this.    Also, I have a copy of an original windows 7 hosts file that I copy into place (the etc folder) so that if I ever need to change it or the user needs to change it while on the road it is there.   

The "echo y" just answers the questions that I do want to overwrite the original file.  I named my batch file copyhost.bat

Now that I have the script written Let's turn to PDQ Deploy and set it up to depoly the hosts file.




If you are already familiar with PDQ Deploy you know how easy it is to setup deploying files.   I just choose my batch file and include the entire directory which includes "copyhost.bat", "hosts", "hosts.orig"  and that it.  I save it and the I just deploy it by choosing deploy and then choosing which computers I want to deploy this to.  Within seconds I'm done and I have a status report saying "successful".

Let me know if you figured out another way to do this.

Tuesday, June 5, 2012

Backup and Restore using mysqlbackup from MySQL Enterprise


All this was done in a lab.  Don't do this on a production server.  There is my warning and disclaimer.

I did a full backup of my MySQL instance which resides under /var/lib/mysql.   I then delete all the data, all the ib* files, and mysql.   After deleting those files I ran a full recovers.  Here is how I did it.

Full Backup
Run the following command to do a full backup.   What this will do is backup everything in /var/lib/mysql and with the backup-and-apply-log it will go back and grab anything that has changed during the backup.   Also, that argument prepares the backup for a restore.
./mysqlbackup --user=root --password  --backup-dir=/storage/backups backup-and-apply-log
 
You need to specify a backup directory that is empty of mysqlbackup will complain.
When the backup completes you will have a message at the end telling you that it was successful.

I cd to the location of my backup and everything looked like it was there.   I ran a du -sh * and results looked the same.

Full Restore
./mysqlbackup --defaults-file=/storage/backups/backup-my.cnf --datadir=/var/lib/mysql --backup-dir=/storage/backups copy-back

Under "--default-file=" I originally put /etc/my.cnf thinking that I should use the my.cnf file I created.  However, part of the backup creates a "backup-my.cnf" with a few lines in it.  You should use this file for the restore or you could put these lines in your restore command.  It is much easier to just use the file that was created.

I started mysql /etc/init.d/mysql start, but it failed with the PID error.   I looked at my restored directory and noticed many of the files was owned by root, so I ran this command on the mysql directory.
chown -R mysql:mysql mysql

Now MySQL starts and every thing looks great.