Back Up Script for site and database showing error - linux

I am no expert at this, but I wanted to created a script that I could turn into a cron job to run a backup of the websites and database on my home server. I found this script and customized it to my settings:
#!/bin/sh
THESITE="ic.sitescribersdev.com"
THEDB="zadmin_ironcowboy"
THEDBUSER="xxxxxx"
THEDBPW="xxxxxx"
THEDATE=`date +%d%m%y%H%M`
mysqldump -u $THEDBUSER -p${THEDBPW} $THEDB | gzip > /var/zpanel/hostdata/zadmin/public_html/$THESITE/backups/files/dbbackup_${THEDB}_${THEDATE}.bak.gz
tar czf /var/zpanel/hostdata/zadmin/public_html/$THESITE/backups/files/sitebackup_${THESITE}_${THEDA TE}.tar -C / var/zpanel/hostdata/zadmin/public_html/$THESITE
gzip /var/zpanel/hostdata/zadmin/public_html/$THESITE/backups/files/sitebackup_${THESITE}_${THEDA TE}.tar
find /var/zpanel/hostdata/zadmin/public_html/$THESITE/backups/files/site* -mtime +5 -exec rm {} \;
find /var/zpanel/hostdata/zadmin/public_html/$THESITE/backups/files/db* -mtime +5 -exec rm {} \;
I tried to run it in ssh to see if it would output the data but I get this error:
arudd#new-host-3:/var/zpanel/hostdata/zadmin/public_html/ic_sitescribersdev_com/backups$ sudo sh backup.sh
: not found2: backup.sh:
: not found8: backup.sh:
: Directory nonexistent_zadmin_ironcowboyar/zpanel/hostdata/zadmin/public_html/ic.sitescribersdev.com
'#'localhost' (using password: YES) when trying to connectwboy
: not found10: backup.sh:
tar: var/zpanel/hostdata/zadmin/public_html/ic.sitescribersdev.com\r\r: Cannot stat: No such file or directory
tar (child): /var/zpanel/hostdata/zadmin/public_html/ic.sitescribersdev.com\r/backups/files/sitebackup_ic.sitescribersdev.com\r_1107141435\r.tar: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
: No such file or directoryc.sitescribersdev.com.sitescribersdev.com
: not found13: backup.sh:
find: missing argument to `-exec'
find: `/var/zpanel/hostdata/zadmin/public_html/ic.sitescribersdev.com\r/backups/files/db*': No such file or directory
Any help on what I am doing wrong to get this to work would be awesome.
Thanks!

I figured it out. It was throwing out errors because the script was made in windows. All I had to do was install and run dos2unix on the script and then it worked like a charm!

Related

Remove a file has the same name with folder after using tar

I created a script to find files, move them to a folder, compress this folder using tar. Then delete original folder. But after running the script, the folder was removed but a file having same name with the folder was created. I tried to run one by one command, it's OK. There is not this file. I added rm command in the script to remove it but not working.
I don't know why this file was create.
My script is below:
#!/bin/bash
cd /home/tuan/testrm/9/
mkdir compressdir
sudo find . -type f -name "*.txt" -print | xargs -I {} mv {} compressdir > /dev/null 2>&1 &
tar -cvzf compresslog.tar.gz --remove-files compressdir
mv compresslog.tar.gz compresslog-`date +"%d%m%Y"`.tar.gz
rm -rf compressdir
I want to know why this file was create and how to prevent this happen.
You should remove & at the end of sudo find line, and it will works.
Because of The & makes the command run in the background.
Root cause: the sudo find command line and tar -> mv -> rm run at synchronized.
If you had checked the file compresslog.tar.gz which the script generated, you will found that it was null or error, and the compress file contain the same content with someone file which you find.

How to write a script for bash to archive a directory and keep its name?

