backup only bash files

backup only files less than 1mb that starting with #!/bin/bash

find . -type f -size -1024k -exec egrep -m1 -l '^??/bin/bash' {} + | xargs tar -cjvf backup-$(date +%Y-%m-%d).tar.bz2
Advertisements

find files between dates

find files between two specified dates

find / -type f -newermt 2017-07-01 ! -newermt 2017-08-30 -ls

find files created between begin of current month and the current day

find / -type f -newermt $( date +%Y-%m-01 ) ! -newermt $( date +%Y-%m-%d ) -ls

find .php files modified on the last 30 days

find / -type f -regex .*php$ -mtime -30 -exec ls -lt {} +

find files between 1 month ago and 3 days ago that ends with numbers in name

find / -type f -regex .*[0-9]$ -newermt $( date -d '-1 month' +%Y-%m-%d ) ! -newermt $( date -d '-3 day' +%Y-%m-%d ) -exec ls -la {} +

compress files created between two specified dates

find / -type f -newermt $( date -d '-1 month' +%Y-%m-%d ) ! -newermt $( date -d '-3 day' +%Y-%m-%d ) -exec gzip -f {} +

Script para Exportar usuarios e permissoes MySQL

<?php
// Exporta usuarios e permissoes MySQL

$host = 'localhost';
$user = 'root';
$pass = 'toor';

header('Content-type: text/plain; Charset=UTF-8');
$export = array();
$link = new mysqli($host, $user, $pass, 'mysql', 3306);

if ($link->connect_error) {
  printf('Connect failed (%s): %s', $link->connect_errno, $link->connect_error);
  die();
}

$link->query('SET NAMES \'utf8\'');
$result = $link->query('SELECT `User`, `Host`, `Password` FROM `user`');

if ($result) {
  
  while ($row = $result->fetch_row()) {
    $user = $row[0];
    $host = $row[1];
    $pass = $row[2];

    $export[] = 'CREATE USER `'. $user .'`@`'. $host .'` IDENTIFIED BY \''. $pass .'\'';

    // Fetch any permissions found in database
    $result2 = $link->query('SHOW GRANTS FOR `'. $user .'`@`'. $host .'`');

    if ($result2) {
      while ($row2 = $result2->fetch_row()) {
        $export[] = $row2[0];
      } // while ($row2...
    } // if ($result2)...
  } // while ($row...
}

$link->close();
echo implode(";\n", $export);
?>

Script Importação MySQL

#!/bin/bash
# Script importacao MySQL

MYSQL_USER='root'
MYSQL_PASSWD='MysqlPasswordHere'
DIR_LOCAL_BACKUP_SQL=/tmp/backup_mysql

echo -e "\n[ $( date '+%d/%m/%Y %R' ) ] INICIO EXECUCAO SCRIPT DE IMPORTACAO " | tee -a /var/log/$( basename $0 ).log

for i in $( ls $DIR_LOCAL_BACKUP_SQL/*.sql ); do

# Ex de nome de arquivo: intranet-2017-03-15-diario.sql
  DB=$( basename $( echo $i | awk -F- '{print $1}') )

  if [ $DB != 'mysql' ] && [ $DB != 'information_schema' ] && [ $DB != 'performance_schema' ]; then

    echo "inicio importacao $DB [ $( date '+%d/%m/%Y %R' ) ]" | tee -a /var/log/$( basename $0 ).log
    mysql -u $MYSQL_USER -p$MYSQL_PASSWD -e "DROP DATABASE $DB"
    mysql -u $MYSQL_USER -p$MYSQL_PASSWD < $i
    echo "fim importacao $DB [ $( date '+%d/%m/%Y %H:%M' ) ]" | tee -a /var/log/$( basename $0 ).log
  fi

done

echo "[ $( date '+%d/%m/%Y %R' ) ] FIM EXECUCAO SCRIPT DE IMPORTACAO " | tee -a /var/log/$( basename $0 ).log

Script Backup MySQL

#!/bin/bash
# Script de Backup MySQL

DIR_BACKUP='/backup_sql'
MYSQL_USER='root'
MYSQL_PASSWD='toor'

DATABASES=$( mysql -u $MYSQL_USER -p$MYSQL_PASSWD -e 'SHOW DATABASES' | tr -d '' | grep -v Database )

DATA=$( date +%F )

for i in ${DATABASES[*]}; do
  if [ $( date +%d ) == 01 ]; then
    echo "[ $DATA ] Backup Mensal e Diario - Database: ( $i )"
    mysqldump -u $MYSQL_USER -p$MYSQL_PASSWD --databases $i --single-transaction > $DIR_BACKUP'/mensal/'$i-$DATA'-mensal.sql'
    cp $DIR_BACKUP'/mensal/'$i-$DATA'-mensal.sql' '/backup_sql/diario/'$i-$DATA'-diario.sql'
  elif [ $( date +%u ) == 7 ]; then
    echo "[ $DATA ] Backup Semanal e Diario - Database: ( $i )"
    mysqldump -u $MYSQL_USER -p$MYSQL_PASSWD --databases $i --single-transaction > $DIR_BACKUP'/semanal/'$i-$DATA'-semanal.sql'
    cp $DIR_BACKUP'/semanal/'$i-$DATA'-semanal.sql' '/backup_sql/diario/'$i-$DATA'-diario.sql'
  else
    echo "[ $DATA ] Backup Diario - Database: ( $i )"
    mysqldump -u $MYSQL_USER -p$MYSQL_PASSWD --databases $i --single-transaction > $DIR_BACKUP'/diario/'$i-$DATA'-diario.sql'
  fi
done

# Rotacao dos arquivos de backup
find $DIR_BACKUP/diario/* -mtime +6 -delete
find $DIR_BACKUP/semanal/* -mtime +28 -delete
find $DIR_BACKUP/mensal/* -mtime +31 -delete