• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

bash

Backup Rotation

June 12, 2015

After another downtime with my service provider, I wasn’t really happy with my backup strategy. Although, I have a backup plan in place, it’s not to the level of detail that I wanted. Back to the drawing board I went. I realized that what I really needed, was to back up my databases and keep copies of the last few days. Prior to that I was just relying on full file system backup and restore. Thankfully, backing up MySQL databases is not that difficult. The following pieces of code is the backup script I have put together to backup each MySQL database, as well rotate the backups and keep 5 days of copies of each database.

#! /bin/bash
 
# where sql files are stored
cd /home/user/mysql
 
# list all databases and assign to variable
# exclude 3 MySQL default databases (information_schema,performance_schema,mysql)
DATABASES=`mysql -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)"`
 
# loop through all databases
# remove db that's 5 days old
# rotate dbs. rename 1 day older
# perform backup today
for db in $DATABASES; do
  rm -rf $db-4.sql
  mv $db-3.sql $db-4.sql
  mv $db-2.sql $db-3.sql
  mv $db-1.sql $db-2.sql
  mv $db-0.sql $db-1.sql
  mysqldump $db > "$db-0.sql"
done

#! /bin/bash # where sql files are stored cd /home/user/mysql # list all databases and assign to variable # exclude 3 MySQL default databases (information_schema,performance_schema,mysql) DATABASES=`mysql -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)"` # loop through all databases # remove db that's 5 days old # rotate dbs. rename 1 day older # perform backup today for db in $DATABASES; do rm -rf $db-4.sql mv $db-3.sql $db-4.sql mv $db-2.sql $db-3.sql mv $db-1.sql $db-2.sql mv $db-0.sql $db-1.sql mysqldump $db > "$db-0.sql" done

The for loop will execute everything inside it. The backup rotation gives me 5 days of backups of each database. You can schedule the script to run daily with cron. Just a side note. You need to create .my.cnf file in your home directory with your database credentials, so you don’t have to enter your username and password when running it from cron. Also script needs to be executable. A simple chmod +x will do the trick.

Filed Under: Linux, WP Tagged With: backup, bash, mysql, rotation

WordPress Bash Script

March 10, 2014

You’ve heard of the famous 5-minute WordPress installation. You might be able to shave off some considerable time if you try installing WordPress from the command line using a Bash script. So, here’s my stab at how to install WordPress from the Linux shell. To be able to do this, you need access to server’s command line or shell as it’s commonly known in the Linux world. Some hosting companies allows you to have access to shell, some don’t. So, your mileage may vary. In this exercise, you need access to it, if you want to perform these functions.

#!/bin/bash
sudo wget http://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo mv wordpress/* ./
rm -rf wordpress
rm -f latest.tar.gz
echo "done!"

#!/bin/bash sudo wget http://wordpress.org/latest.tar.gz sudo tar -xzvf latest.tar.gz sudo mv wordpress/* ./ rm -rf wordpress rm -f latest.tar.gz echo "done!"

The sequence

The first line tells the system it’s a Bash script.
The second line downloads the latest WordPress.
The third line untags or unzips the compressed file.
The fourth line moves wordpress to the current directory.
The fifth line deletes the wordpress folder.
The sixth removes the compressed file.
The seventh, you echo “done” to the shell.

The fourth line needs a little bit of explaining. By default, when you uncompress the WordPress zipped file, it dumps all the files in the “wordpress” directory. It might be your intention to create a sub-folder called “wordpress”, but if that is not what you want to do, then you will need to move all the contents out of the ‘wordpress’ folder and place them in the current directory. That’s what I’m doing here. I’m just moving the WordPress files up one level. After the moving the files, I just delete the now empty ‘wordpress’ folder.

The echo ‘done’ to the screen is just to tell the user that the installation has completed. All you need to do from this point on is to go to your browser and complete the now famous 5-minute installation. Except that you’ll do it at least half the time. Maybe a quarter off the time.

Filed Under: Linux, WP Tagged With: bash

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 4
  • Go to page 5
  • Go to page 6
  • Home
  • About
  • Archives

Copyright © 2023