For my uni I need to write a simple script that makes backups of few directories and move them to another directories. I'm very new to Linux so I'm kinda lost. Before that, they showed how to archive with tar and compress with gzip, so I'm assuming that's how I need to do those backups. It is also beginner level, so the script should be as simple as possible.
Here's my script:
#!/bin/bash
echo
directories="work/ extract/ test/"
for directory in $directories
do
tar --create --gzip --file= ~/backups "$directory".tgz $directory
done
And that's the outcome:
pi#raspberry:~ $ ./my_backup.sh
tar: Removing leading `/' from member names
tar (child): : Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: : Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar: Removing leading `/' from member names
tar (child): : Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: : Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar: Removing leading `/' from member names
tar (child): : Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: : Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
I can only guess it's something to do with the names, because '/' is not allowed in names, I think, but I don't know how to remove it. I asked this question on my uni's forum, but nobody answered.
Thanks a lot for your help.
#!/bin/bash
set -e
directories=(work extract test)
for directory in ${directories[#]}; do
tar -czf "${HOME}/backups/${directory}.tgz" "${directory}"
done
I think you could just put the directories into a list do iterate through them via a for loop.
So instead of
directories="work/ extract/ test/"
write
directories=("work" "extract" "test")
if you use the script in the same directory as the directories you need to backup are.
I wasnt able to test it yet, but I think it should work this way.

This tar command works on command line but fails in a script

This tar command works on command line but fails in a script.
CODE:
find ~/ -name "sql*.db" | tar -czv -f backup.tar.gz -T -
OUTPUT FROM COMMAND LINE:
find ~/ -name "sql*.db" | tar -czv -f backup.tar.gz -T -
tar: Removing leading `/' from member names
/home/myuser/folder1/sql1.db
/home/myuser/folder2/sql2.db
/home/myuser/folder3/sql3.db
/home/myuser/folder4/sql4.db
/home/myuser/folder5/sql5.db
ERROR FROM SCRIPT:
[ps623237]$ sh backup.sh
tar: -\r: Cannot stat: No such file or directory
tar: Error is not recoverable: exiting now
Problem was the Windows (CRLF) instead of the Linux (LF). I used notepad++
Edit/EOL Conversion/UNIX
to fix it. Here is the refined code.
#!/bin/sh
DOW=$(date +"%A");
find /home/user1 -name sql*.db | tar -czv -f /home/user1/backup/$DOW.DB.tar.gz -T -

How do I backup a directory using bash script

I am having difficulty trying to backup my 'my_work' directory into my destination 'Backup' directory. I tried running the script, but it does not seem to work. This is the script that I have written :
#!/bin/bash
SRCDIR="/home/student/Documents/my_work/"
DESTDIR="/home/student/Backups/"
FILENAME=backup1-$(date +%-Y%-m%-d)-$(date +%-T).tgz
tar --create --gzip --file=$DESTDIR $FILENAME $SRCDIR
This is the output I received :
tar: backup1-201576-10\:24\:17.tgz: Cannot stat: No such file or directory
tar: Removing leading '/' from member names
tar (child): /home/student/Backups/: Cannot open: Is a directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
I can't seem to find a solution to this, please help
Just make a subtle change in your script:
Change
tar --create --gzip --file=$DESTDIR $FILENAME $SRCDIR
to
tar --create --gzip --file=$DESTDIR$FILENAME $SRCDIR
Notice there is no space between $DESTDIR and $FILENAME. To suppress tar: Removing leading '/' from member names, you may use the -P flag cautiously.
Also, it'd be nice to replace colons in the filename with underscores or dashes. Colon is a reserved character that is also used in PATH and may cause confusion.

Gunzip alternative to tar command

A straight-forward question:
Could you please guys give me an alternative to this tar command, using gunzip ?
tar -C /var/cpanel/easy/apache/custom_opt_mods -xzf custom_opt_mod-mod_geoip.tar.gz
I want this because when I am trying to run that command I get with this:
gzip: stdin: not in gzip format tar: Child returned status 1 tar:
Error is not recoverable: exiting now
Thanks

Resources