Archive for February, 2013

How to get fresh file/mysql backup from remote on minimal effort.

19/02/2013 Leave a comment

Lets assume that we have a remote web server (with ssh access) and we want fresh backup of files and mysql databases. Of course we can login, type some commands and then sftp those files but lets do this on minimum effort:

Backup files (tar, gzip and ‘download’ them):

ssh user2@remote "tar cf - /var/www/somepage | gzip -c" > files.tar.gz

Backup MySQL database:

ssh user2@remote "mysqldump -pXXXXXX database | gzip -c" > mysql.gz

Lets merge those command and write a little bash script that will automate those activities:

now=$(date +"%Y%m%d%H%M")
ssh user2@remote "mysqldump -pXXXXXX databases | gzip -c" > $bckdir/$now.mysql.gz
ssh user2@remote "tar cf - /var/www/somepage | gzip -c" > $bckdir/$now.files.tar.gz

XXXXXXX is a mysql password,
database is our db to backup.

Categories: Bash, Linux, Useful commands

How to download archived videos from

19/02/2013 1 comment

Working source here.

This php script will download all archived videos from (and probably from – because there is same api as on twitch) using wget. All videos would be saved to download folder (create it first) with date as a name and ‘show title’. File name is limited to 50 chars.

Downloading is done by parsing json files from justin, more info: Channel/archives api wiki.

$url = ""; //where xxxx is a channel id or login
$c = curl_init($url);
$options = array(
    CURLOPT_HTTPHEADER => array('Content-type: application/json')
curl_setopt_array( $c, $options );
$json = curl_exec($c);
$json_a = json_decode($json,true);
$json_a = array_reverse($json_a);
foreach($json_a as $k => $v) {
        exec('wget -c '.$v['video_file_url'].' -O "downloads/'.format_date($v['created_on']).' - '.format_title($v['title']).'.flv"', $array);
        echo implode('"\n"', $array);
function format_date($date){
    $out = preg_replace('/[^0-9\.]/', '', $date);
    return $out;
function format_title($title){ //format title and limit to 50 chars
    $out = preg_replace('/[^a-zA-Z \.]/','', $title);
    $out = substr($out, 0, 50);
    return $out;
function history_check($date){
    $data = file_get_contents('history');
    if(strpos($data, $date) !== FALSE)
        $out = TRUE;
        $out = FALSE;
return $out;
function history_add($date){
    file_put_contents('history', $date."\n", FILE_APPEND);

Create download dir, save this source as get.php file and simply run it from command line (php-cli need to be installed):

php get.php

Take in mind that information about downloaded videos is stored in history file. This will avoid downloading videos which was downloaded before.

Run command on each file in directory

19/02/2013 Leave a comment

Most useful when you have many files in directory and bash doesn’t want to cooperate:

-bash: -: Argument list too long

First one:

for file in *; do ls -al "$file"; done

where * could be a file name or extension ex.: *.jpg

And much more easy to remember:

find . -exec ls -al {} \;
Categories: Bash, Linux, Useful commands

Change the created/modified time of files

03/02/2013 Leave a comment

You can manually change created/modified/last accessed time of files using power shell and bellow command:

Get-ChildItem * | foreach { $_.CreationTime = get-date "3/2/2013 1:00 pm"; $_.LastWriteTime
 = get-date "3/2/2013 1:00 pm"; $_.LastAccessTime = get-date "3/2/2013 1:00 pm" }

Where * is a wildcard for all files in current directory. You can simply change it to file name etc.

Categories: Windows