snipt

Ctrl+h for KB shortcuts

Bash

Bash: Backup file, without overwrite

1
2
3
4
5
6
7
# Backup file, without overwrite
function bf {
  SAN_TIMESTAMP=$(date +%y%m%d)'_'$(date +%H%M%S)
  cp $1 $1'.backup.'$SAN_TIMESTAMP
  # Prevent actually overwriting the file by making it read only.
  chmod 444 $1'.backup.'$SAN_TIMESTAMP
}
https://snipt.net/embed/87f4403b982dedb92d5a35b020f76019/
/raw/87f4403b982dedb92d5a35b020f76019/
87f4403b982dedb92d5a35b020f76019
bash
Bash
7
2019-06-19T09:07:35
True
False
False
/api/public/snipt/39369/
bash-backup-file-without-overwrite
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1"># Backup file, without overwrite</span> </span><span id="L-2"><a name="L-2"></a><span class="k">function</span> bf <span class="o">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="nv">SAN_TIMESTAMP</span><span class="o">=</span><span class="k">$(</span>date +%y%m%d<span class="k">)</span><span class="s1">&#39;_&#39;</span><span class="k">$(</span>date +%H%M%S<span class="k">)</span> </span><span id="L-4"><a name="L-4"></a> cp <span class="nv">$1</span> <span class="nv">$1</span><span class="s1">&#39;.backup.&#39;</span><span class="nv">$SAN_TIMESTAMP</span> </span><span id="L-5"><a name="L-5"></a> <span class="c1"># Prevent actually overwriting the file by making it read only.</span> </span><span id="L-6"><a name="L-6"></a> chmod <span class="m">444</span> <span class="nv">$1</span><span class="s1">&#39;.backup.&#39;</span><span class="nv">$SAN_TIMESTAMP</span> </span><span id="L-7"><a name="L-7"></a><span class="o">}</span> </span></pre></div> </td></tr></table>
backup, bash

Bash

database backup on Plesk VPS

1
2
3
mysqldump --add-drop-table -u admin -p`cat /etc/psa/.psa.shadow` dbanme > dbname.sql

mysqldump -u admin -p`cat /etc/psa/.psa.shadow ` dbname > dump.sql
https://snipt.net/embed/67798b161cfe28059095f7934df6902e/
/raw/67798b161cfe28059095f7934df6902e/
67798b161cfe28059095f7934df6902e
bash
Bash
3
2019-06-06T06:56:44
True
False
False
Dec 11, 2012 at 02:59 PM
/api/public/snipt/36952/
database-backup
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>mysqldump --add-drop-table -u admin -p<span class="sb">`</span>cat /etc/psa/.psa.shadow<span class="sb">`</span> dbanme &gt; dbname.sql </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a>mysqldump -u admin -p<span class="sb">`</span>cat /etc/psa/.psa.shadow <span class="sb">`</span> dbname &gt; dump.sql </span></pre></div> </td></tr></table>
backup, database, mysql, mysqldump

MySQL

db backup adding date to filename

#db backup adding date to filename
mysqldump -u dbuser --password=password dbname | gzip > db_backup_$(date +%F).gz
https://snipt.net/embed/e7b434841fcd15c3e104f7dae94a4d41/
/raw/e7b434841fcd15c3e104f7dae94a4d41/
e7b434841fcd15c3e104f7dae94a4d41
mysql
MySQL
2
2019-06-24T05:17:06
True
False
False
/api/public/snipt/30206/
db-backup-adding-date-to-filename
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">#db backup adding date to filename</span> </span><span id="L-2"><a name="L-2"></a><span class="n">mysqldump</span> <span class="o">-</span><span class="n">u</span> <span class="n">dbuser</span> <span class="o">--</span><span class="n">password</span><span class="o">=</span><span class="n">password</span> <span class="n">dbname</span> <span class="o">|</span> <span class="n">gzip</span> <span class="o">&gt;</span> <span class="n">db_backup_</span><span class="err">$</span><span class="p">(</span><span class="kt">date</span> <span class="o">+%</span><span class="n">F</span><span class="p">).</span><span class="n">gz</span> </span></pre></div> </td></tr></table>
backup, bash, mysql

MySQL

backup and restore all MySQL databases with 2 simple commands

1
2
3
4
5
6
7
8
#backup all databases in one file (eventually add the option --add-locks):
mysqldump -u username -p -all-databases > file.sql

