Simple backup script
Simple backup script

Today, I want to share with you a new bash script to backup your server with tar and bash.

This script performs the following steps :

  • define a backup folder
  • stop/start services
  • Remove the old backup file
  • create a backup file tar.gz

In the first part of the script, you can set your own parameters :

  • backup_folder : folder where your backup file will be created
  • backup_exclusion : array containing the folder you want to exclude
  • services_arr : array containing the service you want to stop before the backup task and start after.
# Variable definition
now=$(date +"%m_%d_%Y")
backup_exclusion=('proc' 'sys' 'dev/pts' 'home/test1' 'home/test2')
services_arr=('service1' 'service2' 'service3' 'service4')

# Functions
function join { local IFS="$1"; shift; echo "$*"; }

# Remove previous backup file
cd /$backup_folder
rm -f fullbackup_*

# Stop services before backup
for ((i=0;i<cntsrv;i++)); do
    service ${services_arr[i]} stop

for ((i=0;i<cnt;i++)); do
backup_exclusion_str=`join ' ' "${backup_exclusion[@]}"`

cd $folder_to_backup
tar -zcvpf $backup_folder/$backup_filename --directory=$folder_to_backup --exclude=$backup_folder $backup_exclusion_str .

# Start services after backup
for ((i=0;i<cntsrv;i++)); do
    service ${services_arr[i]} start


