How do I backup a directory using bash script - linux

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.

Related

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.

tar: Removing leading `/' from member names "it is not duplicate"

#!/bin/bash
source="/home/user/work/tar/deneme"
source2="/home/user/work/tar/deneme1"
for i in {1..5}
do
tar -czvf $source2/$i/$i.tar.gz $source/$i/
done
I get this error message.
tar: Removing leading/' from member names`
this is my script and error. there are a lot of questions here but my problem doesn't solve. I run script than script create .tar.gz file. But if I unzip with tar -xzvf 1.tar.gzthis command, my file created in full path like home/user/work/tar/deneme/1/1-1.txt.
Do you have any idea?
I try some of ways.
For examle
Find /SED to convert absolute path to relative path within a single line tar statement for crontab
https://unix.stackexchange.com/questions/59243/tar-removing-leading-from-member-names/59244
This is because GNU tar remove leading / (by default). To avoid it you can rewrite your script on this way:
#!/bin/bash
cd /home/user/work/tar
source="deneme"
source2="deneme1"
for i in {1..5}
do
mkdir -p ${source2}/${i}
tar -czvf ${source2}/${i}/${i}.tar.gz ${source}/${i}/
done
Thank you for all your comments and answer.
I find the solution. I change some of codes. which is inside for loop
mkdir $source2/$i
cd $source/
tar -czvf $source2/$i/$i.tar.gz $i/*

Linux tar with pattern match and removing leading paths

I'm trying to archive all the .log files located in the /var/log directory and on creation remove all leading paths on files.
I have found I can archive all the .log files easily with:
tar -cvf ~/backup.tar /var/log/*.log
unfortunately, after searching online the way to remove leading paths is to use -C to change directory for the command only now it doesn't recognize the *.log and thinks * is literal.
using:
tar -cvf ~/backup.tar -C /var/log *.log
I get an error saying cannot find file *.log.
I imagine my syntax must be off and I've tried some changes to the syntax with no avail.
Using find to pass files to tar:
find /var/log -name *.log -printf '%P\n' |\
tar -C /var/log -czf backup.tar.gz -T -
Find will look for *.log files and printf format the output to show just filenames.
tar's '-T -' tells to read filenames from stdin

Staying in another folder, how can i tar specific files from another directory?

Thanks for your support,
I have the following folder structure on my linux laptop
/home
/A
/B
In folder "B", I have files of type *.csv, *.dat.
Now from folder A, How can I create a tar file containing files *.csv in folder B. I am running the command in folder A
Here is the command, I have tried but its not working,
In /home/A folder, I am running the following command
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 *.csv
and also tried with this,
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 --wildcards *.csv
For both of the commands, I get the following error,
tar: *.csv: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
In the tar file, I dont want to include the whole folder structure and this is the reason, I am using option -C (capital)
Moreover, the following command works but it tars all *.csv and *.dat files.
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 .
You can edit the names in the tar command to remove the path. (Assuming that you have GNU tar.)
tar -cf /home/A/Sample1.tar --transform 's,.*/\([^/]*\),\1,' /home/B/ZSBSDP4/*.csv
Note that if you specify more source directories on the command, you could accidentally put more than one file with the same name in the tar file. Then when unpacking, the last one will overwrite those with the same name that precede it.
You can use the --exclude=PATTERN option:
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 . --exclude=*.dat
Other "local file selection" options listed in the man page: http://linux.die.net/man/1/tar

Back Up Script for site and database showing error

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!

Resources