📜 ⬆️ ⬇️

Back up without expenses

image
Backups are an eternal problem: the data volumes are crazy, then you forget about them altogether. In my previous project dvice.ru (it is still closed, so without a link) I made an annoying mistake at the very beginning of its launch. I wrote a small crown, which at 12 o'clock at night deleted all non-activated users who registered more than 24 hours ago. But I made a mistake in the request and lost the data of users who registered before the first launch of this request. Thank God, I had all the data in the sessions, so I restored all those who logged in and checked the checkbox - remember me. It's a shame and stupid, but they learn from mistakes. Therefore, before launching my last project - inwhite.ru , I made a system of backups.

Backups were made and accumulated, the old ones were deleted, and everything was fine. Until suddenly I did not realize that I was suffering from complete garbage. I do backups, but I don’t take backups. The volume of data lying on my VPS is not so big, but you also do not send much by mail. I don’t really want to buy another VPS and upload everything via FTP / SVN / CVS / SSH, although this is also an interesting option.

Until yesterday, I was puzzled by the thought: how can I take this data? And in the evening it dawned on me.

I am a happy user of Dropbox . Although with a free account, but thanks to inwhite.ru I still have 2.5GB of space on top, i.e. Not the initial 2GB, but already as much as 4.5GB. And another 500MB place can increase. And this is already a normal platform for creating backups of average data volumes.
')
At the moment, I have files in archives backed up on 200MB, databases that I put backed up daily - 2MB per day, and backups of the database inwhite.ru, which backs up every hour (away from sin), for a week is typed for about 215MB.

I did not search for the book “HOW TO SET UP BACKUP FROM YOUR SERVER THROUGH DROPBOKS IN 24 HOURS.” Having rummaged in the big Internet, I have found article in which it is told how to configure and force to work Dropbox in the absence of GUI. This was the most important, because if you don’t have a Windows server, then it’s unlikely that you have X on your server

The article is located at this address , and I will not give it here, because The process itself is quite simple and described in sufficient detail. Let me just say a couple of things:
  1. If you started setting up Dropbox, then HOST_ID depends on the folder from which you launched Dropbox
  2. If you transfer files, for example, like me, to / usr / local / dropbox (I used to store programs there) from the location of the files ~ / .dropbox-dist discussed in the article, then you will need to get the HOST_ID again
  3. dbreadconfig hasn’t worked for me, and something tells me that you won’t just work either, unless you fix this script written in Python
  4. Consequence from p.3 - stock sqlite3
  5. The Debian / Ubuntu startup script also didn’t particularly work for me, so I’ll post it to you in a corrected form
I also allowed myself to make a few new steps, because I have my own rules on the server. You need to create a user for Dropbox:

adduser --home /home/dropbox --shell /bin/false --disabled-login dropbox

Please note that in the user's folder you will need to make another folder - “Dropbox”, because Dropbox will write files to it by default.

Those. in fact, you should get something like:

/home
/home/dropbox
# Dropbox
/home/dropbox/.dropbox
# , .. Dropbox -
/home/dropbox/Dropbox

Startup script itself


USERS= "dropbox" <br>DAEMON=/usr/local/dropbox/dropbox<br>LAUNCH=/usr/local/dropbox/dropboxd<br><br>start() {<br> echo "Starting dropbox..." <br> for dbuser in $USERS; do <br> HOMEDIR=`getent passwd $dbuser | cut -d: -f6`<br> if [ -x $DAEMON ]; then<br> HOME= "$HOMEDIR" start-stop-daemon -b -o -c $dbuser -S -u $dbuser -x $LAUNCH<br> fi<br> done<br>}<br> <br>stop() {<br> echo "Stopping dropbox..." <br> for dbuser in $USERS; do <br> if [ -x $DAEMON ]; then<br> start-stop-daemon -o -c $dbuser -K -u $dbuser -x $DAEMON<br> fi<br> done<br>}<br> <br>status() {<br> for dbuser in $USERS; do <br> HOMEDIR=`getent passwd $dbuser | cut -d: -f6`<br> USERPID=`cat $HOMEDIR/.dropbox/dropbox.pid`<br> if [ -z $USERPID ] ; then<br> echo "Dropbox for USER $dbuser: not running." <br> else <br> echo "Dropbox for USER $dbuser: running (pid $USERPID)" <br> fi<br> done<br>}<br> <br> case "$1" in <br> start)<br> start<br> ;;<br> <br> stop)<br> stop<br> ;;<br><br> restart|reload|force-reload)<br> stop<br> start<br> ;;<br> <br> status)<br> status<br> ;;<br> <br> *)<br> echo "Usage: /etc/init.d/dropbox {start|stop|reload|force-reload|restart|status}" <br> exit 1<br> <br>esac<br> <br>exit 0

But writing an article about how to use some other article there seems to me to be nonsense, so I decided to share not just this kind of variation on the topic “How to back up data from the server without unnecessary gestures”, but also scripts of the process itself.