Tar manual


       tar - The GNU version of the tar archiving utility


       tar <operation> [options]

       [-]A --catenate --concatenate
       [-]c --create
       [-]d --diff --compare
       [-]r --append
       [-]t --list
       [-]u --update
       [-]x --extract --get

       Common Options:
       -C, --directory DIR
       -f, --file F
       -j, --bzip2
       -p, --preserve-permissions
       -v, --verbose
       -z, --gzip

       All Options:
       [  --atime-preserve  ]  [  -b, --blocking-factor N ] [ -B, --read-full-
       records ] [ --backup BACKUP-TYPE ] [ --block-compress ] [ -C,  --direc-
       tory DIR ] [ --check-links ] [ --checkpoint ] [ -f, --file [HOSTNAME:]F
       ] [ -F, --info-script F --new-volume-script F ] [ --force-local	  ]  [
       --format	 FORMAT ] [ -g, --listed-incremental F ] [ -G, --incremental ]
       [ --group GROUP ] [ -h, --dereference ] [ --help ] [ -i, --ignore-zeros
       ]  [  --ignore-case  ] [ --ignore-failed-read ] [ --index-file FILE ] [
       -j, --bzip2 ] [ -k, --keep-old-files ] [	 -K,  --starting-file  F  ]  [
       --keep-newer-files  ] [ -l, --one-file-system ] [ -L, --tape-length N ]
       [ -m, --touch, --modification-time ] [ -M, --multi-volume  ]  [	--mode
       PERMISSIONS  ]  [ -N, --after-date DATE, --newer DATE ] [ --newer-mtime
       DATE ] [ --no-anchored ] [ --no-ignore-case ] [ --no-overwrite-dir ]  [
       --no-recursion  ]  [ --no-same-permissions ] [ --no-wildcards ] [ --no-
       wildcards-match-slash ] [ --null	    ] [ --numeric-owner ] [ -o, --old-
       archive,	 --portability,	 --no-same-owner  ]  [	-O,  --to-stdout  ]  [
       --occurrence[=NUMBER] ] [ --overwrite ] [ --overwrite-dir ]  [  --owner
       USER ] [ -p, --same-permissions, --preserve-permissions ] [ -P, --abso-
       lute-names ] [ --pax-option KEYWORD-LIST ] [ --posix ] [ --preserve ] [
       --acls  ]  [  --selinux ] [ --xattrs ] [ --no-acls ] [ --no-selinux ] [
       --no-xattrs ] [ -R, --block-number ] [ --record-size SIZE ] [  --recur-
       sion  ] [ --recursive-unlink ] [ --remove-files ] [ --rmt-command CMD ]
       [ --rsh-command CMD ] [ -s,  --same-order,  --preserve-order  ]	[  -S,
       --sparse ] [ --same-owner ] [ --show-defaults ] [ --show-omitted-dirs ]
       [ --strip-components NUMBER, --strip-path NUMBER (1) ] [ --suffix  SUF-
       FIX  ]  [  -T, --files-from F ] [ --totals   ] [ -U, --unlink-first ] [
       --use-compress-program PROG ] [ --utc ] [ -v, --verbose ] [ -V, --label
       NAME ] [ --version  ] [ --volno-file F ] [ -w, --interactive, --confir-
       mation ] [ -W, --verify ] [ --wildcards ] [ --wildcards-match-slash ] [
       --exclude  PATTERN  ]  [	 -X,  --exclude-from  FILE ] [ -Z, --compress,
       --uncompress ] [ -z, --gzip, --gunzip, --ungzip ] [ -[0-7][lmh] ]

       (1) tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components


       This manual page documents the GNU version of tar, an archiving program
       designed	 to  store  and	 extract files from an archive file known as a
       tarfile.	 A tarfile may be made on a tape drive, however,  it  is  also
       common  to write a tarfile to a normal file.  The first argument to tar
       must be one of the options Acdrtux, followed by any optional functions.
       The  final  arguments  to tar are the names of the files or directories
       which should be archived.  The use of a directory name  always  implies
       that the subdirectories below should be included in the archive.


       tar -xvf foo.tar
	      verbosely extract foo.tar

       tar -xzf foo.tar.gz
	      extract gzipped foo.tar.gz

       tar -cjf foo.tar.bz2 bar/
	      create   bzipped	 tar  archive  of  the	directory  bar	called

       tar -xjf foo.tar.bz2 -C bar/
	      extract bzipped foo.tar.bz2 after changing directory to bar

       tar -xzf foo.tar.gz blah.txt
	      extract the file blah.txt from foo.tar.gz


       One of the following options must be used:

       -A, --catenate, --concatenate
	      append tar files to an archive

       -c, --create
	      create a new archive

       -d, --diff, --compare
	      find differences between archive and file system

       -r, --append
	      append files to the end of an archive

       -t, --list
	      list the contents of an archive

       -u, --update
	      only append files that are newer than the existing in archive

       -x, --extract, --get
	      extract files from an archive

	      delete from the archive (not for use on mag tapes!)


       -C, --directory DIR
	      change to directory DIR

       -f, --file [HOSTNAME:]F
	      use archive file or device F (default "-", meaning stdin/stdout)

       -j, --bzip2
	      filter archive through bzip2, use to decompress .bz2 files

       -p, --preserve-permissions
	      extract all protection information

       -v, --verbose
	      verbosely list files processed

       -z, --gzip, --ungzip
	      filter the archive through gzip


	      don't change access times on dumped files

       -b, --blocking-factor N
	      block size of Nx512 bytes (default N=20)

       -B, --read-full-blocks
	      reblock as we read (for reading 4.2BSD pipes)

       --backup BACKUP-TYPE
	      backup  files  instead of deleting them using BACKUP-TYPE simple
	      or numbered

	      block the output of compression program for tapes

       -C, --directory DIR
	      change to directory DIR

	      warn if number of hard links to the file on the filesystem  mis-
	      match the number of links recorded in the archive

	      print directory names while reading the archive

       -f, --file [HOSTNAME:]F
	      use archive file or device F (default "-", meaning stdin/stdout)

       -F, --info-script F --new-volume-script F
	      run script at end of each tape (implies --multi-volume)

	      archive file is local even if has a colon

       --format FORMAT
	      selects output archive format
	      v7 - Unix V7
	      oldgnu - GNU tar <=1.12
	      gnu - GNU tar 1.13
	      ustar - POSIX.1-1988
	      posix - POSIX.1-2001

       -g, --listed-incremental F
	      create/list/extract new GNU-format incremental backup

       -G, --incremental
	      create/list/extract old GNU-format incremental backup

       -h, --dereference
	      don't dump symlinks; dump the files they point to

       --help like this manpage, but not as cool

       -i, --ignore-zeros
	      ignore blocks of zeros in archive (normally mean EOF)

	      ignore case when excluding files

	      don't exit with non-zero status on unreadable files

       --index-file FILE
	      send verbose output to FILE instead of stdout

       -j, --bzip2
	      filter archive through bzip2, use to decompress .bz2 files

       -k, --keep-old-files
	      keep existing files; don't overwrite them from archive

       -K, --starting-file F
	      begin at file F in the archive

	      do not overwrite files which are newer than the archive

       -l, --one-file-system
	      stay in local file system when creating an archive

       -L, --tape-length N
	      change tapes after writing N*1024 bytes

       -m, --touch, --modification-time
	      don't extract file modified time

       -M, --multi-volume
	      create/list/extract multi-volume archive

       --mode PERMISSIONS
	      apply PERMISSIONS while adding files (see chmod(1))

       -N, --after-date DATE, --newer DATE
	      only store files newer than DATE

       --newer-mtime DATE
	      like --newer, but with a DATE

	      match any subsequenceof the name's components with --exclude

	      use case-sensitive matching with --exclude

	      don't recurse into directories

	      apply user's umask when extracting  files	 instead  of  recorded

	      don't use wildcards with --exclude

	      wildcards do not match slashes (/) with --exclude

       --null --files-from reads null-terminated names, disable --directory

	      always use numbers for user/group names

       -o, --old-archive, --portability
	      like  --format=v7;  -o  exhibits	this behavior when creating an
	      archive (deprecated behavior)

       -o, --no-same-owner
	      do not attempt to restore ownership when extracting; -o exhibits
	      this behavior when extracting an archive

       -O, --to-stdout
	      extract files to standard output

	      process  only  the  NUMBERth  occurrence	of  each  file	in the
	      archive; this option is valid only in conjunction	 with  one  of
	      the subcommands --delete, --diff, --extract or --list and when a
	      list of files is given either on the command line or via the  -T
	      option; NUMBER defaults to 1

	      preserve metadata of existing directories

	      overwrite existing files and directory metadata when extracting

	      overwrite directory metadata when extracting

       --owner USER
	      change owner of extraced files to USER

       -p, --same-permissions, --preserve-permissions
	      extract all protection information

       -P, --absolute-names
	      don't strip leading '/'s from file names

       --pax-option KEYWORD-LIST
	      used  only with POSIX.1-2001 archives to modify the way tar han-
	      dles extended header keywords

	      like --format=posix

	      like --preserve-permissions --same-order

       --acls this option causes tar to store each file's ACLs in the archive.

	      this  option  causes  tar	 to store each file's SELinux security
	      context information in the archive.

	      this option causes tar to store each file's extended  attributes
	      in  the archive. This option also enables --acls and--selinux if
	      they haven't been set already, due to the fact that the data for
	      those are stored in special xattrs.

	      This  option  causes  tar	 not  to store each file's ACLs in the
	      archive and not to extract any ACL information in an archive.

	      this option causes tar not to store each file's SELinux security
	      context  information  in	the  archive  and  not	to extract any
	      SELinux information in an archive.

	      this option  causes  tar	not  to	 store	each  file's  extended
	      attributes  in  the  archive  and	 not  to  extract any extended
	      attributes in an archive. This option also enables --no-acls and
	      --no-selinux if they haven't been set already.

       -R, --record-number
	      show record number within archive with each message

       --record-size SIZE
	      use SIZE bytes per record when accessing archives

	      recurse into directories

	      remove existing directories before extracting directories of the
	      same name

	      remove files after adding them to the archive

       --rmt-command CMD
	      use CMD instead of the default /usr/sbin/rmt

       --rsh-command CMD
	      use remote CMD instead of rsh(1)

       -s, --same-order, --preserve-order
	      list of names to extract is sorted to match archive

       -S, --sparse
	      handle sparse files efficiently

	      create extracted files with the same ownership

	      display the default options used by tar

	      print directories tar skips while operating on an archive

       --strip-components NUMBER, --strip-path NUMBER
	      strip NUMBER  of	leading	 components  from  file	 names	before

	      (1) tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-compo-

       --suffix SUFFIX
	      use SUFFIX instead of default '~' when backing up files

       -T, --files-from F
	      get names to extract or create from file F

	      print total bytes written with --create

       -U, --unlink-first
	      remove existing files before extracting files of the same name

       --use-compress-program PROG
	      access the archive through PROG which is generally a compression

       --utc  display file modification dates in UTC

       -v, --verbose
	      verbosely list files processed

       -V, --label NAME
	      create archive with volume name NAME

	      print tar program version number

       --volno-file F
	      keep track of which volume of a multi-volume archive its working
	      in FILE; used with --multi-volume

       -w, --interactive, --confirmation
	      ask for confirmation for every action

       -W, --verify
	      attempt to verify the archive after writing it

	      use wildcards with --exclude

	      wildcards match slashes (/) with --exclude

       --exclude PATTERN
	      exclude files based upon PATTERN

       -X, --exclude-from FILE
	      exclude files listed in FILE

       -Z, --compress, --uncompress
	      filter the archive through compress

       -z, --gzip, --gunzip, --ungzip
	      filter the archive through gzip

       --use-compress-program PROG
	      filter the archive through PROG (which must accept -d)

	      specify drive and density
Simple backup script

Leave a Reply

Your email address will not be published.