I'm trying to create a backup script in bash, to tar the contents of a folder and move the resulting file somewhere, but I don't really know how to do it.
#!/bin/bash
name="$date +"%y-%m-%d""
tar -zcvf $name code
But the result is that the file is just named +%y-%m-%d. How can I change the script to name the file by the date as intended?
Intended output: 2013-08-29.tar
Like this:
name=$(date '+%Y-%m-%d')
tar -zcvf "$name.tar.gz" code
or even in one line:
tar -zcvf "$(date '+%Y-%m-%d').tar.gz" code
Drop -z flag if you want .tar instead of .tar.gz.
Use %y instead of %Y if you want just 2 digits of a year (17 instead of 2017).
$() is used for command substitution.
tar -zcvf "$(date '+%F').tar.gz" code-path
will produce full date; same as %Y-%m-%d. (see man date follow by /) to search and %F.
For example, it will generate:
2015-07-21.tar.gz
If code-path is a directory it will compress and archive all the files in it.
On BSD System you may need to use it like this:
/usr/bin/tar -czvf /home/user/backup-`(date +%y-%m-%d)`.tar.gz /some/file.txt
[root#node2rhel75:~]# tar -jcf /var/log/lvm_etc_backup-(date +%F_%H-%M_%p).tar.bz2 /etc/lvm/; ls -lh /var/log/
tar: Removing leading `/' from member names
-rw-r--r-- 1 root root 46 Jul 13 02:57 lvm_etc_backup-2021-07-13_02-57_AM.tar.bz2
[root#node2rhel75:~]#
Related
I have created some .tar folders that for the most part contain some text files. Is is possible to edit programmatically a text file from the command line (bash and related tools) without fully un-packing the tar?
Context: these .tars were created by a script and I realized I made a mistake. I'm looking for the most efficient and simple solution to edit a part of a single line of a text file.
You could use tar --delete and tar --update to replace a file inside a tar
i think that you won't be able do it with out descompressing. You could to do a loop for descompress only your text file, edit with sed and compress again. In other case, i think that it is impossible...
maybe something like this
xzcat blah | sed /../../ | xz blah
within the loop that runs through all the files
Thanx for the inspiration Grammy, here is a minimal example in case someone comes looking for the same.
Create some data in a folder:
cd /tmp;
mkdir dir1;
echo "foo" >> ./dir1/test_txt;
echo "bar" >> ./dir1/test_txt;
Pack in a tar:
tar -cf test_tar.tar dir1;
Delete the directory that we are going to decompress and edit.
rm -rf dir1;
And unpack only the relevant file from the tar folder and edit:
WARNING:
Tar updates only if the timestamp changed since the last edit!
This is why there is a sleep command here (to change the timestamp.).
sleep 1
tar -xf test_tar.tar dir1/test_txt
sed -i 's/foo/baar/' dir1/test_txt ;
Update the relevant file.
tar -uf test_tar.tar dir1/test_txt
Check if this worked:
tar -xf test_tar.tar dir1
cat ./dir1/test_txt
[...]$
baar
bar
# Current date
now=`date +%Y-%m-%d:%H:%M`
# Compress folder
tar czf "$now.tar.gz" dump/
does not work. No tar is created. But
tar czf someName.tar.gz dump/
works fine. Can someone point out the problem? It seems that
tar czf "$now.tar.gz" dump/
is not accepted as filename. Any ideas?
Found the problem:
If the archive file name includes a colon (‘:’), then it is assumed to
be a file on another machine[...]
which lays here
now=`date +%Y-%m-%d:%H:%M`
I replaced the : with - and it works fine. More info here: http://www.gnu.org/software/tar/manual/html_section/tar_46.html#file
I develop following script to gzip yesterday files in a directory, any improvements suggestions
Yesterday=`TZ=GMT+24 date +%d-%b-%y`;
mkdir $Yesterday
mv /tmp/logs/servicemix.log.* /tmp/logs/$Yesterday
for File in /tmp/logs/$Yesterday/app.log.*;
do gzip $File;
done
Regards
1.Replace
mkdir $Yesterday
by
mkdir -p /tmp/logs/${Yesterday}
gzip the files you have moved
When you move servicemix* files, do not gzip app* files
Use following lines of codes
TIME=`date +"%b-%d-%y"` # This Command will add date in Backup File Name.
now=$(date +"%T")
FILENAME="filename-$TIME:$now.tar.gz" # Here i define Backup file name format.
SRCDIR="/home" # Location of Important Data Directory (Source of backup).
DESDIR="/home/user/backup" # Destination of backup file.
tar -cpzf $DESDIR/$FILENAME $SRCDIR #creating backup as tar file
echo
echo "Backup finished"
Were they changed or created yesterday? Use find with the correct modifier.
Yesterday=`TZ=GMT+24 date +%d-%b-%y`;
mkdir $Yesterday
# -1 is equal to last 24 hours
# ctime is for last inode modified time. If you need creation date you need a different modifier. See attached link.
find -ctime -1 -exec mv '{}' "$yesterday/" \;
But this is i think pretty much the best option: Use 'find' to determine files modified yesterday.
I seem to want the opposite of everyone else - How may i use (in bash scripts) an ls -al /some/path/to/where/ever/. to get just the entry for ".", not for everything in "."? What I'm after is the dir's date, so. in other words, what's the date on the /some/path/to/where/ever/. directory?
Doesn't have to be "ls" that is just what seemed natural.
You can do stat command:
stat -c "%y %n" .
OR for EPOCH value:
stat -c "%Y %n" .
You want to use the -d option to get the entry for the directory itself, not the contents of the directory.
ls -ld /some/path/to/where/ever
In this case, the -a option would be unnecessary, since you are not listing the contents of the given argument.
Instead of using ls, you can use stat to capture the date. This way, the date isn't in a shifting format, and you don't have to filter it out from the rest of the output:
$ stat -f "%Sm" $directory_name
Feb 10 14:19:47 2014
$ stat -f "%Dm" $directory_name
1392059987 # Number of seconds since the "Epoch" (Usually Jan 1, 1970).
The stat command varies from system to system, so read your manpage.
I'm extracting a tar file within a shell script and it is is creating folder with a name like:
supportsuite-5-20-3/
The "supportsuite" part of the name is always the same, but I don't know what the remaining characters are in advance. I'm unpacking the tar file in a script and I need the name of the resulting folder from within that script. What is the best way to put that directory name into a shell variable within the script after the tar has been extracted?
I'm script challenged, so thanks in advance for any help you can provide.
If you're assuming that all of the files in the tar file are in the same directory, you could do something like:
DIRNAME=$(tar -tvf $TARFILE | head -1 | sed -e 's:^.* \([^/]*\)/.*$:\1:')
Note that it will probably misbehave if your filenames have spaces in them. If you care about this you'll need to adjust the regex in the sed command.
This will unzip it and keep the folder name in tact.
find . -type f -name "*.tar" -exec tar -xv '{}' \;
It's a little bit of a hack, but this might help (written for bash:
tar zxf supportsuite-5-20-3.tgz
NEWDIR=$(cd supportsuite-* && pwd)