Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:51:44+00:00 2026-06-08T16:51:44+00:00

I’m trying to learn some scripting however I can’t find solution for one functionality.

  • 0

I’m trying to learn some scripting however I can’t find solution for one functionality.

Basically I would like to ask to evaluate my script as it’s probably possible to reduce the complexity and number of lines.

The purpose of this script is to download random, encrypted MySQL backups from Amazon S3, restore the dump and run some random MySQL queries.

I’m not sure how to email the output from printf statements – one is for headers and second one for actual data. I’ve tried to format the output so it looks like below but I had to exclude the headers from the loop:

Database:   Table:          Entries:                      

database1   random_table        0                             
database2   random_table        0                             
database3   random_table        0
database4   random_table        0

I would like to include this output in the email and also change the email subject based on the success/failure of the script.

I probably use to much if loops and MySQL queries are probably to complicated.

Script:

#!/usr/bin/env bash

# DB Details:
db_user="user"
db_pass="password"
db_host="localhost"

# Date
date_stamp=$(date +%d%m%Y)

# Initial Setup
data_dir="/tmp/backup"

# Checks
if [ ! -e /usr/bin/s3cmd ]; then
    echo "Required package (http://s3tools.org/s3cmd)"
    exit 2
fi
if [ -e /usr/bin/gpg ]; then

    gpg_key=$(gpg -K | tr -d "{<,>}" | awk '/an@example.com/ { print $4 }')

    if [ "$gpg_key" != "an@example.com" ]; then
        echo "No GPG key"
        exit 2
    fi

else
    echo "No GPG package"
    exit 2
fi

if [ -d $data_dir ]; then
    rm -rf $data_dir/* && chmod 700 $data_dir
else
    mkdir $data_dir && chmod 700 $data_dir
fi

# S3 buckets
bucket_1=s3://test/

# Download backup

for backup in $(s3cmd ls s3://test/ | awk '{ print $2 }')
do
    latest=$(s3cmd ls $backup | awk '{ print $2 }'  | sed -n '$p')
    random=$(s3cmd ls $latest | shuf | awk '{ print $4 }' | sed -n '1p')
    s3cmd get $random $data_dir >/dev/null 2>&1
done

# Decrypting Files
for file in $(ls -A $data_dir)
do
    filename=$(echo $file | sed 's/\.e//')
    gpg --out $data_dir/$filename --decrypt $data_dir/$file >/dev/null 2>&1 && rm -f $data_dir/$file
    if [ $? -eq 0 ]; then

        # Decompressing Files
        bzip2 -d $data_dir/$filename
        if [ $? -ne 0 ]; then
            echo "Decompression Failed!"
        fi
    else
        echo "Decryption Failed!"
        exit 2
    fi
done

# MySQL Restore

printf "%-40s%-30s%-30s\n\n" Database: Table: Entries:

for dump in $(ls -A $data_dir)
do
    mysql -h $db_host -u $db_user -p$db_pass < $data_dir/$dump
    if [ $? -eq 0 ]; then

        # Random DBs query
        db=$(echo $dump | sed 's/\.sql//')
        random_table=$(mysql -h $db_host -u $db_user -p$db_pass $db -e "SHOW TABLES" | grep -v 'Tables' | shuf | sed -n '1p')
        db_entries=$(mysql -h $db_host -u $db_user -p$db_pass $db -e "SELECT * FROM $random_table" | grep -v 'id' | wc -l)

        printf "%-40s%-30s%-30s\n" $db $random_table $db_entries

        mysql -h $db_host -u $db_user -p$db_pass -e "DROP DATABASE $db"     
    else
        echo "The system was unable to restore backups!"
        rm -rf $data_dir
        exit 2
    fi
done

#Remove backups
rm -rf $data_dir
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:51:47+00:00Added an answer on June 8, 2026 at 4:51 pm

    move out of the loop :

    random_tables=$(mysql -h $db_host -u $db_user -p$db_pass $db -e "SHOW TABLES" | grep -v 'Tables')
    table_nb=$(wc -l <<<"$random_tables")
    

    and in the loop

    random_table=$(sed -n $((RANDOM%table_nb+1))p <<<"$random_tables")
    

    A remark $? is the status of latest command executed so after && rm it will not be the status of decrypt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.