I have already said that I backup occurs with different constancy for different sites, but I remind you again:
  1. inwhite.ru - every hour
  2. all the rest - every day
In fact, there is no particular difference in these scripts, so I will give them 1 time. There are two scripts, because we back up the databases and files, and they are different things.

So.

Backing up the database


#!/bin/bash<br><br>DATE=`date "+%Y-%m-%d" `<br>TIME=`date "+%H-%M" `<br><br># Dropbox Backup, ..<br># <br># Dropbox<br># , , , <br>HOME= "/home/dropbox/Dropbox/Backup" <br><br># , <br># , , <br># <br># , , <br># , , <br>SKIP=( "information_schema" "mysql" )<br><br>HOST= "127.0.0.1" # , <br>USER= "" # <br>PASSWORD= "" # <br><br># <br>DBS= "$(mysql -h$HOST -u$USER -p$PASSWORD -Bse 'show databases')" <br><br># , <br># , , , <br># , .. <br>in_array() {<br> haystack=( "$@" )<br> needle=$1<br> <br> unset haystack[0]<br> <br> for i in "${haystack[@]}" ; do <br> if [ "$needle" == "$i" ]; then<br> return 1<br> fi<br> done<br> <br> return 0<br>}<br><br># HERE GOES THE MAGIC <br> for DB in $DBS; do <br> # <br> in_array $DB "${SKIP[@]}" <br> <br> # <br> if [ "$?" == 0 ]; then<br> # , <br> # :<br> # /home/dropbox/Dropbox/Backup/DB/inwhite<br> mkdir -p $HOME/DB/$DB<br><br> # <br> mysqldump -h$HOST -u$USER -p$PASSWORD $DB > /tmp/db-$DB-$DATE-$TIME.sql<br> # TAR '<br> tar -Pcf /tmp/db-$DB-$DATE-$TIME.tar /tmp/db-$DB-$DATE-$TIME.sql<br> # GZIP' . , ,<br> # <br> gzip -c9 /tmp/db-$DB-$DATE-$TIME.tar > $HOME/DB/$DB/$DATE.tar.gz<br><br> # , <br> rm -f /tmp/db-$DB-$DATE-$TIME.tar<br> rm -f /tmp/db-$DB-$DATE-$TIME.sql<br><br> # , 7 <br> find $HOME/DB/$DB/* -type d -mtime +7 -exec rm -rf {} \;<br> fi<br>done<br><br>exit 0;

Now back up the files


In fact, these are two approximately identical processes with slight differences in terms of data acquisition.

#!/bin/bash<br><br>HOME= "/home/dropbox/Dropbox/Backup" <br><br>DATE=`date "+%Y-%m-%d" `<br>TIME=`date "+%H-%M" `<br><br># , <br>SKIP=( "test1.ru" "test2.ru" "test3.ru" )<br><br># , <br># /home/apache<br># , , / var /www, <br># :<br># DOMAINS=`find / var /www -maxdepth 1 -type d | sed 's/\/var\/www//' | sed 's/\///' `<br>DOMAINS=`find /home/apache -maxdepth 1 -type d | sed 's/\/home\/apache//' | sed 's/\///' `<br><br>in_array() {<br> haystack=( "$@" )<br> needle=$1<br> <br> unset haystack[0]<br> <br> for i in "${haystack[@]}" ; do <br> if [ "$needle" == "$i" ]; then<br> return 1<br> fi<br> done<br> <br> return 0<br>}<br><br> for DOMAIN in $DOMAINS; do <br> in_array $DOMAIN "${SKIP[@]}" <br><br> if [ "$?" == 0 ]; then<br> mkdir -p /$HOME/FS/$DOMAIN<br><br> tar -Pcf /tmp/fs-$DOMAIN-$DATE-$TIME.tar /home/apache/$DOMAIN<br> gzip -c9 /tmp/fs-$DOMAIN-$DATE-$TIME.tar > /$HOME/FS/$DOMAIN/$DATE.tar.gz<br><br> # TAR, .. SQL <br> # <br> rm -f /tmp/fs-$DOMAIN-$DATE-$TIME.tar<br><br> find /$HOME/FS/$DOMAIN/* -type d -mtime +7 -exec rm -rf {} \;<br> fi<br>done;<br><br>exit 0;

I call them database and filesystem respectively, so after installing them in /etc/cron.daily you need to do:

chmod +x /etc/cron.daily/database
chmod +x /etc/cron.daily/filesystem

This, of course, the simplest option. You can do everything more “right” and put these scripts somewhere, and add the task via crontab -e. But I am the sole user of my server, and no one else has access to it, so I have the right to do it as it is easier and more convenient for me.

I also draw attention to the fact that the user on whose behalf you will be doing database backups has such rights:

SELECT, SHOW DATABASES, LOCK TABLES, SHOW VIEW

Here, in general, and all that can be told here. Thank you for your attention and enjoy your health. I hope someone it is still useful.

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


All Articles