background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

Adding a lot of untracked files in a Git working copy can be a pain. When I want move control over "git add *" or "git add .", I sometimes run the following and pipe to xargs (and optionally use grep to filter my selection of files). This allows you conditionally add files based on your grep.

# review file modifications
$ git status
#
# ...snip...
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# ../all/modules/contrib/examples/ajax_example/ajax_example.test
# ../all/modules/contrib/examples/contextual_links_example/
# ../all/modules/contrib/examples/examples.info
# ../all/modules/contrib/examples/examples.module
# ../all/modules/contrib/examples/form_example/
# ../all/modules/contrib/examples/pager_example/
# ../all/modules/contrib/examples/tablesort_example/
# ../all/modules/contrib/examples/theming_example/

# add all untracked files
$ git status -s | grep ^?? | awk '{print $2}' | xargs -i git add '{}'

For example, if you only wanted to add jpg files..

$ git status -s | grep ^?? | awk '{print $2}' | grep -i \.jpg$ | xargs -i git add '{}'

Eric.London's picture

In this tutorial, I'll show how you can use awk, grep, and sed (my favorite command line tools) to backup and archive your MySQL databases. This can be useful to schedule a cron job, transfer your databases to another server, or any other type of scripting.

First, you'll have to get acquainted with connecting to and dumping your database on the command line. Depending on your user, credentials, and where the databases are located, your command might look something like this. Please note, there is no space between the password and the "-p" flag.

$ mysqldump -u user -pPASSWORD -h hostname database > database.sql

To simplify my example, I'm going to shorten the mysqldump command to the follow.

$ mysqldump database > database.sql

Now that we're MySQL command line pros, I'll break down each command. I'll start by showing all the databases.

Eric-Londons-MacBook-Pro:backup Eric$ mysql --execute="show databases"
+---------------------+
| Database            |
+---------------------+
| customers           |
| db_pics_ericlondon  |
| db_thedrupalblog_d6 |
| drupal              |
| drupal-pics         |
| drupalmusicproject  |
| itunes              |
+---------------------+

Now, I'll "pipe" the output from the previous command into awk to show the first column data.

Eric-Londons-MacBook-Pro:backup Eric$ mysql --execute="show databases" | awk '{print $1}'
Database
customers
db_pics_ericlondon
db_thedrupalblog_d6
drupal
drupal-pics
drupalmusicproject
itunes

And use grep to remove the first line that says "Database".

Eric-Londons-MacBook-Pro:backup Eric$ mysql --execute="show databases" | awk '{print $1}' | grep -iv ^Database$
customers
db_pics_ericlondon
db_thedrupalblog_d6
drupal
drupal-pics
drupalmusicproject
itunes

And use sed to build the mysqldump command. This one is kinda tricky, sorry. As you can see, I also embedded the date command in there to generate today's date in the format: YYYYMMDD.

Eric-Londons-MacBook-Pro:backup Eric$ mysql --execute="show databases" | awk '{print $1}' | grep -iv ^Database$ | sed 's/\(.*\)/mysqldump \1 > \1.'$(date +"%Y%m%d")'.sql/'
mysqldump customers > customers.20100825.sql
mysqldump db_pics_ericlondon > db_pics_ericlondon.20100825.sql
mysqldump db_thedrupalblog_d6 > db_thedrupalblog_d6.20100825.sql
mysqldump drupal > drupal.20100825.sql
mysqldump drupal-pics > drupal-pics.20100825.sql
mysqldump drupalmusicproject > drupalmusicproject.20100825.sql
mysqldump itunes > itunes.20100825.sql

Lastly, if everything looks good, you can pipe the output back to the command line.

Eric-Londons-MacBook-Pro:backup Eric$ mysql --execute="show databases" | awk '{print $1}' | grep -iv ^Database$ | sed 's/\(.*\)/mysqldump \1 > \1.'$(date +"%Y%m%d")'.sql/' | sh

Eric-Londons-MacBook-Pro:backup Eric$ ls -1
customers.20100825.sql
db_pics_ericlondon.20100825.sql
db_thedrupalblog_d6.20100825.sql
drupal-pics.20100825.sql
drupal.20100825.sql
drupalmusicproject.20100825.sql
itunes.20100825.sql

You could even take this one step further and pipe the output through gzip to compress the dumps :)

Recently, I was working with the file_get_contents() and file_put_contents() functions to read in a file, change its contents, and write it to the file system. Although the is_writable() function returned TRUE, my file_put_contents() command was returning FALSE. This lead me to try a fopen command with the "w" switch (which opens a file for writing and truncates it), and still no luck. Although the file had group permissions to read and write on the file system, the file was owned by a non-apache user. In certain PHP configurations (safe mode, etc) this will not work.

I then created a hook_requirements() function to test for the proper file permissions for my custom module. When I deployed my code to the test server, I got a white screen, which usually signifies a PHP fatal error. Upon review of the apache vhost error log, I saw the function I used "posix_getpwuid" was not defined.

In the end I decided to create a helper function that returns the owner of a file:

<?php
function _MYMODULE_get_file_owner($file) {
   
 
// if posix library is installed:
 
if (function_exists('posix_getpwuid')) {
   
$owner = fileowner($file);
   
$ownerInfo = posix_getpwuid($owner);
    if (
$ownerInfo['name']) return $ownerInfo['name'];
  }

 
// try using ls & awk
 
$command = "ls -l $file | awk '{print $3}'";
 
$result = exec($command);
  if (
$result) return $result;
   
  return
false;

}
?>

NOTE: the "stat" shell function could also be used:

<?php
$command
= "stat -c %U $file";
?>

Syndicate content