Jump to content

Need Help Fixing Code In Shell Script For Backup


Eggmeng

Recommended Posts

Hi,

I have cron jobs set up on my server to run two shell scripts for backing up a wordpress site. The script that backs up the files is working but the one that is supposed to back up the db is not.

I will pay someone who know shell scripting language, for the time it takes to look at the code and fix it.

Please send me a pm if interested and available and if you have a bonafide company.

Thanks,

Link to comment
Share on other sites

You mean a plug in? Yes, I'm using one now that was free, and looking at another one with better features that I will have to pay for. In the meantime I'd like to get both cron jobs working. I feel sure the code fix would take 5 minutes for someone who knows what they are doing.

Plugins are great but they constantly have to be updated and sometimes they break.

Link to comment
Share on other sites

I do scripting (but not coding).

Do you get any error messages?

Would you be able to post the script here so we'll be able to take a peak?

Check: Crontab, which user is running the script (and that it has privileges to access the database), script execute privileges / ownership. After this you can try to rung the script on command line with the correct user.. this might bring out some error messages.

--

So first check that the crontab entry is in order

crontab -l -u username_which_is_runnign_the_script #This is probably the database user

Then check that the script permissions are ok.

ls -l /path/to/the/script.sh

It's possible that the script simply lacks execute privileges. You can add some

chmod 755 /path/to/the/script.sh

And then try to run in manually

/path/to/the/script.sh

Link to comment
Share on other sites

Okay, here is the file back up script, which works fine, where public html is the root directory .

#!/bin/sh
backup_files="/home/eggmeng/public_html"
dest="/home/eggmeng/public_html/shell-backups"
day=$(date +%A)
host=$(hostname -s)
archive_file="$host-$day.tgz"
tar czf $dest/$archive_file $backup_files
And here is the db backup script which doesn't work.
#!/bin/bash
PATH=/usr/sbin:/sbin:/bin:/usr/bin
user="[db_name_goes_here]"
pass="[db-password_goes_here]"
host="localhost"
sub="$(date +"%Y-%m-%d")"
dest="/home/eggmeng/public_html/shell-backups"
mdb="$dest/db/$sub"
if [ ! -d $mdb ]
then
mkdir -p $mdb >/dev/null 2>&1 && echo "Directory $mdb created." || echo "Error: Failed to create $mdb directory."
else
echo "Error: $mdb directory exits!"
fi
now="$(date +"%Y-%m-%d_%H-%M-%S")"
file=""
dbs="$(mysql -u $user -h $host -p$pass -Bse 'show databases')"
for db in $dbs
do
file="$mdb/$db.$now.sql.gz"
mysqldump -u $user -h $host -p$pass --complete-insert $db | gzip -9 > $file
echo "Backup $file.....DONE"
done
find $dest/db/ -maxdepth 1 -type d -mtime +6 -exec echo "Removing Directory => {}" \; -exec rm -rf "{}" \;
Link to comment
Share on other sites

For the first, you need to replace the ">" with ">" and "&" with "&" on your script. The second last line does not matter, just an beauty thing, but these lines will make the script fail:

mkdir -p $mdb >/dev/null 2>&1 && echo "Directory $mdb created." || echo "Error: Failed to create $mdb directory."

--> mkdir -p $mdb > /dev/null 2>&1 && echo "Directory $mdb created." || echo "Error: Failed to create $mdb directory."

mysqldump -u $user -h $host -p$pass --complete-insert $db | gzip -9 > $file

--> mysqldump -u $user -h $host -p$pass --complete-insert $db | gzip -9 > $file

After modifications try to run it manually and report the possible error messages.
Link to comment
Share on other sites

Thanks ENC. I made the mods, replaced the script and I'm getting this email notification and the same result as before, wherein a folder named with today's date is created inside another folder named DB. But the folder is empty.

Looks like the script is being denied access to the db, doesn't it?

+ PATH=/usr/sbin:/sbin:/bin:/usr/bin

+ user='[actual_db_name]'

+ pass='[actual_db_password]'

+ host=localhost

++ date +%Y-%m-%d

+ sub=2013-03-16

+ dest=/home/eggmeng/public_html/shell-backups


+

mdb=/home/eggmeng/public_html/shell-backups/db/2013-03-16


+ '[' '!' -d

