📜 ⬆️ ⬇️

DIY backups for dummies

It has long been known that system administrators are divided into two categories: those who are not yet doing backups and those who are already doing. At a time when some DCs are burning, and the latter suddenly disappear, it is best to belong to the second category.

So, I want to share the experience of simple automatic data backup for dummies.

Task


It is necessary to create backup copies of files and databases on a schedule. These two processes should be independent of each other, because database backup is done more often than file backup (or vice versa).
Given


purpose

Get file and database archives on the server in a specific folder. Archives must be accessible by http-protocol.

')
Decision


Since this is the first experience of writing scripts, the code may seem a bit clumsy to someone.

MySQL backup
Makes dumps of all databases available to the specified user.
#!/bin/sh # ----  -------------------------------------- # ,    BACKUP_DIR="/var/www/wscms/data/www/site.ru/backup/" #    BACKUP_FILE="sql.tar" # ,     USER="wscms" #     MySQL MDIR="mysql/" #   MySQL MHOST="localhost" # MYSQL root  MUSER="root" # MYSQL root  MPASS="***************" #  "-zcf"      "-cf"    PARAMS="-cf" #---   -------------------------- #   mysql. MYSQL="$(which mysql)" #   mysqldump MYSQLDUMP="$(which mysqldump)" #   tar TAR="$(which tar)" #   chown CHOWN="$(which chown)" #   RM RM="$(which rm)" #   MKDIR MKDIR="$(which mkdir)" # ----     DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" # ----      ,     $RM -rf $BACKUP_DIR$MDIR # ----     $MKDIR -p $BACKUP_DIR$MDIR # ----    .    for db in $DBS do FILE=$BACKUP_DIR$MDIR$db.sql $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db > $FILE done # ----   $TAR $PARAMS $BACKUP_DIR$BACKUP_FILE $BACKUP_DIR$MDIR # ----        MySQL  $RM -rf $BACKUP_DIR$MDIR $CHOWN $USER $BACKUP_DIR $CHOWN $USER $BACKUP_DIR$BACKUP_FILE exit 0 


Backup files
 #!/bin/sh # ----  -------------------------------------- # ,    BACKUP_DIR="/var/www/wscms/data/www/site.ru/backup/" #    EXT=".tar" # ,     MAIN_DIR="/var/www/" # ,      DATA_DIR="/data/www/" # ,     USER="wscms" #  "-zcf"      "-cf"    #  u      PARAMS="-cf" #---   -------------------------- #   tar TAR="$(which tar)" #   chown CHOWN="$(which chown)" RM="$(which rm)" MKDIR="$(which mkdir)" # ----     $MKDIR -p $BACKUP_DIR $CHOWN -R $USER $BACKUP_DIR # ----     for DIR in $(/usr/local/ispmgr/sbin/mgrctl -m ispmgr user | cut -d' ' -f1 | sed s/name=//) do # ----   $TAR $PARAMS $BACKUP_DIR$DIR$EXT $MAIN_DIR$DIR$DATA_DIR $CHOWN $USER $BACKUP_DIR$DIR$EXT done $CHOWN -R $USER $BACKUP_DIR exit 0 


Add to CRON as root user
/ bin / sh /backup/backup.sh
/ bin / sh /backup/sql.sh
We set the schedule we need

After the operation of these two scripts, we have backup files for each user separately and backup MySql.

Now we can do anything with them: either on a third-party server in another DC, pick them up using wget, or (as I have done) with a simple windows-application loaded into the scheduler, download backups to the local machine daily.

For security purposes, I advise you to put the following .htaccess folder in the site.ru/backup/ folder
Order Deny, Allow
Deny from all
Allow from your.IP.Address, IP.Address.VPS.If you use.wget

Good luck to you, and do not lose your data.

Source: https://habr.com/ru/post/132262/


All Articles