Tag Archives: bash

A Backup Script For WordPress

I’ve written a backup script in order to get my WordPress blog backup automatically. The script is only usable on a Linux/Unix box, since it uses default GNU tools.

The script connects to the server via ssh, copies a folder to a location, dumps a database to the same place with the copied folder, creates a tar.gz out of it, then gets the new file via ftp to a prefered location.

The important thing here is that, you should add your ssh public key to the server so that ssh will connect automatically. I also use .my.cnf files to login mysql without specifying password, so you’d better do that. I’ve talked about it in an earlier post here.

Keep in mind that you need an ftp client to connect. If you don’t have it, install it using yum, apt or whatever.


HOST='' # ip address of your server
SSHUser='root' # user to connect as ssh
FTPUser='myfunkyftpusername' # user to connect as ftp
FTPPass='mysupersecretFTPpassword!' # ftp connection password
MYSQLUser='root' # # user to connect as MySQL

SSHPort=22 # change if different
FTPPort=21 # change if different
DB=wordpress # which database to backup?
DIRECTORY='/home/eaydin/public_html/wp-content' # directory to back up - server side
DIRWRITE='/home/eaydin/' # move the backup here on the server.
DROPBOX='/home/eaydin/Dropbox' # local file path to backup - host side. use your Dropbox folder?
FILENAME='wp-backup' # Filename to use for backups

DIRWRITE=${DIRWRITE%/} # remove trailing / from dir name.
FILENAME=${FILENAME%/} # remove trailing / from filename in case the user types it.
DATE=`eval date +%d%m%Y"-"%H%M` # create date format. (created on the host side, not server. depends on the host time setings.)
FILETAR=$FILENAME-$DATE.tar.gz # name of the tar.gz file (not path!)

ssh -t $SSHUser@$HOST -p $SSHPort "\
mysqldump --add-drop-table -u $MYSQLUser $DB > $DIRWRITE/$FILENAME-$DATE/wordpress.sql ;\
chown $FTPUser:$FTPser $DIRWRITE/$FILETAR ;\
ftp -n $HOST $FTPPort <<END_SCRIPT
quote USER $FTPUser
quote PASS $FTPPass
exit 0

The lines between 5 and 17 are the ones you should edit, they’re all self explained in the comments.

It’s a good idea to add the script to your crontab.
In order to do it, especially on Ubuntu systems, just add your current PATH value right below the /bin/sh line. Like this,

eaydin@eaVT:~$ echo $PATH

So you should add this line at the top of the script,

#!/bin/env bash

Or, just run the script whenever you want. I usually set the download path (defined as the $DROPBOX variable on line 16) to my Dropbox folder, this way my backups get automatically synced on the Dropbox server.

Converting YouTube Videos to MP3 Files in Linux

Well, there are dozens of way to do this. And when I tell people about this, they simply say “Why bother? There are tons of websites that do this for you!” Yeah, that is true, it’s nice to do some stuff on your own.

First of all, I’m not gonna go into detail on how to GET the video from YouTube. There are lots of add-ons to do this for Firefox and Opera etc. I’m not sure if there is any for Chrome, probably the folks at Google don’t like the idea that much. Actually at this Yahoo! Answers question, the Official YouTube account has answered why: It’s against their terms of service.  Anyway, using anyone of these add-ons you can download the video.

If you wanna go extra lower-level and do it without any add-on, you have get the videos source code in HTML5 and parse the necessary parts. For different browsers, YouTube acts differently of course, so you have to handle it accordingly. There’s an old discussion about this on stackoverflow.

Well, let’s say you’ve downloaded the video. As you can guess, it can be easily done with avconv (or ffmpeg) like,

$ avconv -i “my video.flv” “my video.mp3”

And that’s it! But, if you’re downloading lots of files (which I sometimes do for preparing playlists for the bands I play at) it is easier to do it in a bash line. The line below converts every file with the flv extension, and removes the “- YouTube” part from the name (the Opera add-on adds it) then deletes the flv file.

for i in *.flv; do avconv -i "$i" "${i// - YouTube.flv}".mp3; rm -f "$i"; done

When dealing with videos on YouTube, it is important to select which source to download. Don’t forget that the quality of the video also effects the quality of the sound. So getting a low quality to convert to mp3 will most likely result in a low quality mp3. Well, getting the highest quality video won’t help go easy on your bandwidth either.  Also video on YouTube don’t support every file type. So you have to choose between various codecs. A detailed list of codecs available on YouTbe are presented here in this wikipedia page : http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs

Now don’t worry, you don’t have to study that table to get the optimal output. I’ve done the homework for you guys. The table below is in such an order from high audio quality to low audio quality. If two of them are the same quality, it is ordered in a fashion that the latter one uses more bandwidth.

1) webm 720p (192k)
2) mp4 720p (192k)
3) webm 360p (128k)
4) flv 360p (128k)
5) flv 480p (128k)
6) webm 480p (128k)
7) mp4 270p (96k)
8) mp4 360p (96k)
9) flv 240p (64k)
10) flv 270p (64k)

So this means, downloading webm720p will cost less bandwidth than mp4 720p but will result in the same audio quality.

Unfortunately the one-liner above I’ve provided doesn’t always preserve the Audio quality on the output. This was discussed in a stackoverflow post earlier and I’ve provided my modified version of a bash script that uses ffmpeg to do the job.

#!/bin/env bash
for f in *.${ext}; do
    x=${f%.*} ;
    x=${x% - YouTube}; # I usually download some song covers from YouTube.
    bit=`ffmpeg -i "${f}" 2>&1 | grep Audio | awk -F", " '{print $5}' | cut -d' ' -f1`
    if [ -n "$bit" ]; then
        ffmpeg -i "$f" -ab ${bit}k "$x"
        ffmpeg -i "$f" "$x" # this is if we don't have an output from the grep line above, ffmpeg will keep the original quality, that is 192k for 192k

So this script gets the file extension you want to convert (flv ?) and greps the Audio quality of each of them and converts accordingly. Simply use it like this :

$ bash script.sh flv

Of course you’ll need the ffmpeg and libavcodec-extra-53 packages installed on Ubuntu.

$ sudo apt-get install ffmpeg libavcodec-extra-53


Adding Cronjob via Script

Here’s another snippet I don’t want to forget,
adding a cronjob via scripting.
Here’s the source for it, : http://stackoverflow.com/questions/878600/how-to-create-cronjob-using-bash

Don’t forget that it only works on BASH! Doesn’t work on sh.

command="$mypath/autograph.sh > /dev/null 2>&1"
job="*/5 * * * * $command"
cat <(grep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -

Setting the 1st Weekday of Calendar on Gnome 2

Well, here is something I always use, and always forget how it’s done.

Even though googling helps it, sometimes the fastest way is to check where you know it is, your own blog! So here it is…

By default, the Gnome calender shows the 1st day of the week as Sunday. It can be easily changed.

First, to see your local settings :

$ locale

Let’s say the output is en_US, then let’s back it up :

$ sudo cp /usr/share/i18n/locales/en_US /usr/share/i18n/locales/en_US.bak

Then edit your en_US file and change the value of first_weekday to 2.

first_weekday 2

If you wish to make Tuesday your first weekday of the calender, set it to 3 and so on.

Now update your locale’s.

$ sudo locale-gen

Now we need to restart gnome’s panel cause the calendar is on it, it need to be refreshed.

$ killall gnome-panel

That’s it!