How to Make a Backup Script for Shell in Linux
For those who hack a lot of Linux at the command line interface, backing up files such as config files before editing is a good habit to have. IMHO anyway. So to save time, a little script can be written, chmod +x, and placed into /usr/local/bin or some other that users have in their executable shell PATH. I name mine “bu” to make it easy, and it takes one command line argument which is the filename of the file you want to make a backup copy of. It is called by running something like $ bu myfile
Here’s the code. Comments welcome
The code creates a timecode based on today’s date and the current time to the second, and appends that to the original filename. The a copy is saved with this new “timestamped” backup filename, right in the same directory. Easy-peasy!
#!/bin/bash
OLDFILENAME=$1
DATECODE=$(date +%Y%m%d)
TIMECODE=$(date +%H%M%S)
NEWFILENAME="${OLDFILENAME}_backup${DATECODE}-$TIMECODE"
cp $OLDFILENAME $NEWFILENAME
SUCCESS=$?
if [ $SUCCESS -eq 0 ];then
echo "OK! Copied \"$OLDFILENAME\" to \"$NEWFILENAME\""
else
echo "FAIL! You'll need to try again!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
fi
Posted under Linux
This post was written by kb-admin on April 13, 2010
How To Make puTTY Automatically Load a Session
The most awesome emulator of all time, puTTY.exe, just got even easier to use. Along with loggiong automatically into a SSH session add the Windows shortcut that loads a saved session and launches it, now you have one click shell access to your Linux host from your Windows PC.
Here’s how:
- Download puTTY.exe
- Save it to the folder C:\puTTY\
- Open a Windows Explorer window in C:\puTTY\
- Run puTTY.exe once, and create a “saved session”, making note of what you name it. My example below uses the name my neatly named Saved Session
- Right-click-drag puTTY.exe and drop it next to itself, this creates a shortcut to the .exe file.
- Right-click the shortcut you just created, on the popup menu click Properties.
- In the Target box, add -load “your-saved-session-name” after C:\putty\putty.exe
- The final content in the target box should look like:
C:\putty\putty.exe -load "my neatly named Saved Session"
- Save the shortcut. Viola! Move or copy this shortcut anywhere you like (e.g. your Desktop, your QuickLaunch toolbar, your custom explorer toolbar, etc.) and you have 1-click access to a command prompt on your Linux / Unix host.
Enjoy!
Posted under Apple, Freeware, Linux, Microsoft, Network, Software, WebDev, ZyXel
This post was written by kb-admin on December 5, 2009
Use puTTY to automatically login a SSH session
Many thanks to Jon Lee at jonlee.ca for this excellent procedure allowing for the automtic login of a session using SSH and puTTY.exe terminal emulator. You da man!
——————————-
From his site:
As many web developers can attest to, logging into your server through SSH (Secure Shell) is one of the more common day-to-day tasks (you can even use it as a secure tunnel for your traffic). It only makes sense to automate this process which in turn can save many many keystrokes.
This how-to is written with PuTTY and Windows in mind and requires several other tools that are available from PuTTY’s website. So from their download page, make sure you have these files:
- PuTTY (putty.exe)
- PuTTYgen (puttygen.exe)
Then to automate SSH login, do the following:
- Run PuTTYgen.
- Select SSH-2 DSA as the Type of Key to generate.
- Click generate and move your mouse around to generate randomness.
- Click “Save Private Key” and save it somewhere on your computer.
- Copy the entire content inside the box to your clipboard (this is your generated public key).
- Login to your SSH server.
- Create the file ~/.ssh/authorized_keys containing the generated public key (from step 3) on a single line.
- Make this file readable (chmod 755).
- Then open up PuTTY and navigate to Connection->Data and fill in the auto-login username.
- Navigate to Connection->SSH->Auth and under Private-key, browse to the file you had saved earlier on your computer.
That’s it! Now you can try logging in to your SSH server and it should login automatically. If it works, make sure you save your session so you don’t have to repeat these steps every time!
Hopefully these steps work for everyone! Let me know if there are any problems.
——————————-
Had some problems with a CentOS5 server not accepting keys… found that this server was being finicky for some reason, and used this article on how to generate the keys on the Linux server, and then import the public key to the client Windows box. To make it automatically login simply do not enter any passphrase. This is probably a huge security risk or something like that, but if you’re using it on a secured LAN then perhaps it’s ok.
This post was written by kb-admin on November 27, 2009
Alternate Time Servers for Automatic Clock Set
In modern Microsoft Windows operating systems and all Unix / Linux systems the system clock time can be set automatically. The computer will connect to a special server on the Internet called a Network Time Protocol server, and get the current universal time, and then adjust it according to the local time zone on the computer. It keeps the computer clock very accurate and corrects drift before it can become much of a problem.
We recommend using a reliable server: pool.ntp.org
A wonderful group of generous folks provide this extremely reliable cluster of computers for us all to use, free of charge. Thank you NTP Pool Project and all the participants!
Posted under Freeware, Linux, WebDev
This post was written by kb-admin on November 16, 2009
How To Copy Directories and Subdirectories Recursively With FTP (scp)
When transferring file directory structures between linux / Unix hosts, usinf FTP was what came to mind. FTP has been used for many transfers in the past, but when forced (read:allowed) to use the command line to transfer files, the MGET and other FTP related commands were useless. So google to the rescue, and up pops this great simple writeup about how to copy host-to-host using the SCP command. Sweetness defined.
In essence:
scp -vr -P 2222 ./* REMOTEUSERNAME@REMOTEHOSTNAME.TLD:/FULL/UNIX/PATH/TO/DESTINATION/FOLDER/(OR/FILENAMES.ABC)
This command will Verbosely and Recirsively do it’s thang. It will contact the remote host on port 2222 instead of the default port 22 used for SSH. The remote username is the unix username, and the remote hostname is the full DNS name or IP address of the remote unix box. The destination path is reltive to the root of the system, NOT relative to the user’s home dirtectory.
Have fun, and leave FTP for transferring single files or batches of files inside a single directory container only.
Posted under Freeware, Linux, Network
This post was written by kb-admin on October 23, 2009
How to CHMOD on files but not directories (inodes)
From Linuxquestions.org:
“There are probably several methods, but one is to use find to produce a list of all files (not directories) and then execute chmod on each of them. For example
find /my/directory -type f -exec chmod 644 '{}' +
Change the red parts to fit your needs. If you wonder what that ‘{}’ is..well, you may have guessed that it’s where the filelist is being put when exec’ing the given command on each file.
You can also first try the command without chmod’ing to see that it affects the right files:
find /my/directory -type f
The above would find all regular files (not directories, for example) from within /my/directory.
If the command happens to throw you an error about the exec part, chances are it’s because of the plus sign (+) that ends the exec part. In this case try replacing the plus (+) with an escaped semicolon (\;) so it becomes
find /my/directory -type f -exec chmod 644 '{}' \;
On some machines I remember that it worked with semicolon (which needs a backslash in front of it, to protect it from being interpreted by your shell), but on my current installation it’s the plus sign (without a backslash).
Another way would probably be to list all regular files on the directory (using either find or any tool that can just list all files without directories) and then pipe the output to xargs with which the chmod was run.”
and from http://movabletripe.com/archive/recursively-chmod-directories-only/
”
June 19th, 2006
find . -type d -exec chmod 755 {} \;
This will recursively search your directory tree (starting at dir ‘dot’) and chmod 755 all directories only.
Similarly, the following will chmod all files only (and ignore the directories):
find . -type f -exec chmod 644 {} \;
“
Posted under Linux
This post was written by kb-admin on October 17, 2009
Fedora 9 connect: Network is unreachable error
Ran across this installing Red Hat’s free Linux distribution Fedora Core 9 code named Suplhur. Installed totally vanilla install with the GUI anaconda front end. The box sees the NIC, and can ping within the local subnet of the LAN network, but can’t ping out. BTW, it holds a static IP on the network interface card (NIC). Gets the error:
connect: Network is unreachable error
We need to set a default route. Here’s how:
Looked in /etc/sysconfig/network-scripts for the file route-eth0 but it wasn’t there.
Using vi (you can use whichever text editor you prefer) I created that file route-eth0, and put in this one line:
defult via 192.168.0.1
(note: 192.168.0.1 is the LAN IP address of my router. Your router IP address may differ. So you should put in whatever the IP addres of your router is, instead. Most Netgear routers and Qwest DSL boradband modems use 192.168.0.1 and Linksys uses 192.168.1.1 and Belkin uses 192.168.2.1 just to name some common ones.)
Then a simple task of restarting the network and testing:
# service network restart
# ping yahoo.com
Success!
Also, I found that the ethernet adapters weren’t starting automatically. Using the GUI taskbar/ Start Menu, I went into System Administration, then into Services, and enabled the “network” service. Then change runlevel to 3. Ping works so eth0 is up, and that happened at the runlevel change. Change to runlevel 5, test, and… yeppers, it works.
Posted under Freeware, Linux, Network
This post was written by kb-admin on September 24, 2008
Fix 403 Forbidden error in OTRS/Apache
This applies to (at least) OTRS 2.2 on Fedora Core 6 running Apache 2:
To fix this, change the following in the file /etc/httpd/conf.c/otrs.conf
Find the line that reads “Deny from All”, and comment it out by placing a # at the beginning of the line.
Save the file.
Restart Apache
# service httpd restart
Now try browsing your webserver otrs from other machines, success!
This post was written by kb-admin on December 4, 2007
Set Windows clock to UTC time
Save the following lines as utc.reg, and then run it to import this registry tweak. It allows you to set the hardware clock in your PC’s BIOS to UTC time. This is handy for boot dual-booting Mac, or Linux, when those operating systems are set to read the BIOS clock as UTC time, instead of Windows’ preferred Local Time (ie. PST, PDT, MST, MDT, CST, CDT, EST, EDT, or the standard “GMT-
Here is the code to save as utc.reg:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation]
“RealTimeIsUniversal”=dword:00000001
Posted under Apple, Linux, Microsoft
This post was written by kb-admin on October 30, 2007