Useful programming scripts

⚡️ Introduction

Scripts

What Scripts?!

I am a developer and i have not so much time to waste in regularly operation like backups or updates things.

So i wrote some scripts for me and you, my dear reader. These scripts are intended to be used in a unix system.

Maybe you can notice that something could be improved, i know too, so you can copy it, paste in your editor and improve without problem ;)

📜 The List

Git

Components installed

  • Git

What to do before

Execute command git config --global credential.helper store in your git directory to save your git credentials.

Script

#!/bin/bash

if [ -z "$1" ]; then
        echo "Syntax: git.sh 'commit message'"
        exit 1
fi
cd yourgitdir/
echo "Starting with commit message: $1"
git add . && git commit -m "$1" && git push
echo "Done"

Explanation

This script can make a fast commit and push for a specific git project.

I noticed that i optimized git wasted time by using it, you can also use without saving git credentials.

Backup

Components installed

  • AWS Cli
  • rsync

What to do before

Execute command aws configure to set your aws s3 keys.

Create a file my.cnf with this content:

[client]
password="yourmysqlpassword"

Create a folder backup.

Script

#!/bin/bash
if [ -d "./tmp-backup" ]; then
        rm -r ./tmp-backup
fi
mkdir tmp-backup
mkdir tmp-backup/db
mkdir tmp-backup/files
mkdir tmp-backup/data
mkdir tmp-backup/www
echo "Backup database..."
mysqldump --defaults-file=my.cnf -u root DBNAME > ./tmp-backup/db/DBNAME.sql

echo "Backup structure..."
mysqldump --defaults-file=my.cnf -d -u root DBNAME --skip-dump-date > DBNAME.sql
sed -i -E 's/ AUTO_INCREMENT=[0-9]+/ AUTO_INCREMENT=0/g' DBNAME.sql

echo "Backup directories..."
cp *.sh ./tmp-backup/files/
cp my.cnf ./tmp-backup/files/
cp -r ./someFiles ./tmp-backup/someFiles/

echo "Backup www..."
rsync -a --no-links /var/www/html/ tmp-backup/www/
TIME=$(date +"%Y-%m-%d_%H:%M:%S")
FILENAME=backup_$TIME.tar.gz

echo "Compress..."
mv tmp-backup backup_files
export GZIP=-9
tar -cpzf ./backup/$FILENAME ./backup_files
rm -r backup_files

echo "AWS sync..."
/usr/local/bin/aws s3 sync --delete backup s3://yours3bucket/
echo "Done"

Explanation

This script create complete backups of your project with a database, www directory, files directory, etc. then upload it to an s3 bucket.

If you want to keep clean the backup directory, you can use this cron line to delete file older than 5 days:

0 0 * * * find /yourfolder/backup -mtime +5 -type f -delete

Logs aliases

Components installed

  • nano

Commands

Edit your user bashrc: nano nano ~/.bashrc

Add one line:

alias yourlogcommand='tail -f /var/log/yourlogfile.log'

Logout and login to apply.

Explanation

This commands help you to open logs very fast, is very comfortable if you concatenate with other commands, for example:

script.sh reboot && logcommand

So you reboot your script then open logs immediately.

Homebrew upgrade (Mac)

Components installed

  • homebrew

Script

brew update
brew upgrade
brew cleanup
echo "Done"

Explanation

This script allow homebrew packages to upgrade, then delete old logs and updates. Very fast and useful doing all things one after another.

That’s the end (for now)

When some of my scripts will be available to be posted here, i will update this article.

Until then, hope you liked this list, enjoy!


Thanks for reading! If you see something not so much clear, bad formatted or broken links, contact me and i will fix asap!

comments powered by Disqus