I am very new in bash and never coded in before but this task is stuck so need to get rid of it . I need to make bash script to make a single compressed file with several dirs.
Like -
/home/code/bots/
/var/config/
.
.
.
/var/system/
and all will be compressed to single file /var/file/bkup.[zip][tar.gz]
Thanks in advance
tar -cf {tar-filename} /path/to/dir # step 1 - create the tarfile. gzip {tar-filename} # step 2 - compress the tarfile. However you can instruct the tar command to also do the gzipping for you: tar -cvzf {tar-filename} /path/to/dir # Here, tar compresses the tar file using the gzip utility.
gzip compression over tar archive with -z option It is the archive of every . txt file. The command is as follows: $ tar cvzf file.
# tar: (c)reate g(z)ip (v)erbose (f)ile [filename.tar.gz] [contents]...
tar -czvf /var/file/bkup.tar.gz /home/code/bots /var/config /var/system
# zip: (r)ecursive [filename.zip] [contents]...
zip -r /var/file/bkup.zip /home/code/bots /var/config /var/system
The problem as you've described it doesn't require a bash script, just tar
.
tar cvzf /var/file/bkup.tar.gz /home/code/bots/ /var/config/ . . . /var/system/
You could create a bash file for it, if you intend to run it in a cronjob for example and add some other commands like a mysqldump beforehand
You need to create a file like backup.sh with the following contents
(You may need to alter the path to bash, you can find bash with whereis bash
)
#!/bin/bash
#
# Backup script
#
# Format: YEAR MONTH DAY - HOUR MINUTE SECOND
DATE=$(date +%Y%m%d-%H%M%S)
# MySQL backup file
MYSQLTARGET="/var/file/backup-mysql-$DATE.sql"
# Target file
TARTARGET="/var/file/backup-$DATE.tar.gz"
# MySQL dump
# you cannot have a space between the option and the password. If you omit the password value
# following the --password or -p option on the command line, you are prompted for one.
mysqldump -u root -ppassword --all-databases > $MYSQLTARGET
tar -czvf $TARTARGET $MYSQLTARGET /home/code/bots /var/config /var/system
PS. This is untested code. It's just an example of how a bash script works in the current replied context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With