Saturday, July 25, 2015

MySQL backup database to a compressed GZIP and with date and time in file name

How to backup a MySQL database to a compressed zip file with current date and time

mysqldump -u {username} -p{password} {database} | gzip -9 - > /{target_directory}/{sub_dir}/{dbname}-`date '+\%Y_\%m_\%d-\%H_\%M'`.sql.gz



To Restore (decompress zip and restore to MySQL)

gunzip < some-backup-file.gz | mysql -u {username} -p{password}


How to synchronize files to cloud storage like google drive using rclone

Use rclone to copy local files or directory to google drive, google cloud storage, rackspace etc...


Main website:
http://rclone.org

How to Install and Use RClone with Google Drive

1. Download 'binary' to /usr/local/src
cd /usr/local/src
wget http://downloads.rclone.org/rclone-v1.17-linux-amd64.zip

2. Decompress zip
unzip rclone-v1.17-linux-amd64.zip

3. Copy rclone binary to /usr/local/bin (so that it can be executed from anywhere)
cd rclone-v1.17-linux-amd64
cp rclone /usr/local/bin/

4. Add configuration 'example-config'
rclone config

answer questions here ... this will setup a connection name and you will be required to authorize (get token) from google.

For google drive, when asked which type of storage, answer #6 'drive'.

5. Test it like this:

rclone --bwlimit=500k --log-file="/log/rclone/{{{backup_log_file.log}}}" sync {{{source_directory_to_sync}}} {{{rclone_connection_name}}}:{{{destination_directory_in_google_drive}}}

destination_directory_in_google_drive (without / prefix)
when asked about using your own certificate / key (answer just use rclone's)

for example:

(I am copying /databak/last_30_day directory from my server to google_drive_databak connection)
rclone --bwlimit=500k --log-file="/log/rclone/filename.log" sync /databak/last_30_day google_drive_databak:last_30_day

(I am copying /some_file.zip from my server to google_drive_databak connection's export directory)
rclone --bwlimit=500k --log-file="/log/rclone/filename.log" sync /some_file.zip google_drive_databak:export

Sunday, June 28, 2015

Find files on certain directory for excluding files certain permission

Why is this useful? When your directory contains thousands of files.
Yes, CHMOD is slow and surprisingly very I/O taxing.
So being able to use 'find' command first to only CHMOD certain files
that needs to be CHMODED is useful.


find /your_directory ! -perm 0777

-OR-

find /your_directory \! \( -perm 0777 -o -perm 0775 \)

Check if file or directory exists using bash or shell script sh

if [ -f file_path_name.ext ]
then
echo "file exists"
else
echo "file does not exists"
fi


if [ -d directory_path ]
then
echo "dir exists"
else
echo "dir does not exists"
fi

Send HUP hangup hang-up signal to linux process to re-read configuration file

kill -HUP $( cat /var/run/nginx.pid )

Find inodes where it is being used inside directories

This command will show inodes being used in directories recursively:

find {{{starting directory}}} -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n

Friday, February 27, 2015