#backup all databases in one gzipped file:
mysqldump -u username -p -all-databases | gzip > file.sql.gz

#restore all databases:
mysql -u username -p < file.sql 
https://snipt.net/embed/1742a7a1c6ba85b1398a7ddf7f216114/
/raw/1742a7a1c6ba85b1398a7ddf7f216114/
1742a7a1c6ba85b1398a7ddf7f216114
mysql
MySQL
8
2019-06-24T05:17:18
True
False
False
/api/public/snipt/28295/
backup-and-restore-all-mysql-databases-with-2-simple-commands
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">#backup all databases in one file (eventually add the option --add-locks):</span> </span><span id="L-2"><a name="L-2"></a><span class="n">mysqldump</span> <span class="o">-</span><span class="n">u</span> <span class="n">username</span> <span class="o">-</span><span class="n">p</span> <span class="o">-</span><span class="err">–</span><span class="k">all</span><span class="o">-</span><span class="k">databases</span> <span class="o">&gt;</span> <span class="n">file</span><span class="p">.</span><span class="k">sql</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">#backup all databases in one gzipped file:</span> </span><span id="L-5"><a name="L-5"></a><span class="n">mysqldump</span> <span class="o">-</span><span class="n">u</span> <span class="n">username</span> <span class="o">-</span><span class="n">p</span> <span class="o">-</span><span class="err">–</span><span class="k">all</span><span class="o">-</span><span class="k">databases</span> <span class="o">|</span> <span class="n">gzip</span> <span class="o">&gt;</span> <span class="n">file</span><span class="p">.</span><span class="k">sql</span><span class="p">.</span><span class="n">gz</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="c1">#restore all databases:</span> </span><span id="L-8"><a name="L-8"></a><span class="n">mysql</span> <span class="o">-</span><span class="n">u</span> <span class="n">username</span> <span class="o">-</span><span class="n">p</span> <span class="o">&lt;</span> <span class="n">file</span><span class="p">.</span><span class="k">sql</span> </span></pre></div> </td></tr></table>
backup, mysql, restore

Bash

backup mysql

1
2
3
4
5
#hacer copia de seguridad de base de datos en mysql
mysqldump --opt --password=miclave --user=miuser mibasededatos > archivo.sql

#resturar la copia de seguridad
mysql --password=miclave --user=miuser mibase < archivo.sql
https://snipt.net/embed/00751c547d341c6d8f47bfc5b21d68d2/
/raw/00751c547d341c6d8f47bfc5b21d68d2/
00751c547d341c6d8f47bfc5b21d68d2
bash
Bash
5
2019-06-23T23:15:36
True
False
False
/api/public/snipt/27942/
backup-mysql
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">#hacer copia de seguridad de base de datos en mysql</span> </span><span id="L-2"><a name="L-2"></a>mysqldump --opt --password<span class="o">=</span>miclave --user<span class="o">=</span>miuser mibasededatos &gt; archivo.sql </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">#resturar la copia de seguridad</span> </span><span id="L-5"><a name="L-5"></a>mysql --password<span class="o">=</span>miclave --user<span class="o">=</span>miuser mibase &lt; archivo.sql </span></pre></div> </td></tr></table>
backup, dump, mysql, mysqldump, restore

Bash

dump postgres schema

1
2
3
4
5
#dump postgres
su - postgres -c "/usr/bin/pg_dump dbname | gzip -c > /path/dbname.sql.gz"

#dump only schema
su - postgres -c "/usr/bin/pg_dump -s dbname | gzip -c > /path/dbname_schema.sql.gz"
https://snipt.net/embed/869683e174a385507555fd0ec84d98a0/
/raw/869683e174a385507555fd0ec84d98a0/
869683e174a385507555fd0ec84d98a0
bash
Bash
5
2019-06-16T02:00:34
True
False
False
/api/public/snipt/27810/
dump-postgres-schema
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">#dump postgres</span> </span><span id="L-2"><a name="L-2"></a>su - postgres -c <span class="s2">&quot;/usr/bin/pg_dump dbname | gzip -c &gt; /path/dbname.sql.gz&quot;</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">#dump only schema</span> </span><span id="L-5"><a name="L-5"></a>su - postgres -c <span class="s2">&quot;/usr/bin/pg_dump -s dbname | gzip -c &gt; /path/dbname_schema.sql.gz&quot;</span> </span></pre></div> </td></tr></table>
backup, db, dump, postgres