Learn about Centmin Mod LEMP Stack today
Register Now

Creating bash script to backup files from several directories

Discussion in 'System Administration' started by Sunka, Mar 5, 2016.

  1. Sunka

    Sunka Active Member

    917
    240
    43
    Oct 31, 2015
    Rijeka, Croatia
    Ratings:
    +388
    Local Time:
    2:48 PM
    Nginx 1.13.3
    MariaDB 10.1.24
    Could someone help me to create bash script wich will be run with cron once every night.
    I want to put script in xxx folder, execute with cron and result should be in same folder.
    Script is going to copy several files from several different directories into xxx folder, gzip that files with date name for gzip and delete then that copied files and leave just gzip file with all that files in there. Also in script has to be included mtime command to delete gzip files older than 5 days (-mtime +3).

    Basically, I want to copy random conf and ini files, and gziped in one folder.
    I have full path for every file.

    Thanks
     
  2. eva2000

    eva2000 Administrator Staff Member

    30,161
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,136
    Local Time:
    10:48 PM
    Nginx 1.13.x
    MariaDB 5.5
    Might want to read some of the bash / shell related threads in this sub forum as there's a few tutorial threads including creating shell script discussions - see tag at bash | Centmin Mod Community
     
    • Optimistic Optimistic x 1
  3. Sunka

    Sunka Active Member

    917
    240
    43
    Oct 31, 2015
    Rijeka, Croatia
    Ratings:
    +388
    Local Time:
    2:48 PM
    Nginx 1.13.3
    MariaDB 10.1.24
    I ended up with classic cp files to new folder. Running with cron every 24 hours and backuped with rsnapshot to another backup server will be enough.
    Because cp command will overwrite older files with new one, I do not need mtime to clear older one, and because files are so small in size, I do not need gziped.
    Of course that I have hard time to chown script, and also edit eol to unix... new staff for me, but google answered me

    In case that anyone need, this is my "bash script" to copy important (for me) conf and ini files, to have it just in case.

    Code:
    #!/bin/bash
    /bin/cp /etc/logrotate.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/lib/php.ini /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/my.cnf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/redis.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/redis-sentinel.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/centminmod/custom_config.inc /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/centminmod/php.d/zendopcache.ini /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/centminmod/php.d/zzz_customphp.ini /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/csf/csf.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/elasticsearch/elasticsearch.yml /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /etc/logwatch/conf/logwatch.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/etc/php-fpm.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/nginx.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/php.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/drop.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/staticfiles.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/conf.d/virtual.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/conf.d/ssl.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/conf.d/pijanitvor.com.ssl.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/brotli_inc.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/maintenance.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup
    /bin/cp /usr/local/nginx/conf/php-rediscache.conf /home/nginx/domains/pijanitvor.com/backup/conf_backup