/home/eggmeng/public_html/shell-backups/db/2013-03-16 ']'


+ echo 'Error: /home/eggmeng/public_html/shell-backups/db/2013-03-16

directory exits!'


Error:

/home/eggmeng/public_html/shell-backups/db/2013-03-16 directory exits!


++ date +%Y-%m-%d_%H-%M-%S


+ now=2013-03-16_05-20-01


+ file=


++ mysql -u 1 -h localhost '-p[actual_db_password]' -Bse

'show databases'


ERROR 1045 (28000): Access denied for user

'1'@'localhost' (using password: YES)

+ dbs=


+ find /home/eggmeng/public_html/shell-backups/db/

-maxdepth 1 -type d -mtime +6 -exec echo 'Removing Directory => {}' ';'
-exec rm -rf '{}' ';'

Link to comment
Share on other sites

That looks like it. Your username is '1' ? Could you double check that one as well as the password.

Then try on the command line if you can connect to the database

mysql -u user_name_here -pthe_password_for_the_username

Link to comment
Share on other sites

Great to hear it works!

Btw. Last night I drunk for the health. Today I'm feeling that the effects were only temporary. Memory corrupted, system not in good health, but the core was not dumped! :)

Link to comment
Share on other sites

  • 1 month later...

Oilinki,

Your answers for Eggmeng a few months ago were so helpful for me, too! Thank you!

I am still getting a couple errors... would you be willing to take a look and see if you find any problems?

THANK YOU!!!!!

File backup ERROR:

tar: AM.tgz: Cannot stat: No such file or directory

tar: Removing leading `/' from member names

tar: /home/content/m/i/l/milwaukiepre/html/cron_backups/p3nlh229-Tue-05-14-13-12\:05\:02: file changed as we read it

File backup SCRIPT:

#!/bin/sh

backup_files="/home/content/m/i/l/milwaukiepre/html/"
dest="/home/content/m/i/l/milwaukiepre/html/cron_backups/"
day=$(date +%a-%m-%d-%y-%r)
host=$(hostname -s)
archive_file="$host-$day.tgz"
tar czf $dest/$archive_file $backup_files

Database backup ERROR: (not really an error, but I'd rather not get an email every day)

Directory /home/content/m/i/l/milwaukiepre/html/cron_backups//db/2013-05-14 created.

Database backup SCRIPT:

#!/bin/bash
PATH=/usr/sbin:/sbin:/bin:/usr/bin
user="milwaukiepre"
pass="Fun2learn"
host="milwaukiepre.db.6018926.hostedresource.com"
sub="$(date +"%Y-%m-%d")"
dest="$HOME/html/cron_backups/"
mdb="$dest/db/$sub"
if [ ! -d $mdb ]
then
mkdir -p $mdb >/dev/null 2>&1 && echo "Directory $mdb created." || echo "Error: Failed to create $mdb directory."
else
echo "Error: $mdb directory exits!"
fi
now="$(date +"%Y-%m-%d_%H-%M-%S")"
file=""
dbs="$(mysql -u $user -h $host -p$pass -Bse 'show databases')"
for db in $dbs
do
file="$mdb/$db.$now.sql.gz"
mysqldump -u $user -h $host -p$pass --complete-insert $db | gzip -9 > $file
echo "Backup $file.....DONE"
done
find $dest/db/ -maxdepth 1 -type d -mtime +6 -exec echo "Removing Directory => {}" \; -exec rm -rf "{}" \;

Ah. Remove the "[" "]" marks from both this one as well as from the password

--> user="eggmeng_alphanumericstring1"

Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.


  • Topics

  • Latest posts...

    1. 6

      Can I send the 400,000 baht to my wife's account?

    2. 84

      Why experts are blasting Trump for 'already breaking the law'

    3. 6

      Chinese loan shark gang member arrested at Bangkok airport

    4. 0

      Bangkok Uncovered: Essential Tips to Avoid Scams in the Land of Smiles

    5. 104

      Pete Hegseth-Sec Def Nominee

    6. 81

      Misogynistic Attacks Against Women Surge on Social Media Following Trump’s Election Win

    7. 104

      Pete Hegseth-Sec Def Nominee

    8. 115

      The major backers of "populist" trump

  • Popular in The Pub


×
×
  • Create New...